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
- Variables
- func Keys(keys ...string) io.Reader
- func Theme() *huh.Theme
- type Progress
- type UI
- func (u *UI) Bad(format string, a ...any)
- func (u *UI) Box(lines ...string)
- func (u *UI) Code(s string) string
- func (u *UI) Confirm(question string, def bool) (bool, error)
- func (u *UI) Detail(format string, a ...any)
- func (u *UI) Dimmed(s string) string
- func (u *UI) Hint(lines ...string)
- func (u *UI) Info(format string, a ...any)
- func (u *UI) Input(title, def, placeholder string, validate func(string) error) (string, error)
- func (u *UI) OK(format string, a ...any)
- func (u *UI) Password(title, placeholder string) (string, error)
- func (u *UI) Report() *UI
- func (u *UI) Run(groups ...*huh.Group) error
- func (u *UI) Section(title, sub string)
- func (u *UI) Select(title string, options []string) (string, error)
- func (u *UI) Spin(ctx context.Context, title string, fn func(context.Context) error) error
- func (u *UI) SpinProgress(ctx context.Context, title string, progress func() (Progress, bool), ...) error
- func (u *UI) Table(headers []string, rows [][]string)
- func (u *UI) Warn(format string, a ...any)
Constants ¶
const ( Enter = "\r" Down = "\x1b[B" Up = "\x1b[A" Space = " " Tab = "\t" )
Key sequences for Keys.
Variables ¶
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.
var ErrAborted = errors.New("cancelled")
ErrAborted means the user cancelled a prompt (ctrl-c or esc).
var PlainProgressEvery = 30 * time.Second
PlainProgressEvery is how often plain mode prints a step's progress.
Functions ¶
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.
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 ¶
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 NewPlainStreams ¶ added in v0.3.0
NewPlainStreams returns a plain UI with separate stdout and stderr, for tests that check what goes where.
func NewTerminal ¶
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 ¶
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) Report ¶ added in v0.3.0
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) Spin ¶
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.