Skip to content
Shared Options

Shared Options

go-tui uses opt functions. Every option supports a fixed set of widgets, and widgets reject options they do not support with ErrWrongWidget, so a misplaced option fails instead of silently doing nothing.

OptionSupported by
WithDefault, WithHideAnswerInput, Password, Number, dropdowns, multichoice, Confirm, FilePicker
WithNonEmpty, WithRegex, WithValidationInput, Password
WithTemplatedropdowns, multichoice, Confirm, FilePicker
WithOneReturndropdowns, Confirm, FilePicker
WithStartDir, WithExtensions, WithIgnoreUp, WithIgnoreDirs, WithShowHiddenFilePicker
WithPrefixf, WithKeepSpinners.Add
WithFormatRate, WithWorkersprogress bars
WithColumn*, WithIncludeColumns, WithSkipColumns, WithMaxWidth, WithMultilineCells, WithFloat64*Table, TableAuto, TableIter, Facts

Tables take their writer as an argument, so they accept none of the shared options below. Column options only shape auto-generated templates and have no effect when you pass an explicit row template.

Defaults without a terminal

WithDefault(d any) is also the batch answer. Every prompt follows one rule: when the output is not a terminal it returns its explicit default, otherwise an error wrapping ErrNoTTY. A pipe, a redirected file or a bytes.Buffer is not a terminal, however the input is wrapped. Neither is any output when CI is set to anything but 0 or false, since nobody answers prompts or watches redraws in a CI job, even on a pseudo-terminal. Spinners and progress bars likewise draw nothing there. The answer reads no input and prints no prompt, answer or escape codes. An invalid, missing or ambiguous default is ErrInvalidOption, with the validation error still reachable through errors.Is. There is no extra option to enable this. See Input, Number, dropdowns, multichoice and FilePicker; Confirm returns false where the others return an error.

To verify it, redirect the prompt’s output away from the terminal, for example go run ./examples/prompt/number/main.go 2>/dev/null (prompts draw on stderr), or pipe stderr, not stdout, through cat with 2>&1 | cat: a prompt with WithDefault prints nothing and returns the default, and one without it fails with no tty.

Context

Interactive and long-running widgets (Input, Password, dropdowns, multichoice, Confirm, FilePicker, NewSpinners and progress bars) take a context.Context as the first argument. Cancelling it, for example on Ctrl-C or a parent shutdown, stops the widget and its workers. For an upper bound on interaction time, derive the context with context.WithTimeout and cancel it when done.

ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()

value, err := tui.Input(ctx, "Name")

Input validation and feedback

WithNonEmpty, WithRegex, and WithValidation check confirmed input in option order and display the first failure below the editable answer. The Feedback slot of the theme customizes that row. These options support both Input and Password; see Input and Password for examples and the validator context contract.

Stderr

Returns go-tui’s package-default append-only terminal writer.

slog.SetDefault(slog.New(
	tint.NewHandler(tui.Stderr(), nil),
))

Use this when logs or other external terminal output should coexist with default go-tui widgets on the same TTY.

  • On a TTY, Stderr() routes writes through the shared terminal arbiter so log lines and widgets are redrawn in a stable order.
  • On a non-TTY, Stderr() returns the configured raw writer directly, so daemon and batch logging stay simple.
  • Only cooperating writers routed through tui.Stderr() participate in that coordination. Direct writes to raw os.Stderr bypass it.

WithFn

Registers a global template helper function for operations started afterwards.

tui.WithFn("upper", strings.ToUpper)

Then use it in templates: {{ upper .Name }}. It panics on an invalid name or signature, or on a built-in helper’s name. Theme.Funcs with SetDefaultTheme returns an error instead; see Themes.

All predefined helper functions are documented on Template functions.

Common errors

  • ErrNoTTY: a prompt without an explicit WithDefault ran where the output is not a terminal.
  • ErrNoItems: selection widget called with empty items.
  • ErrInvalidOption: nil multichoice lazy sequence, nil input validator, invalid regex pattern, or blank regex feedback message.
  • ErrNoSpace: insufficient terminal space, including fewer than two rows when input rejection feedback is needed.
  • ErrEmptyLazyResult: a completed lazy selection source contains no items.
  • ErrInvalidState: invalid option value or configuration.
  • ErrUnsupportedPlatform: platform-specific feature unavailable.
  • ErrWrongWidget: option is not supported by the widget it was passed to.
  • ErrSpinnerStopped: a child was added to a spinner that has already closed or failed.
Last updated on