termstyle

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 6 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 termstyle 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 (
	Depth16   = termcore.Depth16
	Depth256  = termcore.Depth256
	DepthTrue = termcore.DepthTrue
)

Color depth tiers reported by ColorDepth.

View Source
const DefaultTerminalWidth = termcore.DefaultTerminalWidth

DefaultTerminalWidth is used when detection fails.

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.

func ColorDepth

func ColorDepth() int

ColorDepth reports what ANSI color tiers the terminal can paint.

func ColorFor

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

ColorFor reports whether ANSI may be written to w.

func Emoji

func Emoji() bool

Emoji reports whether emoji glyphs may be used.

func EnsureConsole

func EnsureConsole() error

EnsureConsole prepares the console for ANSI output.

func EscapeLen

func EscapeLen(s string, i int) int

EscapeLen returns the byte length of the ANSI escape sequence beginning at index i, or 0 when no escape starts there.

func IsTerminal

func IsTerminal(f *os.File) bool

IsTerminal reports whether f is an interactive terminal.

func Paint

func Paint(enabled bool, attr 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 RuneWidth

func RuneWidth(r rune) int

RuneWidth returns the terminal column width of one rune.

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.

func TerminalWidth

func TerminalWidth(f *os.File) int

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

func Truncate

func Truncate(s string, max int) string

Truncate shortens s to at most max columns, appending "..." when cut.

func VisibleWidth

func VisibleWidth(s string) int

VisibleWidth returns the terminal column width of s ignoring ANSI escape sequences.

func Width

func Width(s string) int

Width returns the terminal column width of s.

Types

type Attribute

type Attribute = color.Attribute

Attribute is a foreground color value. It aliases the owned color engine's attribute type, so style constants flow into Faint and Paint while user code imports only terminull.

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

type Scheme struct {
	Enabled bool
}

Scheme paints named styles, returning text unchanged when disabled. Build one per command from terminal detection:

scheme := termstyle.NewScheme(termstyle.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

func NewScheme(enabled bool) Scheme

NewScheme builds a Scheme with explicit enablement.

func (Scheme) Bold

func (s Scheme) Bold(str string) string

Bold bolds text.

func (Scheme) Cyan

func (s Scheme) Cyan(str string) string

Cyan paints highlights.

func (Scheme) FailureIcon

func (s Scheme) FailureIcon() string

FailureIcon is a red X, plain otherwise.

func (Scheme) Faint

func (s Scheme) Faint(attr Attribute, str string) string

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

func (s Scheme) FaintRGB(r, g, b int, str string) string

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) Green

func (s Scheme) Green(str string) string

Green paints success.

func (Scheme) Italic

func (s Scheme) Italic(str string) string

Italic italicizes text.

func (Scheme) Muted

func (s Scheme) Muted(str string) string

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) Paint

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

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

func (Scheme) Red

func (s Scheme) Red(str string) string

Red paints errors.

func (Scheme) Strikethrough

func (s Scheme) Strikethrough(str string) string

Strikethrough strikes text.

func (Scheme) SuccessIcon

func (s Scheme) SuccessIcon() string

SuccessIcon is a green check, plain otherwise.

func (Scheme) TableHeader

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) Underline

func (s Scheme) Underline(str string) string

Underline underlines text.

func (Scheme) WarningIcon

func (s Scheme) WarningIcon() string

WarningIcon is a yellow bang, plain otherwise.

func (Scheme) Yellow

func (s Scheme) Yellow(str string) string

Yellow paints warnings.

Directories

Path Synopsis
internal
color
Package color is an ANSI color package to output colorized or SGR defined output to the standard output.
Package color is an ANSI color package to output colorized or SGR defined output to the standard output.

Jump to

Keyboard shortcuts

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