ui

package
v1.169.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package ui holds the terminal capability kernel that doctl's output components are built on. Env captures the current process's capabilities - terminals, color, animation, width - so components take them as an argument rather than a package global. Out carries data and Err carries chrome.

Index

Constants

View Source
const (
	GlyphSuccess   = "✓"
	GlyphFailure   = "✗"
	GlyphWarning   = "!"
	GlyphInfo      = "i"
	GlyphCancelled = "·"
	GlyphPending   = "⟳"
	GlyphBullet    = "•"
	GlyphArrow     = "❯"
	GlyphHint      = "→"
	GlyphEllipsis  = "…"
	GlyphSeparator = "·"
	GlyphNone      = "—"

	// GlyphAsterisk marks a required field on a prompt. It is decoration rather
	// than state, which is why it is not part of Glyphs.
	GlyphAsterisk = "✱"
)

Glyph characters are the one definition of doctl's symbol vocabulary, in the same way that style.go owns the palette.

View Source
const (
	// ColorError is the red a failure is named in.
	ColorError lipgloss.Color = "#ff6188"
	// ColorSuccess is the green of a resource that reached its desired state.
	ColorSuccess lipgloss.Color = "#04b575"
	// ColorWarning is the yellow that reads as attention without alarm.
	ColorWarning lipgloss.Color = "#ffd866"
	// ColorInfo is the blue that names a resource.
	ColorInfo lipgloss.Color = "#2ea0f9"
	// ColorMuted is xterm 241, dim enough to recede behind a value.
	ColorMuted lipgloss.Color = "241"
)

The semantic palette, and the one definition of doctl's colors. They are the colors doctl's interactive surfaces already use, so that a table, a prompt and an error all name the same state the same way. termenv downsamples them for terminals that cannot show truecolor.

View Source
const SpinnerInterval = 120 * time.Millisecond

SpinnerInterval is how often an animated spinner repaints.

View Source
const StageHeartbeat = time.Minute

StageHeartbeat is how often a plain stream repeats an unchanged stage.

Variables

This section is empty.

Functions

func IsCI

func IsCI() bool

IsCI reports whether the process appears to be running in a CI environment.

Types

type Env

type Env struct {
	// Out is the destination for data: tables, JSON, anything pipeable.
	Out io.Writer

	// Err is the destination for chrome: spinners, prompts, and diagnostics.
	Err io.Writer

	// Style reports whether ANSI styling may be written to Out.
	Style bool

	// ErrStyle reports whether ANSI styling may be written to Err.
	ErrStyle bool

	// Anim reports whether animation may be written to Err. It is stricter than
	// ErrStyle, additionally requiring an interactive, non-CI session.
	Anim bool

	// ASCII reports whether output must avoid non-ASCII glyphs.
	ASCII bool

	// Width is the session width, for chrome on Err, or 0 when undetermined.
	Width int

	// DataWidth is the width available to data on Out, or 0 when data must be
	// left unconstrained. It is tracked separately from Width because a
	// terminal on Err says nothing about Out: reflowing to a width Out does not
	// have would corrupt a pipe.
	DataWidth int

	// DataTTY reports whether Out is an interactive terminal outside CI.
	// Layout that is only meaningful on a screen someone is watching live -
	// cards, boxed tables, the records fallback - is gated on it rather than
	// on Style.
	DataTTY bool

	// ErrTTY reports whether Err is an interactive terminal outside CI, which
	// gates glyphs and other screen-only chrome.
	ErrTTY bool

	// Machine reports whether machine-readable output was asked for. When true,
	// Style, ErrStyle, and Anim are all false.
	Machine bool

	// Mask reports whether sensitive values (secrets, tokens, and similar)
	// should be hidden rather than printed in full. It is independent of
	// Machine: --output json still masks, since piping to a script is not
	// consent to leak a secret into a log. It is true unless the invocation
	// is running in CI or --show was passed.
	Mask bool
	// contains filtered or unexported fields
}

Env describes the capabilities available to a single command invocation.

func Detect

func Detect(out, err io.Writer, opts ...Option) Env

Detect resolves the capabilities of out and err.

func Plain

func Plain(out, err io.Writer) Env

Plain returns an Env with every capability disabled, for deterministic output.

func (Env) DataProfile

func (e Env) DataProfile() termenv.Profile

DataProfile returns the color profile for Out, where a global must point.

func (Env) ErrRenderer

func (e Env) ErrRenderer() *lipgloss.Renderer

ErrRenderer returns the lipgloss renderer bound to Err.

func (Env) ErrWriter

func (e Env) ErrWriter() io.Writer

ErrWriter returns Err, defaulting to os.Stderr when unset.

func (Env) Glyphs

func (e Env) Glyphs() Glyphs

Glyphs returns the symbol vocabulary appropriate to the environment.

func (Env) NewErrStyle

func (e Env) NewErrStyle() lipgloss.Style

NewErrStyle returns an empty style bound to Err's renderer.

func (Env) NewSpinner

func (e Env) NewSpinner(message string, opts ...SpinnerOption) *Spinner

NewSpinner returns a Spinner reporting message. The caller must Start it.

func (Env) NewStyle

func (e Env) NewStyle() lipgloss.Style

NewStyle returns an empty style bound to Out's renderer.

func (Env) Profile

func (e Env) Profile() termenv.Profile

Profile returns the color profile resolved for Err.

func (Env) Renderer

func (e Env) Renderer() *lipgloss.Renderer

Renderer returns the lipgloss renderer bound to Out.

func (Env) Sprint

func (e Env) Sprint(style lipgloss.Style, s string) string

Sprint renders s with style when styling is permitted on Out, rebinding the style to this Env's renderer.

func (Env) SprintErr

func (e Env) SprintErr(style lipgloss.Style, s string) string

SprintErr renders s with style when styling is permitted on Err.

func (Env) SprintTone

func (e Env) SprintTone(tone Tone, s string) string

SprintTone renders s in the color for tone when styling is permitted on Out, and returns s unchanged otherwise.

func (Env) Writer

func (e Env) Writer() io.Writer

Writer returns Out, defaulting to os.Stdout when unset.

type Glyphs

type Glyphs struct {
	Success   string
	Failure   string
	Warning   string
	Info      string
	Cancelled string
	Pending   string
	Bullet    string
	Arrow     string
	Ellipsis  string

	// Hint leads the suggestion that follows a diagnostic, pointing at what to
	// do about it. It is a different arrow from Arrow, which prefixes prompts.
	Hint string

	// Separator and None carry no state, but both need an ASCII fallback.
	Separator string
	None      string

	// Spinner holds the animation frames, which must all be the same display
	// width or the line jitters as it cycles.
	Spinner []string
}

Glyphs is the symbol vocabulary used to convey state. It exists so that meaning survives when color does not, as when output is piped.

type Option

type Option func(*config)

Option customises capability detection.

func WithASCII

func WithASCII(v bool) Option

WithASCII forces the ASCII fallback on or off, overriding DOCTL_ASCII.

func WithAnimation

func WithAnimation(v bool) Option

WithAnimation forces animation on or off. Machine output still wins.

func WithInteractive

func WithInteractive(v bool) Option

WithInteractive records whether doctl's --interactive flag permitted it.

func WithMachineOutput

func WithMachineOutput(v bool) Option

WithMachineOutput suppresses all styling and animation, overriding options.

func WithProfile

func WithProfile(p termenv.Profile) Option

WithProfile forces the color profile of both streams; termenv.Ascii is off.

func WithShow

func WithShow(v bool) Option

WithShow records whether doctl's --show flag was passed, revealing values Mask would otherwise hide.

func WithWidth

func WithWidth(v int) Option

WithWidth overrides the detected width. A width of 0 leaves it unconstrained.

type Spinner

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

Spinner reports the progress of a long-running operation. It renders to Err so that data on Out stays parseable: an animated frame on a terminal, one plain line per stage change elsewhere. Every method is a no-op after Stop.

func (*Spinner) Elapsed

func (s *Spinner) Elapsed() time.Duration

Elapsed reports how long the operation has run, or ran before it stopped.

func (*Spinner) Fail

func (s *Spinner) Fail(format string, a ...any)

Fail stops the spinner and reports that the operation did not complete.

func (*Spinner) Message

func (s *Spinner) Message(message string)

Message replaces the text shown alongside the spinner.

func (*Spinner) Note

func (s *Spinner) Note(format string, a ...any)

Note records a stage that has passed, leaving it on screen as work moves on.

func (*Spinner) Start

func (s *Spinner) Start()

Start begins reporting progress. It animates on an interactive terminal, and otherwise prints the opening line and leaves later stages to Message.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop halts the spinner without reporting an outcome, clearing any frame it left on screen. It is safe to call more than once, so a defer may guard it.

func (*Spinner) Succeed

func (s *Spinner) Succeed(format string, a ...any)

Succeed stops the spinner and reports that the operation completed.

type SpinnerOption

type SpinnerOption func(*Spinner)

SpinnerOption configures a Spinner.

func WithHeading

func WithHeading(format string, a ...any) SpinnerOption

WithHeading titles the wait, on its own line above the progress line.

type Style

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

Style is a presentation helper bound to an Env, for chrome written to Err.

func NewStyle

func NewStyle(env Env) Style

NewStyle wraps env for semantic chrome rendering on Err.

func (Style) Bold

func (s Style) Bold(text string) string

Bold emphasizes flag names and command paths when Err styling is on.

func (Style) Dim

func (s Style) Dim(text string) string

Dim renders secondary hint text.

func (Style) ErrorLabel

func (s Style) ErrorLabel() string

ErrorLabel returns the failure label, led by its glyph on a terminal.

func (Style) Hint

func (s Style) Hint(text string) string

Hint renders the suggestion that follows a diagnostic, led by its arrow.

func (Style) PaintCommand

func (s Style) PaintCommand(line string) string

PaintCommand dims the lead-in of a suggestion and bolds the command path, so that the part worth copying stands out from the sentence around it.

func (Style) SuccessLine

func (s Style) SuccessLine(msg string) string

SuccessLine reports that a command finished, glyph and message painted together so that completion reads as one mark rather than a labelled diagnostic. The glyph leads only on a terminal, as ErrorLabel's does.

type Tone

type Tone int

Tone is the meaning a value carries, separate from how it is painted.

const (
	// ToneNone carries no meaning and is rendered unstyled.
	ToneNone Tone = iota
	// ToneSuccess is a resource that has reached its desired state.
	ToneSuccess
	// TonePending is a resource still moving towards it, or worth attention.
	TonePending
	// ToneError is a resource that failed or is unusable.
	ToneError
	// ToneMuted is a resource that is intentionally inert: off, deleted, unknown.
	ToneMuted
	// ToneIdentifier is the value that names the resource a row is about.
	// ToneFor never returns it: a name belongs to no vocabulary, so only a
	// caller that knows which column identifies the resource can ask for it.
	ToneIdentifier
)

func ToneFor

func ToneFor(value string) (Tone, bool)

ToneFor classifies a state value, reporting false when it is not recognized so that callers leave unfamiliar text alone. The whole value is tried first, then each of its words: "SCENARIO_SET_STATUS_READY" is a success on its last.

Jump to

Keyboard shortcuts

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