spruce/palette
Deterministic hash-based colors for consistent terminal output.
The palette module maps strings to colors using a simple hash function,
ensuring that the same input always produces the same color. This is useful
for coloring log categories, service names, user IDs, or any other repeated
identifiers in a visually consistent way.
The palette automatically adapts to the terminal’s color support:
- When color is disabled (
NoColor),hashreturns a plain style - When 256-color or truecolor is available, it uses a broader palette
- When only basic ANSI is available, it falls back to a smaller set
import spruce
import spruce/palette
import spruce/style
pub fn main() {
let sp = spruce.detect()
let colored = style.render(sp, palette.hash(sp, "database"), "database")
// "database" will always be rendered with the same color
}
Values
pub fn hash(sp: spruce.Spruce, text: String) -> style.Style
Map a string to a deterministic color style.
The same input string will always produce the same color. The color palette adapts to the context’s color level: a broader set of colors is used when 256-color or truecolor support is detected, and a smaller set for basic ANSI.
When the context has NoColor, this returns a plain style with no color.