Documentation
¶
Overview ¶
Package style is the single place that decides how text looks.
Detection (TTY? NO_COLOR? TERM=dumb? CI? old Windows console?) happens here once, so every app renders identically on Linux, macOS, and Windows. App code never prints with fmt directly and never imports color libraries directly; it asks style for writers and paint.
Index ¶
- Constants
- func Color(f *os.File, noColor bool) bool
- func ColorDepth() int
- func ColorFor(w io.Writer, noColor bool) bool
- func Emoji() bool
- func EnsureConsole() error
- func EscapeLen(s string, i int) int
- func IsTerminal(f *os.File) bool
- func Paint(enabled bool, attr Attribute, s string) string
- func RuneWidth(r rune) int
- func Stderr() io.Writer
- func Stdout() io.Writer
- func TerminalWidth(f *os.File) int
- func Truncate(s string, max int) string
- func VisibleWidth(s string) int
- func Width(s string) int
- type Attribute
- type Scheme
- func (s Scheme) Bold(str string) string
- func (s Scheme) Cyan(str string) string
- func (s Scheme) FailureIcon() string
- func (s Scheme) Faint(attr Attribute, str string) string
- func (s Scheme) FaintRGB(r, g, b int, str string) string
- func (s Scheme) Green(str string) string
- func (s Scheme) Italic(str string) string
- func (s Scheme) Muted(str string) string
- func (s Scheme) Paint(attr Attribute, str string) string
- func (s Scheme) Red(str string) string
- func (s Scheme) Strikethrough(str string) string
- func (s Scheme) SuccessIcon() string
- func (s Scheme) TableHeader(str string) string
- func (s Scheme) Underline(str string) string
- func (s Scheme) WarningIcon() string
- func (s Scheme) Yellow(str string) string
Constants ¶
const ( Depth16 = 16 Depth256 = 256 DepthTrue = 1 << 24 )
Color depth tiers reported by ColorDepth.
const DefaultTerminalWidth = 80
DefaultTerminalWidth is used when detection fails (pipes, files, dumb terminals): the classic 80 columns.
Variables ¶
This section is empty.
Functions ¶
func Color ¶
Color reports whether ANSI color codes may be written to f. Precedence: noColor flag, NO_COLOR env, TERM=dumb, not-a-tty, CI.
Pass the --no-color flag value as noColor. Never read os.Stdout directly for this decision; always pass the actual destination file.
func ColorDepth ¶ added in v1.2.1
func ColorDepth() int
ColorDepth reports what ANSI color tiers the terminal can paint: COLORTERM truecolor/24bit wins (case-insensitive), a real Windows console paints 24-bit once VT is enabled, then 256-color terms, else the 16-color base set. One detector feeds text dimming and image backends alike.
func ColorFor ¶ added in v1.1.0
ColorFor reports whether ANSI may be written to w: false for --no-color, non-file writers (buffers, pipes without files), dumb terminals, and CI; true only for capable terminal files.
func Emoji ¶
func Emoji() bool
Emoji reports whether emoji glyphs may be used. False on dumb terminals, in CI, and on legacy Windows consoles (conhost without Windows Terminal, detected via missing WT_SESSION), where raster fonts cannot render them and they break column layout.
func EnsureConsole ¶ added in v1.1.0
func EnsureConsole() error
EnsureConsole prepares the console for ANSI output. No-op on Unix.
func EscapeLen ¶ added in v1.2.1
EscapeLen returns the byte length of the ANSI escape sequence beginning at index i, or 0 when no escape starts there (i out of range or not an ESC byte). It covers CSI, OSC (BEL- or ST-terminated), and two-byte escapes, so walkers can skip sequences while measuring or slicing styled text.
func IsTerminal ¶
IsTerminal reports whether f is an interactive terminal. Pipes, files, and CI log collectors are not terminals.
func Paint ¶
Paint wraps s in the given ANSI color when enabled is true, and returns s unchanged when false.
The per-instance switch (not the global color.NoColor) keeps this hermetic: parallel code paths can paint differently without racing.
func RuneWidth ¶ added in v1.2.1
RuneWidth returns the terminal column width of one rune: 2 for CJK wide runes, 0 for combining marks, 1 otherwise. Wrappers use it to never split a wide rune across lines.
func Stdout ¶
Stdout returns a writer for standard output that translates ANSI codes on legacy Windows consoles and passes through everywhere else. Always write through this, never os.Stdout directly.
func TerminalWidth ¶ added in v1.1.0
TerminalWidth returns console columns for f, or DefaultTerminalWidth when unknown. Never returns zero or negative.
func Truncate ¶
Truncate shortens s to at most max columns, appending "..." when cut. A max below 4 still returns a valid (shorter) string, never panics.
func VisibleWidth ¶ added in v1.2.1
VisibleWidth returns the terminal column width of s ignoring ANSI escape sequences (CSI colors, OSC hyperlinks): styled text measures by its glyphs, so it embeds in boxes and tables without skewing alignment.
Types ¶
type Attribute ¶ added in v1.2.0
Attribute is a foreground color value. It aliases fatih's attribute type, so style constants flow into Faint and Paint while user code imports only terminull.
const ( FgBlack Attribute = Attribute(color.FgBlack) FgRed Attribute = Attribute(color.FgRed) FgGreen Attribute = Attribute(color.FgGreen) FgYellow Attribute = Attribute(color.FgYellow) FgBlue Attribute = Attribute(color.FgBlue) FgMagenta Attribute = Attribute(color.FgMagenta) FgCyan Attribute = Attribute(color.FgCyan) FgWhite Attribute = Attribute(color.FgWhite) FgHiBlack Attribute = Attribute(color.FgHiBlack) FgHiRed Attribute = Attribute(color.FgHiRed) FgHiGreen Attribute = Attribute(color.FgHiGreen) FgHiYellow Attribute = Attribute(color.FgHiYellow) FgHiBlue Attribute = Attribute(color.FgHiBlue) FgHiMagenta Attribute = Attribute(color.FgHiMagenta) FgHiCyan Attribute = Attribute(color.FgHiCyan) FgHiWhite Attribute = Attribute(color.FgHiWhite) )
Foreground colors for Faint and Paint. Values match the engine's (asserted in tests); the set is stable, so pinning it here carries no drift risk.
const ( Italic Attribute = Attribute(color.Italic) Underline Attribute = Attribute(color.Underline) Strikethrough Attribute = Attribute(color.CrossedOut) )
Text decorations for Paint and the Scheme helpers. Values match the engine's (asserted in tests). Every probed modern terminal renders all three; terminals without support show plain text, which is why they stay behind the same Enabled gate as colors.
type Scheme ¶ added in v1.1.0
type Scheme struct {
Enabled bool
}
Scheme paints named styles, returning text unchanged when disabled. Build one per command from terminal detection:
scheme := style.NewScheme(style.ColorFor(out, noColor))
Every method is safe on dumb terminals, pipes, and legacy consoles when disabled, and degrades to plain text rather than raw escapes.
func (Scheme) FailureIcon ¶ added in v1.1.0
FailureIcon is a red X, plain otherwise.
func (Scheme) Faint ¶ added in v1.1.2
Faint paints str in a half-luminance variant of attr, working on every terminal that renders the color at all — unlike the SGR faint flag (SGR 2), whose dimming strength varies per terminal and whose reset (SGR 22) also clears bold. Returns str unchanged when disabled or when attr is not a foreground color.
func (Scheme) FaintRGB ¶ added in v1.1.2
FaintRGB paints str in a half-luminance variant of an arbitrary color, for custom palettes, hex-derived values, and status colors outside the 16 named attributes. Out-of-range channels clamp to 0-255. On 16-color terminals it falls back to the nearest base color, documented as an approximation where dimming is unrepresentable.
func (Scheme) Muted ¶ added in v1.1.0
Muted dims secondary text in the current color via SGR faint, which is the only way to dim without knowing the terminal foreground. For a deterministic dim of a named color use Faint, for any RGB use FaintRGB.
func (Scheme) Strikethrough ¶ added in v1.2.1
Strikethrough strikes text.
func (Scheme) SuccessIcon ¶ added in v1.1.0
SuccessIcon is a green check, plain otherwise.
func (Scheme) TableHeader ¶ added in v1.1.0
TableHeader uppercases and underlines a table header when enabled, returning it verbatim otherwise so piped output stays script-friendly.
func (Scheme) WarningIcon ¶ added in v1.1.0
WarningIcon is a yellow bang, plain otherwise.