ui

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package ui carries the CLI's output conventions: the [error] / [warn] / [debug] prefixes of the bash implementation's verbose.sh, byte for byte off a terminal so ported commands stay indistinguishable from their argsh originals, and the terminal presentation layer (style.go) on top.

Index

Constants

This section is empty.

Variables

View Source
var ErrHandled = errors.New("handled")

ErrHandled marks an error whose message was already printed in the bash implementation's own format ([error] … on stderr). It is the ONE sentinel: every package's name for it (cli.ErrHandled, kubehz.ErrHandled, secrets.ErrHandled, …) is this value, so errors.Is holds across package boundaries and a %w wrap anywhere still reads as handled. The caller exits non-zero without printing anything further.

Functions

func Closest added in v0.5.0

func Closest(name string, candidates []string) (best string, ok bool)

Closest is the candidate nearest to name by cobra's rule: an edit distance of at most 2, or a candidate that starts with name. The first (in candidates' order) of the nearest wins. ok is false when nothing qualifies.

func Debug

func Debug(format string, a ...any)

Debug writes a [debug] line to stderr when DEBUG is set (bash: debug()).

func DebugTo

func DebugTo(w io.Writer, format string, a ...any)

DebugTo writes a [debug] line to w when DEBUG is set.

func Error

func Error(format string, a ...any)

Error writes an [error] line to stderr (bash: error()).

func ErrorNext added in v0.5.0

func ErrorNext(w io.Writer, cmd, why, format string, a ...any)

ErrorNext writes the error for w. On a terminal (the style of w, see For) the two-line shape prints: the `error:` line, then the Next line for cmd (without `lo`) and why. Otherwise the [error] line prints, byte for byte what ErrorTo prints. An empty cmd prints no second line.

func ErrorTo

func ErrorTo(w io.Writer, format string, a ...any)

ErrorTo writes an [error] line to w.

func ForceColor added in v0.5.0

func ForceColor(on bool) (restore func())

ForceColor is a test-only override of the colour decision. It applies only where TTY is true: ForceColor(true) with ForceTTY(true) renders the coloured terminal form into a buffer, ForceColor(false) keeps a forced terminal plain. Off a terminal the output stays plain whatever the override says. Production code never calls it.

func ForceTTY added in v0.5.0

func ForceTTY(on bool) (restore func())

ForceTTY is a test-only override of the terminal detection: every writer resolves as a terminal (true) or as a pipe (false), so a bytes.Buffer renders the terminal form under go test without a pty. The returned function restores the previous state. Production code never calls it.

func Handled

func Handled(err error) error

Handled marks err as already printed: the returned error reads as ErrHandled to errors.Is, keeps err's text, and unwraps to err so any sentinel or type inside it still matches. A site that prints its own [error] line and then returns an error wraps it here, and the exit mapping at the top (cli dispatchExit) prints nothing more. An error that reaches the top WITHOUT this mark was never printed, so the mapping prints it. nil stays nil.

func HuhTheme added in v0.5.0

func HuhTheme() huh.Theme

HuhTheme is the one huh theme of the CLI: base16 (the terminal's own palette) with the accent on the doctor green: the title, the selector and the selected option. The init wizard and the `lo use` select share it.

func MarkBad added in v0.5.0

func MarkBad(w io.Writer, msg string)

MarkBad writes ` ✗ msg` (red on a TTY).

func MarkInfo added in v0.5.0

func MarkInfo(w io.Writer, msg string)

MarkInfo writes ` ℹ msg`, a neutral fact that is neither a pass nor a finding. The whole line is dim on a TTY (the style the lifecycle commands use for their notes).

func MarkOK added in v0.5.0

func MarkOK(w io.Writer, msg string)

MarkOK writes ` ✓ msg` (green on a TTY).

func MarkWarn added in v0.5.0

func MarkWarn(w io.Writer, msg string)

MarkWarn writes ` ! msg` (yellow on a TTY).

func Next added in v0.5.0

func Next(w io.Writer, cmd, why string)

Next writes the one hint shape: `next: lo <cmd> # why`, dim on a TTY.

func NoticeTo added in v0.5.0

func NoticeTo(w io.Writer, format string, a ...any)

NoticeTo writes an informational line to w, or nothing under --quiet.

func Quiet added in v0.5.0

func Quiet() bool

Quiet reports the quiet level.

func RawErrorTo added in v0.5.0

func RawErrorTo(w io.Writer, format string, a ...any)

RawErrorTo writes the raw `error: …` line the bash `echo "error: …" >&2` family prints (no [error] prefix). The word is red on a TTY.

func RawWarningTo added in v0.5.0

func RawWarningTo(w io.Writer, format string, a ...any)

RawWarningTo writes the raw `warning: …` line. The word is yellow on a TTY.

func Section added in v0.5.0

func Section(w io.Writer, label string)

Section writes a section header: `--- label ---` piped, the bold label on a TTY.

func SetNoColor added in v0.5.0

func SetNoColor(on bool)

SetNoColor records the global --no-color flag (the env form is NO_COLOR). It also exports NO_COLOR=1 so every child (the provider plugins, a routed bash command) sees the same choice.

func SetQuiet added in v0.5.0

func SetQuiet(q bool)

SetQuiet sets the quiet level for the process.

func StdinIsTerminal added in v0.5.0

func StdinIsTerminal() bool

StdinIsTerminal reports whether stdin is a terminal (a prompt or a select needs both stdin and stdout on one).

func Styled added in v0.5.0

func Styled(w io.Writer, s Style) io.Writer

Styled wraps w with a fixed style. For honours the whole style: a Styled(w, Style{TTY: true, Color: false}) writer never carries colour, whatever the overrides say.

func Table added in v0.5.0

func Table(w io.Writer, header []string, rows [][]string, minWidths []int)

Table writes header, its underline and rows, columns two spaces apart (NewColumns for the width rules).

func Title added in v0.5.0

func Title(w io.Writer, text string)

Title writes a title line. Piped it is exactly text (the callers keep their literal `=== … ===`). On a TTY the `=== ` decoration goes and the text is bold.

func Warn

func Warn(format string, a ...any)

Warn writes a [warn] line to stderr (bash: warn()).

func WarnTo

func WarnTo(w io.Writer, format string, a ...any)

WarnTo writes a [warn] line to w.

Types

type Columns added in v0.5.0

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

Columns is a table in progress: the header and the measured widths, so a caller can interleave its own lines between rows (the assets diff prints the per-file rows under each unit). Table is the one-call form.

func NewColumns added in v0.5.0

func NewColumns(w io.Writer, header []string, rows [][]string, minWidths []int) *Columns

NewColumns measures the columns of header over rows. minWidths are the minimum column widths (nil, or 0 for a column: none). Piped, a column with a minimum is exactly that wide, and a cell that is wider pushes only its own row (bash printf `%-20s` semantics), so the bytes match the fixed layouts of the ported commands. On a TTY every column is measured (never narrower than its minimum), so nothing overflows.

func (*Columns) Header added in v0.5.0

func (c *Columns) Header()

Header writes the header row and its underline (one dash per header rune, the shape the addons and assets tables print today). On a TTY the header is bold and the underline dim.

func (*Columns) Row added in v0.5.0

func (c *Columns) Row(cells ...string)

Row writes one row. A missing cell is empty.

type Style added in v0.5.0

type Style struct {
	// TTY is true when the stream is a terminal: titles and sections drop
	// their === / --- decoration, tables are measured, hints are dimmed.
	TTY bool
	// Color is true when ANSI colour is allowed: a TTY, and neither
	// NO_COLOR nor --no-color is set. Never true off a TTY.
	Color bool
}

Style is what a stream can show.

func For added in v0.5.0

func For(w io.Writer) Style

For is the style of w: a Styled wrapper's own (its Color gated by NO_COLOR and --no-color like every stream), the process stdout's or stderr's for those files, plain for anything else (a buffer, a pipe, a file). Under the test overrides every other writer gets the forced style.

func Stderr added in v0.5.0

func Stderr() Style

Stderr is the style of the process's stderr.

func Stdout added in v0.5.0

func Stdout() Style

Stdout is the style of the process's stdout.

func (Style) Bold added in v0.5.0

func (s Style) Bold(text string) string

Bold and Dim are the two weights of the palette; Warn is the `!` colour for a marker inside a row the caller lays out itself (the card of `lo init`; MarkWarn writes a whole line).

func (Style) Dim added in v0.5.0

func (s Style) Dim(text string) string

func (Style) Paint added in v0.5.0

func (s Style) Paint(code, text string) string

Paint wraps text in code and a reset when the style allows colour. An empty text stays empty: no bare escape pair.

func (Style) Warn added in v0.5.0

func (s Style) Warn(text string) string

type Styler added in v0.5.0

type Styler interface {
	Style() Style
}

Styler is a writer that carries its own Style. Styled makes one. It is the per-writer test seam next to the process-wide ForceTTY/ForceColor: a test renders one buffer as a terminal while another stays a pipe.

Jump to

Keyboard shortcuts

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