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
- Variables
- func AddFlags(root *cobra.Command)
- func IsTerminal(v any) bool
- func Validate(cmd *cobra.Command) error
- type Format
- type MissingFlagError
- type Option
- type Options
- type Progress
- type UI
- func (u *UI) Accent(text string) string
- func (u *UI) Bad(text string) string
- func (u *UI) Choose(ctx context.Context, title string, options []Option, ...) (string, error)
- func (u *UI) Confirm(ctx context.Context, title string) (bool, error)
- func (u *UI) Dim(text string) string
- func (u *UI) Err() io.Writer
- func (u *UI) Good(text string) string
- func (u *UI) Heading(text string) string
- func (u *UI) Interactive() bool
- func (u *UI) JSON() bool
- func (u *UI) Live() bool
- func (u *UI) Out() io.Writer
- func (u *UI) Printf(format string, args ...any)
- func (u *UI) Progress(title string, total int) *Progress
- func (u *UI) Select(ctx context.Context, title string, options []Option, preselect, flag string) (string, error)
- func (u *UI) Spin(ctx context.Context, title string, fn func(context.Context) error) error
- func (u *UI) SpinStatus(ctx context.Context, title string, ...) error
- func (u *UI) Status(format string, args ...any)
- func (u *UI) Success(format string, args ...any)
- func (u *UI) Table(headers []string, rows [][]string)
- func (u *UI) Warn(format string, args ...any)
- func (u *UI) Warning(text string) string
- func (u *UI) WriteJSON(v any) error
- func (u *UI) Yes() bool
Constants ¶
const JSONAnnotation = "devex.ui/json"
JSONAnnotation marks a command that supports --output json. Validate rejects json for any command without it.
const YesFlag = "yes"
YesFlag is the global flag that answers confirmations.
Variables ¶
var ErrAborted = errors.New("cancelled at the prompt")
ErrAborted is returned when the user leaves a prompt with Ctrl-C or Esc.
Functions ¶
func IsTerminal ¶
IsTerminal reports whether v is a file attached to a terminal.
Types ¶
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 ¶
Option is one choice in a Select prompt. Select points out a Suggested option with a marker.
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.
type UI ¶
type UI struct {
// contains filtered or unexported fields
}
UI writes one command's output. Its methods are safe for concurrent use.
func NewWithStreams ¶
NewWithStreams builds a UI on explicit streams, for callers and tests outside Cobra.
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 ¶
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) Interactive ¶
Interactive reports whether the UI can prompt.
func (*UI) Live ¶
Live reports whether stderr can redraw a line in place, for spinners and progress.
func (*UI) Printf ¶
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 ¶
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 ¶
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) Table ¶
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.