Help us shape the future of Go! Take the 2026 Go Developer Survey

ui

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package ui is limabean's terminal interface: status lines, spinners, boxes and tables in the README palette, and huh forms for prompts.

When output isn't a terminal, or plain mode is on, everything is printed as plain text and nothing prompts: prompts return their defaults, and the caller decides whether a missing answer is an error.

Data (tables, JSON) goes to Out, stdout; everything else (status lines, hints, spinners, forms) goes to Err, stderr, so a pipe gets only data.

Index

Constants

View Source
const (
	Enter = "\r"
	Down  = "\x1b[B"
	Up    = "\x1b[A"
	Space = " "
	Tab   = "\t"
)

Key sequences for Keys.

Variables

View Source
var (
	Leaf  = lipgloss.Color("#8fd16a")
	Leaf2 = lipgloss.Color("#b9e69a")
	Dim   = lipgloss.Color("#6f8a77")
	Red   = lipgloss.Color("#ef7d7d")
	Amber = lipgloss.Color("#f0c36a")
	Ink   = lipgloss.Color("#0d1912")
)

The palette from the README graphics.

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

ErrAborted means the user cancelled a prompt (ctrl-c or esc).

View Source
var PlainProgressEvery = 30 * time.Second

PlainProgressEvery is how often plain mode prints a step's progress.

Functions

func Keys

func Keys(keys ...string) io.Reader

Keys returns a reader that delivers one key per read, a little apart, the way a terminal does. Forms read a whole chunk as pasted text, so tests that drive a form (NewTerminal) type into it with Keys. Strings are split into single characters; Enter, Down and the like are sent whole.

func Theme

func Theme() *huh.Theme

Theme is the huh theme in the README palette.

Types

type Progress added in v0.3.2

type Progress struct {
	Label       string  // what's under way, e.g. "Downloading ubuntu.img"
	Done, Total int64   // bytes so far and in all; Total is 0 when unknown
	Rate        float64 // bytes a second; 0 until known
}

Progress is how far a long step has got, such as a download.

func (Progress) Fraction added in v0.3.2

func (p Progress) Fraction() float64

Fraction is how much is done, from 0 to 1, or -1 when Total isn't known.

func (Progress) String added in v0.3.2

func (p Progress) String() string

String is the progress in words: "Downloading x: 412 MiB of 902 MiB, 1.1 MiB/s, about 7m left".

type UI

type UI struct {
	Out, Err io.Writer
	In       io.Reader
	// Interactive: output is a terminal and plain mode is off.
	Interactive bool
	// Prompting: Interactive, and input is a terminal too.
	Prompting bool
	// NoColor keeps the layout but drops color (NO_COLOR, --no-color).
	NoColor bool
	// Quiet drops everything but errors from Err; Verbose adds detail.
	Quiet, Verbose bool
	// FormTimeout, when set, fails a form that isn't finished in time
	// (tests use it so a wrong key sequence fails instead of hanging).
	FormTimeout time.Duration
	// contains filtered or unexported fields
}

UI writes to a terminal, or to plain output.

func New

func New(plain, noColor bool) *UI

New returns a UI for stdout, stderr and stdin. plain forces plain output, as does TERM=dumb; NO_COLOR (or noColor) only drops color.

func NewPlain

func NewPlain(out io.Writer) *UI

NewPlain returns a plain UI writing to out, for tests.

func NewPlainStreams added in v0.3.0

func NewPlainStreams(out, err io.Writer) *UI

NewPlainStreams returns a plain UI with separate stdout and stderr, for tests that check what goes where.

func NewTerminal

func NewTerminal(in io.Reader, out io.Writer) *UI

NewTerminal returns an interactive UI reading keys from in and writing to out, as if both were a terminal; for driving forms in tests (see Keys).

func NewTerminalForms

func NewTerminalForms(out io.Writer, forms ...io.Reader) *UI

NewTerminalForms is NewTerminal with one input per form, in the order the forms appear. A command that shows several forms in a row needs this: each form's reader may read ahead, and keys it took are lost when it closes.

func (*UI) Bad

func (u *UI) Bad(format string, a ...any)

func (*UI) Box

func (u *UI) Box(lines ...string)

Box prints lines in a rounded box (plain: just the lines).

func (*UI) Code

func (u *UI) Code(s string) string

Code highlights an inline name or command.

func (*UI) Confirm

func (u *UI) Confirm(question string, def bool) (bool, error)

Confirm asks a yes/no question; without a terminal it returns def.

func (*UI) Detail added in v0.3.0

func (u *UI) Detail(format string, a ...any)

Detail prints a line only with --verbose.

func (*UI) Dimmed

func (u *UI) Dimmed(s string) string

Dimmed renders secondary text.

func (*UI) Hint

func (u *UI) Hint(lines ...string)

Hint prints indented, dimmed lines under a status line.

func (*UI) Info

func (u *UI) Info(format string, a ...any)

Info prints a progress line.

func (*UI) Input

func (u *UI) Input(title, def, placeholder string, validate func(string) error) (string, error)

Input asks for a line of text; without a terminal it returns def.

func (*UI) OK

func (u *UI) OK(format string, a ...any)

OK, Bad and Warn print a status line marked ✓, ✗ or !.

func (*UI) Password

func (u *UI) Password(title, placeholder string) (string, error)

Password asks for a value without echoing it.

func (*UI) Report added in v0.3.0

func (u *UI) Report() *UI

Report returns a UI whose status lines go to stdout: for commands whose human-readable output is the result itself (ps, doctor, status).

func (*UI) Run

func (u *UI) Run(groups ...*huh.Group) error

Run shows a huh form (only call it when Prompting).

func (*UI) Section

func (u *UI) Section(title, sub string)

Section starts a group of status lines, with an optional subtitle.

func (*UI) Select

func (u *UI) Select(title string, options []string) (string, error)

Select asks for one of options.

func (*UI) Spin

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

Spin runs fn behind a spinner titled title. In plain mode it prints the title and runs fn.

func (*UI) SpinProgress added in v0.3.2

func (u *UI) SpinProgress(ctx context.Context, title string, progress func() (Progress, bool), fn func(context.Context) error) error

SpinProgress is Spin with a progress line under the spinner, read from progress while fn runs; progress returns false when there's nothing to show. In plain mode the progress is printed now and then instead.

func (*UI) Table

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

Table prints rows under headers: a bordered table in a terminal, aligned columns otherwise.

func (*UI) Warn

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

Jump to

Keyboard shortcuts

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