ui

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 19 Imported by: 0 modules, 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 ui renders devex's terminal output and prompts so every command looks and behaves the same. Data goes to stdout; status, warnings, prompts, spinners, and progress go to stderr. Styling turns off for a stream that isn't a terminal, and everywhere when NO_COLOR is set, TERM is dumb, or --no-color is passed. Prompts need a terminal on stdin and stderr; without one they fail and name the flag that answers them instead.

Index

Constants

View Source
const JSONAnnotation = "devex.ui/json"

JSONAnnotation marks a command that supports --output json. Validate rejects json for any command without it.

View Source
const YesFlag = "yes"

YesFlag is the global flag that answers confirmations.

Variables

View Source
var ErrAborted = errors.New("cancelled at the prompt")

ErrAborted is returned when the user leaves a prompt with Ctrl-C or Esc.

Functions

func AddFlags

func AddFlags(root *cobra.Command)

AddFlags registers the global output flags on the root command.

func IsTerminal

func IsTerminal(v any) bool

IsTerminal reports whether v is a file attached to a terminal.

func Validate

func Validate(cmd *cobra.Command) error

Validate rejects an unknown --output value, and json on a command without JSONAnnotation.

Types

type Format

type Format string

Format is the value of --output.

const (
	FormatText Format = "text"
	FormatJSON Format = "json"
)

type MissingFlagError

type MissingFlagError struct {
	Flag string
}

MissingFlagError is returned when a command needs an answer but cannot prompt for it.

func (*MissingFlagError) Error

func (e *MissingFlagError) Error() string

type Option

type Option struct {
	Label     string
	Value     string
	Suggested bool
}

Option is one choice in a Select prompt. Select points out a Suggested option with a marker.

type Options

type Options struct {
	Format  Format
	Quiet   bool
	Yes     bool
	NoColor bool
}

Options are the global flag values a UI honors.

type Progress

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

Progress tracks a known number of steps. Call Step as each finishes and Done at the end.

func (*Progress) Add

func (p *Progress) Add(n int, label string)

Add records n finished steps, described by label.

func (*Progress) Done

func (p *Progress) Done()

Done removes the live progress line, or off a terminal prints the final count.

func (*Progress) Step

func (p *Progress) Step(label string)

Step records one finished step, described by label.

type UI

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

UI writes one command's output. Its methods are safe for concurrent use.

func New

func New(cmd *cobra.Command) *UI

New builds a UI from cmd's streams and the global flags.

func NewWithStreams

func NewWithStreams(in io.Reader, out, err io.Writer, opts Options) *UI

NewWithStreams builds a UI on explicit streams, for callers and tests outside Cobra.

func (*UI) Accent

func (u *UI) Accent(text string) string

Accent styles text for stdout as a highlight.

func (*UI) Bad

func (u *UI) Bad(text string) string

Bad styles text for stdout as a failure.

func (*UI) Choose

func (u *UI) Choose(ctx context.Context, title string, options []Option, preselect, flag, flagValue string) (string, error)

Choose returns flagValue when it is set and names one of options, and otherwise prompts like Select. A flagValue outside options fails with the valid values listed.

func (*UI) Confirm

func (u *UI) Confirm(ctx context.Context, title string) (bool, error)

Confirm asks a yes-or-no question. --yes answers it. Without a terminal and without --yes it fails with a MissingFlagError naming --yes.

func (*UI) Dim

func (u *UI) Dim(text string) string

Dim styles text for stdout as secondary.

func (*UI) Err

func (u *UI) Err() io.Writer

Err is stderr, for diagnostics.

func (*UI) Good

func (u *UI) Good(text string) string

Good styles text for stdout as a success.

func (*UI) Heading

func (u *UI) Heading(text string) string

Heading styles text for stdout as a heading.

func (*UI) Interactive

func (u *UI) Interactive() bool

Interactive reports whether the UI can prompt.

func (*UI) JSON

func (u *UI) JSON() bool

JSON reports whether the command should write JSON instead of text.

func (*UI) Live

func (u *UI) Live() bool

Live reports whether stderr can redraw a line in place, for spinners and progress.

func (*UI) Out

func (u *UI) Out() io.Writer

Out is stdout, for data.

func (*UI) Printf

func (u *UI) Printf(format string, args ...any)

Printf writes data to stdout. When stdout and a live spinner or progress line share a terminal, it lifts the live line out of the way and redraws it underneath.

func (*UI) Progress

func (u *UI) Progress(title string, total int) *Progress

Progress starts a progress display of total steps. On a live terminal it redraws one line with a bar, a count, and an ETA. Otherwise Done prints the final count once, so logs stay short.

func (*UI) Select

func (u *UI) Select(ctx context.Context, title string, options []Option, preselect, flag string) (string, error)

Select asks the user to pick one option and returns its value. The option whose value is preselect starts highlighted. Without a terminal it fails with a MissingFlagError naming flag.

func (*UI) Spin

func (u *UI) Spin(ctx context.Context, title string, fn func(context.Context) error) error

Spin runs fn while a spinner labelled title turns on stderr. Without a live terminal it prints title once as a status line instead. Ctrl-C still reaches the process, which cancels ctx.

func (*UI) SpinStatus

func (u *UI) SpinStatus(ctx context.Context, title string, fn func(ctx context.Context, setTitle func(string)) error) error

SpinStatus is Spin with a label fn can change as it works, such as a task's status and elapsed time. Off a live terminal, label changes are dropped.

func (*UI) Status

func (u *UI) Status(format string, args ...any)

Status writes a progress message to stderr unless --quiet is set.

func (*UI) Success

func (u *UI) Success(format string, args ...any)

Success writes a completion message to stderr unless --quiet is set.

func (*UI) Table

func (u *UI) Table(headers []string, rows [][]string)

Table writes rows under headers to stdout: a bordered table on a styled terminal, and space-aligned columns otherwise, which scripts can split on whitespace.

func (*UI) Warn

func (u *UI) Warn(format string, args ...any)

Warn writes a warning to stderr, even under --quiet.

func (*UI) Warning

func (u *UI) Warning(text string) string

Warning styles text for stdout as a warning.

func (*UI) WriteJSON

func (u *UI) WriteJSON(v any) error

WriteJSON writes v to stdout as indented JSON.

func (*UI) Yes

func (u *UI) Yes() bool

Yes reports whether --yes answers confirmations.

Jump to

Keyboard shortcuts

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