prompt

package
v1.116.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package prompt implements small terminal prompts (text input, yes/no confirmation, single- and multi-choice selection, and a generic picker) on top of github.com/charmbracelet/bubbletea. Select, MultiSelect and PickOne share one filterable, scrolling list; see PickOne.

Index

Constants

This section is empty.

Variables

View Source
var ErrAborted = errors.New("prompt aborted")

ErrAborted is returned when the user cancels a prompt, e.g. via Ctrl+C or Esc. It is not used by PickOne, which signals cancellation through its own ok return value instead; see PickOne's doc comment.

Functions

func Ask

func Ask(ctx context.Context, cfg Config) (string, error)

Ask displays a single-field terminal prompt and returns the entered value. It returns ErrAborted if the user cancels the prompt, and a non-nil error if ctx is canceled while the prompt is running.

func Confirm

func Confirm(ctx context.Context, cfg ConfirmConfig) (bool, error)

Confirm displays a yes/no terminal prompt and returns the user's choice. Pressing Enter without typing y/n returns cfg.Default. It returns ErrAborted if the user cancels the prompt, and a non-nil error if ctx is canceled while the prompt is running.

func MultiSelect

func MultiSelect(ctx context.Context, cfg MultiSelectConfig) ([]string, error)

MultiSelect displays a multiple-choice terminal prompt and returns the chosen options, in the order they were listed in cfg.Options. It returns ErrAborted if the user cancels the prompt or if no options are provided, and a non-nil error if ctx is canceled while the prompt is running.

func PickOne

func PickOne[T any](ctx context.Context, title string, options []Option[T]) (value T, ok bool, err error)

PickOne shows options in an interactive list, under the given title. Type to narrow the list down to the options whose Label contains what was typed, move with the arrow keys, PageUp/PageDown or Home/End, and press enter to pick the highlighted option. A list longer than a screenful scrolls a window over the options rather than rendering all of them, and a label wider than the terminal is truncated. Disabled options cannot be picked.

PickOne returns the Value of the picked option. ok is false if the user canceled instead of picking an option (via Esc or Ctrl+C); this is not reported as an error. Canceling ctx also cancels the picker; PickOne then returns a non-nil error.

func Required

func Required(s string) error

Required is a Validate function that rejects empty (or whitespace-only) input.

func Select

func Select(ctx context.Context, cfg SelectConfig) (string, error)

Select displays a single-choice terminal prompt and returns the chosen option. It is the string-list case of PickOne: the options scroll a window, typing narrows them down, and Esc clears the filter.

It returns ErrAborted if the user cancels the prompt or if no options are provided, and a non-nil error if ctx is canceled while the prompt is running.

Types

type Config

type Config struct {
	// Message is the question shown to the user.
	Message string

	// Help, if set, is displayed underneath the message.
	Help string

	// Default, if set, pre-fills the input. The user can edit or clear
	// it before submitting.
	Default string

	// EchoMode controls how typed input is displayed, e.g.
	// textinput.EchoPassword to mask the input. Defaults to
	// textinput.EchoNormal.
	EchoMode textinput.EchoMode

	// Validate, if set, must pass before the input is accepted on submit.
	// The prompt will keep re-displaying the error and accepting input
	// until it does.
	Validate func(string) error
}

Config describes a single-field text prompt.

type ConfirmConfig

type ConfirmConfig struct {
	// Message is the question shown to the user.
	Message string

	// Help, if set, is displayed underneath the message.
	Help string

	// Default is the choice highlighted initially, and the one used when
	// the user presses Enter without typing y/n explicitly.
	Default bool
}

ConfirmConfig describes a yes/no confirmation prompt.

type MultiSelectConfig

type MultiSelectConfig struct {
	// Message is the question shown to the user.
	Message string

	// Help, if set, is displayed underneath the message.
	Help string

	// Options are the choices presented to the user. Move between them
	// with the up/down arrow keys, PageUp/PageDown and Home/End, narrow
	// them down by typing a filter, toggle the highlighted one with
	// Space, and confirm the current set of selections with Enter.
	Options []string
}

MultiSelectConfig describes a multiple-choice prompt.

type Option

type Option[T any] struct {
	// Label is the option's main text, and the text the list is filtered
	// against.
	Label string

	// Description is a second line of text under Label. On a Disabled
	// option it is instead shown only when picking that option is
	// attempted, explaining why it can't be.
	Description string

	// Disabled options show in the list, but the user cannot pick them.
	// Description, if set, explains why.
	Disabled bool

	// Value is the value PickOne returns for this option.
	Value T
}

Option is one choice in a PickOne list.

type SelectConfig

type SelectConfig struct {
	// Message is the question shown to the user.
	Message string

	// Help, if set, is displayed underneath the message.
	Help string

	// Options are the choices presented to the user. Move between them
	// with the up/down arrow keys, PageUp/PageDown and Home/End, narrow
	// them down by typing a filter, and pick the highlighted one with
	// Enter.
	Options []string
}

SelectConfig describes a single-choice prompt.

Jump to

Keyboard shortcuts

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