Documentation
¶
Overview ¶
Package prompt asks a user to pick from a list or type a value, on the terminal or in plain text where there is no terminal to draw on. It knows nothing about what is being asked for: callers pass the question, the answers and whatever validation belongs to their own domain.
Index ¶
Constants ¶
const MultiSelectMaxVisible = 10
MultiSelectMaxVisible caps how many options are on screen at once, so a long list scrolls instead of pushing the prompt off the top. It is the height huh itself falls back to for dynamic options.
Variables ¶
var ErrAborted = errors.New("aborted")
ErrAborted is returned when the user dismisses a prompt. Dismissing a question is a decision rather than a failure, so callers usually stop cleanly on it instead of reporting an error.
Functions ¶
func NewInputField ¶
NewInputField builds the input. It is separate from Input so a test can render it without a terminal to run a form against, the same way NewMultiSelectField is.
func NewMultiSelectField ¶
NewMultiSelectField builds the multi-select. It is separate from MultiSelect so a test can render it without a terminal to run a form against.
Types ¶
type InputOpts ¶
type InputOpts struct {
Title string
// Description is the line under the title, for context the question needs.
Description string
// Default prefills the field.
Default string
// Validate refuses an answer and keeps the prompt open.
Validate func(string) error
// Describe replaces Description as the answer is typed, for showing what an
// answer will become before it is submitted. It is called with what has been
// typed so far and returns the line to draw, which is usually Description
// itself while there is nothing to add. Accessible mode draws no description
// at all, so what it returns is not shown there.
Describe func(value string) string
}
InputOpts is a question asking for a typed answer.
type Prompter ¶
type Prompter struct {
// contains filtered or unexported fields
}
Prompter asks the questions. It is deliberately thin: it owns the terminal and nothing else, so the code deciding what to ask can be tested against a scripted fake rather than against a terminal.
func New ¶
New builds a prompter over the real terminal, in plain-text mode when the environment asks for it.
func NewForStreams ¶
NewForStreams builds a prompter over the given streams, in plain-text mode. It is how a test drives the real implementation without a terminal.
func (*Prompter) Accessible ¶
Accessible reports whether prompts are drawn as plain numbered text.
func (*Prompter) MultiSelect ¶
MultiSelect asks the user to tick any number of options, with defaults already ticked. An empty submission is refused at the prompt, so the caller never has to send the user back to fix it.
func (*Prompter) Select ¶
func (p *Prompter) Select(title, description string, options []string, defaultValue string, validate func(string) error) (string, error)
Select asks the user to pick one of options, starting on defaultValue. Long lists can be narrowed by typing. description is drawn under the title, inside the same frame, and validate — when set — refuses an answer and keeps the prompt open rather than returning it.