Skip to content
Dropdowns

Dropdowns

Dropdown

Selects a value from a typed list. Use this for the common case where you need the selected value.

Selecting a city from a dropdown

DropdownIndex

Like Dropdown, but returns the selected index in []any. Use this when you want to keep value lookup separate.

Selecting an item and printing its index

DropdownKV

Selects from a map and returns both key and value. The map is sorted by key string representation before rendering.

Selecting a key and value from a map

DropdownLazy

Streams items progressively via iter.Seq2. Use this for large or slow data sources.

Items appearing progressively in a lazy dropdown

A slow producer keeps the prompt usable while items arrive:

A lazy dropdown fed by a slow producer

Confirm

Renders a yes/no dropdown and returns true for “yes”.

Confirm and Confirmf prompts

Confirmf

Formatted variant of Confirm.

ok := tui.Confirmf(ctx, "Delete %d files?", 12)
if !ok {
	return nil
}

WithDefault

Sets a match hint, not a pre-filled filter. The dropdown ranks items by similarity to the hint and returns early only when exactly one item matches it as a prefix; the typed filter stays empty. Also useful with WithOneReturn.

city, err := tui.Dropdown(ctx, "City", cities,
	tui.WithDefault("am"),
)

A default can also be a typed value. It is kept as is, never turned into a label: Dropdown[T], DropdownLazy[T] and DropdownIndex take the item, DropdownKV takes the key (a value of the map is not looked up). It must be assignable to the item type, without conversions, and is compared with ==, or with reflect.DeepEqual for slices and maps; pointers compare by identity. It must identify exactly one item: a value that is missing, repeated, a non-nil function or NaN fails with ErrInvalidOption. An untyped nil matches a nil item. Items with identical labels stay distinct.

type Server struct{ Name, Region string }

srv, err := tui.Dropdown(ctx, "Server", servers,
	tui.WithDefault(Server{"api", "eu"}),
)
key, value, err := tui.DropdownKV(ctx, "Port", ports, tui.WithDefault("https"))

With a terminal the typed item is the first row and Enter confirms it. A string that is not an item keeps working as the hint above.

Without a terminal

When the output is not a terminal, every dropdown answers from WithDefault without reading input or printing anything, and fails otherwise:

  • no default: an error wrapping ErrNoTTY; DropdownLazy does not start its producer;
  • a missing, ambiguous or wrongly typed default: ErrInvalidOption;
  • a string is first an exact item, so "a" selects "a" even when "ab" exists; otherwise it is a word-prefix hint that must match exactly one label;
  • with WithOneReturn, a single item is returned without a default, ahead of any hint.

DropdownLazy consumes the whole sequence first, so a later duplicate or a late producer error is not hidden; an empty sequence is ErrEmptyLazyResult.

Confirm keeps its bool result and cannot show errors: WithDefault(true) or "Yes" returns true, WithDefault(false) or "No" false, and no default, an invalid default or any error returns false. Call Dropdown with "Yes" and "No" to see ErrNoTTY.

WithOneReturn

Returns immediately if exactly one item is available.

city, err := tui.Dropdown(ctx, "City", []string{"Amsterdam"},
	tui.WithOneReturn(),
)

WithHideAnswer

Hides the final answer line after selection.

city, err := tui.Dropdown(ctx, "City", cities,
	tui.WithHideAnswer(),
)

To hide answers by default, set HideAnswer on the theme component, such as Input, Number, Dropdown, Multichoice, Confirm or FilePicker. An option cannot show an answer that the theme hides, and passwords never keep one.

WithTemplate

Sets main and active-details templates in one call. Use this to choose the fields an item shows.

A dropdown with a custom focus mark and template

Presentation

A dropdown draws through the theme. Set the slots once with SetDefaultTheme; the same slots serve Confirm and FilePicker unless a family slot overrides them.

SlotPayloadDraws
Dropdown.Label.Labelthe prompt label
Dropdown.Active.Value, .Details, .Indexthe focused row
Dropdown.Inactive.Value, .Indexthe other rows
Dropdown.Hinta list with .Left, .Rightthe key hints, led by the count of hidden items
Dropdown.Answer.Label, .Valuethe confirmation line
th := tui.DefaultTheme()
th.Dropdown.Label = `{{ accent "Select:" }} {{ bold .Label }}`
th.Dropdown.Active = `{{ success .Value }}`
th.Dropdown.Hint = `{{ range . }}{{ .Left }}={{ .Right }} {{ end }}`
th.Dropdown.Answer = `{{ success "selected:" }} {{ .Value }}`
if err := tui.SetDefaultTheme(th); err != nil {
	return err
}
city, err := tui.Dropdown(ctx, "City", cities)

.Value is the item as projected by WithTemplate, so a struct item needs a projection to show its fields.

Default appearance

A dropdown draws through the installed theme: the Dropdown slots, which inherit from Prompt and Choice. See Themes to change it for every widget.

Label resolution for struct items

Dropdown resolves labels in this order:

  1. Explicit label tags (header:"label", tui:"label", or label:"true")
  2. Heuristic field names (Label, Name, Title, Description, DisplayName, FullName, Summary, Text, Subject)
  3. fmt.Stringer
  4. fmt.Sprint
Last updated on