theme

package
v0.3.0-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package theme is the visual vocabulary shared by every CLI renderer: color and status glyphs.

The theme layer resolves how output looks against the environment and user preference, independent of what is being rendered:

  • Palette — a semantic color set (success/error/warn/info/dim/bold) that honors the NO_COLOR standard and TTY detection.
  • Glyphs — a semantic symbol set (✓ ✗ ⚠ ℹ • → …) with a pure-ASCII fallback for terminals without UTF-8 support.
  • Style — the palette and glyphs bundled together, so renderers thread one value instead of a separate palette/glyphs pair.
  • Theme — bold headings and right-aligned, Cargo-like action lines keyed by a semantic Tone, rendered on a resolved Palette.

Both resolve from a single boolean so callers render identically regardless of terminal capability, and both expose an environment-free constructor (NewPalette, NewGlyphs) for deterministic tests alongside the environment-driven resolvers (ResolveColor, GlyphsFromEnv).

Index

Constants

View Source
const DefaultActionWidth = 12

DefaultActionWidth is the default width, in terminal columns, of the right-aligned action-label column produced by Theme.Action.

View Source
const NoColorEnv = "NO_COLOR"

NoColorEnv is the environment variable that, when present (regardless of value), disables color regardless of any explicit choice. See the NO_COLOR standard: https://no-color.org.

Variables

This section is empty.

Functions

func NoColorEnvSet

func NoColorEnvSet() bool

NoColorEnvSet reports whether the NO_COLOR environment variable is present.

Per the NO_COLOR standard (https://no-color.org) any presence disables color, so an empty value (NO_COLOR=) still counts.

func ResolveColor

func ResolveColor(choice ColorChoice, isTerminal bool) bool

ResolveColor resolves a ColorChoice into an effective on/off decision.

Resolution order (the NO_COLOR standard takes precedence over an explicit request): if NO_COLOR is set the result is off; otherwise ColorAlways is on, ColorNever is off, and ColorAuto follows isTerminal. It reads the process environment for NO_COLOR; use ResolveColorWith for a fully injected, environment-free decision.

func ResolveColorWith

func ResolveColorWith(choice ColorChoice, noColor, isTerminal bool) bool

ResolveColorWith is the pure resolver core: it folds an explicit NO_COLOR presence and TTY detection into an effective on/off decision, so it is environment-free and unit-testable.

NO_COLOR wins over every choice; otherwise ColorAlways/ColorNever are absolute and ColorAuto follows isTerminal.

func UnicodeEnvEnabled

func UnicodeEnvEnabled() bool

UnicodeEnvEnabled reports whether the process locale advertises a UTF-8 encoding.

It consults LC_ALL, LC_CTYPE, then LANG in order and reports true when any is set to a value naming UTF-8 (case-insensitive, "utf-8" or "utf8"). When none is set the result is false, so the ASCII fallback is the safe default.

Types

type ColorChoice

type ColorChoice int

ColorChoice is a user's requested color policy, before environment/TTY resolution.

const (
	// ColorAuto enables color only when writing to a terminal and NO_COLOR is unset.
	// It is the zero value, so an unset choice defaults to auto.
	ColorAuto ColorChoice = iota
	// ColorAlways forces color on (still overridden by NO_COLOR).
	ColorAlways
	// ColorNever forces color off.
	ColorNever
)

func ParseColorChoice

func ParseColorChoice(name string) (ColorChoice, bool)

ParseColorChoice parses a choice from its lowercase name (auto/always/never).

The second return value is false for any other value, so the caller can raise its own typed usage error naming the accepted values.

func (ColorChoice) String

func (c ColorChoice) String() string

String returns the canonical lowercase name of the choice.

type Glyphs

type Glyphs struct {
	// contains filtered or unexported fields
}

Glyphs is a resolved set of semantic status glyphs.

When Unicode is disabled every accessor returns its ASCII fallback, so the same rendering code stays byte-clean on terminals that cannot display the Unicode symbols. Construct it from a resolved boolean via NewGlyphs, or from the process locale via GlyphsFromEnv.

func GlyphsFromEnv

func GlyphsFromEnv() Glyphs

GlyphsFromEnv resolves a glyph set from the process locale via UnicodeEnvEnabled.

func NewGlyphs

func NewGlyphs(unicode bool) Glyphs

NewGlyphs returns a glyph set with Unicode explicitly enabled or disabled.

func (Glyphs) Answer

func (g Glyphs) Answer() string

Answer returns the inline answer/input marker — "»" (ASCII ">").

func (Glyphs) Arrow

func (g Glyphs) Arrow() string

Arrow returns the progression arrow glyph — "→" (ASCII "->").

func (Glyphs) ArrowDown

func (g Glyphs) ArrowDown() string

ArrowDown returns the downward navigation arrow glyph — "↓" (ASCII "v").

func (Glyphs) ArrowUp

func (g Glyphs) ArrowUp() string

ArrowUp returns the upward navigation arrow glyph — "↑" (ASCII "^").

func (Glyphs) Bullet

func (g Glyphs) Bullet() string

Bullet returns the list bullet glyph — "•" (ASCII "*").

func (Glyphs) Ellipsis

func (g Glyphs) Ellipsis() string

Ellipsis returns the truncation ellipsis glyph — "…" (ASCII "...").

func (Glyphs) Error

func (g Glyphs) Error() string

Error returns the failure/error glyph — "✗" (ASCII "x").

func (Glyphs) Info

func (g Glyphs) Info() string

Info returns the informational glyph — "ℹ" (ASCII "i").

func (Glyphs) Pointer

func (g Glyphs) Pointer() string

Pointer returns the selection pointer glyph — "❯" (ASCII ">").

func (Glyphs) RadioOff

func (g Glyphs) RadioOff() string

RadioOff returns the unselected radio option glyph — "○" (ASCII "( )").

func (Glyphs) RadioOn

func (g Glyphs) RadioOn() string

RadioOn returns the selected radio option glyph — "◉" (ASCII "(*)").

func (Glyphs) Success

func (g Glyphs) Success() string

Success returns the success/completed glyph — "✓" (ASCII "v").

func (Glyphs) Unicode

func (g Glyphs) Unicode() bool

Unicode reports whether this set emits Unicode glyphs.

func (Glyphs) Warning

func (g Glyphs) Warning() string

Warning returns the warning/attention glyph — "⚠" (ASCII "!").

type Palette

type Palette struct {
	// contains filtered or unexported fields
}

Palette is a resolved, semantic color palette.

When disabled every style is the identity function, so callers render the same way regardless of terminal capability. Construct it from a resolved boolean via NewPalette, or resolve a ColorChoice against a stream's TTY status via PaletteForStream.

func NewPalette

func NewPalette(enabled bool) Palette

NewPalette returns a palette with color explicitly enabled or disabled.

func PaletteForStream

func PaletteForStream(choice ColorChoice, isTerminal bool) Palette

PaletteForStream resolves a palette for a specific output stream by folding NO_COLOR, the choice, and the stream's TTY status via ResolveColor.

func (Palette) Bold

func (p Palette) Bold(text string) string

Bold paints text bold — emphasis (headings, totals).

func (Palette) Dim

func (p Palette) Dim(text string) string

Dim paints text dimmed — secondary detail (cache/skip).

func (Palette) Enabled

func (p Palette) Enabled() bool

Enabled reports whether this palette emits color.

func (Palette) Error

func (p Palette) Error(text string) string

Error paints text red — failure/error status.

func (Palette) Info

func (p Palette) Info(text string) string

Info paints text cyan — informational/neutral highlight.

func (Palette) Success

func (p Palette) Success(text string) string

Success paints text green — successful/complete status.

func (Palette) Warn

func (p Palette) Warn(text string) string

Warn paints text yellow — warnings and attention.

type Style

type Style struct {
	// contains filtered or unexported fields
}

Style bundles a color Palette and a Glyphs set — the two visual capabilities a terminal renderer resolves from NO_COLOR and UTF-8 support — into one value, so renderers thread a single style rather than a separate palette/glyphs pair.

func NewStyle

func NewStyle(palette Palette, glyphs Glyphs) Style

NewStyle bundles a palette and glyph set into a rendering style.

func (Style) Glyphs

func (s Style) Glyphs() Glyphs

Glyphs returns the glyph set.

func (Style) Palette

func (s Style) Palette() Palette

Palette returns the color palette.

type Theme

type Theme struct {
	// contains filtered or unexported fields
}

Theme renders bold headings and right-aligned, Cargo-like action lines keyed by a semantic Tone, on a resolved Palette. It is the semantic styling layer above the raw palette: callers describe intent (a heading, or a toned action) rather than picking colors.

Labels are right-aligned by Unicode scalar count, which matches terminal columns for the ASCII/Latin action verbs it is built for ("Checking", "Compiling"). Column-exact alignment of wide (CJK/emoji) labels is intentionally left to heavier kits.

func NewTheme

func NewTheme(palette Palette) Theme

NewTheme builds a theme from a resolved palette with the default action width.

func (Theme) Action

func (t Theme) Action(label, detail string, tone Tone) string

Action renders a right-aligned, bold semantic action label followed by unstyled detail — the " Checking pkg" form familiar from Cargo. The label is right-aligned to the configured action width; a label wider than that column is emitted whole without padding rather than truncated.

func (Theme) Heading

func (t Theme) Heading(title string) string

Heading renders a bold heading line.

func (Theme) Palette

func (t Theme) Palette() Palette

Palette returns the resolved palette the theme paints with.

func (Theme) WithActionWidth

func (t Theme) WithActionWidth(width int) Theme

WithActionWidth returns a copy of the theme with the right-aligned action-label width (in columns) set; the receiver is unchanged.

type Tone

type Tone int

Tone is the semantic outcome applied to an action label, selecting the Palette color a Theme paints the label with.

const (
	// ToneSuccess marks successful or completed work.
	ToneSuccess Tone = iota
	// ToneError marks failed work.
	ToneError
	// ToneWarning marks work requiring attention.
	ToneWarning
	// ToneInfo marks neutral progress or information.
	ToneInfo
	// ToneDim marks secondary or skipped work.
	ToneDim
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL