style

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func Color(f *os.File, noColor bool) bool

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 ColorFor added in v1.1.0

func ColorFor(w io.Writer, noColor bool) bool

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 IsTerminal

func IsTerminal(f *os.File) bool

IsTerminal reports whether f is an interactive terminal. Pipes, files, and CI log collectors are not terminals.

func Paint

func Paint(enabled bool, attr color.Attribute, s string) string

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 Stderr

func Stderr() io.Writer

Stderr returns a writer for standard error with the same guarantee as Stdout.

func Stdout

func Stdout() io.Writer

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

func TerminalWidth(f *os.File) int

TerminalWidth returns console columns for f, or DefaultTerminalWidth when unknown. Never returns zero or negative.

func Truncate

func Truncate(s string, max int) string

Truncate shortens s to at most max columns, appending "..." when cut. A max below 4 still returns a valid (shorter) string, never panics.

func Width

func Width(s string) int

Width returns the terminal column width of s. East-Asian wide runes count as 2, so CJK text never breaks tables.

Types

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 NewScheme added in v1.1.0

func NewScheme(enabled bool) Scheme

NewScheme builds a Scheme with explicit enablement.

func (Scheme) Bold added in v1.1.0

func (s Scheme) Bold(str string) string

Bold bolds text.

func (Scheme) Cyan added in v1.1.0

func (s Scheme) Cyan(str string) string

Cyan paints highlights.

func (Scheme) FailureIcon added in v1.1.0

func (s Scheme) FailureIcon() string

FailureIcon is a red X, plain otherwise.

func (Scheme) Green added in v1.1.0

func (s Scheme) Green(str string) string

Green paints success.

func (Scheme) Muted added in v1.1.0

func (s Scheme) Muted(str string) string

Muted dims secondary text.

func (Scheme) Paint added in v1.1.0

func (s Scheme) Paint(attr color.Attribute, str string) string

Paint wraps s in attr when enabled, mirroring package Paint.

func (Scheme) Red added in v1.1.0

func (s Scheme) Red(str string) string

Red paints errors.

func (Scheme) SuccessIcon added in v1.1.0

func (s Scheme) SuccessIcon() string

SuccessIcon is a green check, plain otherwise.

func (Scheme) TableHeader added in v1.1.0

func (s Scheme) TableHeader(str string) string

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

func (s Scheme) WarningIcon() string

WarningIcon is a yellow bang, plain otherwise.

func (Scheme) Yellow added in v1.1.0

func (s Scheme) Yellow(str string) string

Yellow paints warnings.

Jump to

Keyboard shortcuts

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