Documentation
¶
Overview ¶
Package theme ...
Index ¶
- Constants
- func Apply(cfg *config.ThemeConfig) error
- func ApplyUserConfig(user *config.ThemeConfig) (warnings []string)
- func ClassifyColor(token string) (Kind, ColorPayload)
- func ColorParamSGR(token string, layer Layer) string
- func ColorSGR(token string, layer Layer) string
- func Current() *themeState
- func IsMonochrome() bool
- func PreviewSGR(s string) string
- func SetMonochromeForTest(v bool) (restore func())
- type ColorPayload
- type Kind
- type Layer
- type Style
Constants ¶
const AnsiReset = "\x1b[0m"
AnsiReset is the ANSI SGR sequence that clears all attributes. Pair it with the escape returned by ColorSGR to bound a tinted span.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
func Apply(cfg *config.ThemeConfig) error
Apply atomically swaps the active theme to the one described by cfg. It returns a non-nil error when cfg is nil, in which case the previously applied theme is preserved. Unknown color strings are accepted and stored verbatim; the rendering layer decides how to interpret them.
func ApplyUserConfig ¶ added in v0.4.0
func ApplyUserConfig(user *config.ThemeConfig) (warnings []string)
ApplyUserConfig validates the colour tokens in the user's theme config, applies it as the active theme, and returns warnings for any tokens that do not name a renderable colour. It does NOT print the warnings — the caller (app bootstrap) decides where they surface (see epic pgsavvy-w1gh.3).
theme.Apply is called directly with user (no overlay onto DefaultDark): config.LoadUserConfig already overlays the user's YAML onto the full GetDefaultConfig() baseline, so by the time a *config.ThemeConfig reaches here every field is populated — unset keys carry the DefaultDark default, set keys carry the user value. The config<->builtin invariant test (pgsavvy-w1gh.2) guards that baseline. There is therefore no err to return: Apply only errors on a nil cfg and user is never nil here.
Validation reuses parseStyle — the SAME tokenizer the renderer uses — so the tokens it classifies are exactly the ones rendered as colours: the Fg token and the 'on' Bg token. Attribute keywords (bold/underline/italic) and stray trailing barewords never land in Fg/Bg, so they are never classified and cannot produce a false-positive warning (e.g. "blue notacolor" renders blue and warns nothing). Only a genuinely unrenderable Fg/Bg yields a warning.
func ClassifyColor ¶ added in v0.4.0
func ClassifyColor(token string) (Kind, ColorPayload)
ClassifyColor classifies a color token into a Kind plus its ColorPayload.
The token is lowercased internally because theme.Style stores Fg/Bg verbatim (only switch keys were lowercased upstream). Classification is allocation-free on the hot path: it runs per grid cell and per syntax token, so it uses only a switch plus strconv parsing — no regexp compilation and no map allocation.
The returned ColorPayload is the shared cross-emitter contract; see its godoc. Injection-unsafe tokens (containing ';', 'm', escape/OSC bytes, etc.) fall through every strict branch and classify as Unknown, so no raw token can ever reach an escape sequence.
func ColorParamSGR ¶ added in v0.4.0
ColorParamSGR returns the BARE SGR parameter for a color token on the given layer — e.g. "33", "90", "38;5;42", "38;2;255;136;0" — or "" for empty and unknown tokens. The result never contains "\x1b[" or a trailing "m"; callers compose it into a larger escape (the highlight path appends it after bold/italic/underline params). Bold is the caller's concern; this never emits it.
func ColorSGR ¶ added in v0.4.0
ColorSGR returns the full "\x1b[<param>m" escape for a color token on the given layer, or "" when ColorParamSGR returns "".
func Current ¶
func Current() *themeState
Current returns the currently-active theme snapshot. The pointer is safe to read concurrently with calls to Apply; readers should treat the returned snapshot as immutable. Current never returns nil: if no theme has been applied yet it lazily applies the built-in default-dark theme.
func IsMonochrome ¶
func IsMonochrome() bool
IsMonochrome reports whether the runtime should suppress color output in renderers that otherwise paint accent colors (e.g. EXPLAIN plan cost-percentile coloring). Returns true when the NO_COLOR environment variable is set to any non-empty value (per https://no-color.org).
The value is resolved once on first call and cached for the lifetime of the process — subsequent calls are O(1). Production callers do not need to invalidate; the variable is read at startup.
func PreviewSGR ¶ added in v0.7.0
PreviewSGR returns a combined ANSI SGR escape that renders a style string in its own colours for live-preview displays (e.g. settings theme tab). It handles compound values like "black on yellow", "bold #ff8800", "on yellow", and single-colour tokens. Returns "" for empty, unknown, or monochrome inputs.
func SetMonochromeForTest ¶
func SetMonochromeForTest(v bool) (restore func())
SetMonochromeForTest forces the cached monochrome value to v, marking the once as resolved so subsequent IsMonochrome() calls return v regardless of the NO_COLOR env var. It returns a restore func that reverts the once + value to their prior state. Test-only seam: the production monochrome cache is a process-lifetime sync.Once with no env re-read, so a no-color render test cannot otherwise drive IsMonochrome deterministically. Do NOT call from production code.
Types ¶
type ColorPayload ¶ added in v0.4.0
type ColorPayload struct {
// Palette is the 0..15 named-color index (Named16 only).
Palette int
// Index is the 0..255 palette index (Index256 only).
Index int
// R, G, B are the 0..255 channel values (Hex only).
R, G, B int
}
ColorPayload is the shared cross-emitter classification contract returned by ClassifyColor. It is consumed by BOTH the SGR emitter in this package and the border emitter in a different package (the truecolor / 256 / named border path), so every field is a plain int usable cross-package.
Only the field(s) relevant to the returned Kind are meaningful:
- Named16: Palette holds the palette index 0..15. The 8 basic colors map black=0..white=7; the 8 bright colors map brightblack=8..brightwhite=15; gray/grey alias to 8 (brightblack). This palette index is the canonical value the border path uses; the inline SGR path emits 30-37 / 90-97 (fg) and 40-47 / 100-107 (bg) instead.
- Index256: Index holds the validated index 0..255.
- Hex: R, G, B hold the 8-bit channel values 0..255 (#rgb expanded to #rrggbb for both layers).
For Empty and Unknown all fields are zero.
Note on bold: bold is emitted by callers as-authored. ClassifyColor and the SGR emitters never suppress or imply bold based on a bright color name.
type Kind ¶ added in v0.4.0
type Kind int
Kind classifies a color token into one of the supported vocabularies.
const ( // Empty is the empty token "" (no color). Empty Kind = iota // Named16 is one of the 16 named ANSI colors (8 basic + 8 bright). Named16 // Index256 is a "colorN" 256-palette index (N in 0..255). Index256 // Hex is a "#rgb" or "#rrggbb" 24-bit truecolor token. Hex // Unknown is any token that is not a valid color (also injection-unsafe // tokens such as those containing ';', 'm', or escape bytes). Unknown )