Documentation
¶
Overview ¶
Package prompt asks a person on the terminal. A command passes its stdin for the answers and its stderr for the questions, so that stdout stays free for what the command outputs.
Index ¶
- Variables
- func RunProgram(ctx context.Context, program *tea.Program) (tea.Model, error)
- func Select[C Candidate](ctx context.Context, p Prompt, what string, candidates []C, ...) (selected C, err error)
- type Candidate
- type Prompt
- func (p Prompt) Ask(ctx context.Context, what, defaultAnswer string, validate func(string) error) (string, error)
- func (p Prompt) Confirm(ctx context.Context, question string) (bool, error)
- func (p Prompt) Next(ctx context.Context, what string) (string, error)
- func (p Prompt) Printf(format string, args ...any) (err error)
- func (p Prompt) RunOnTerminal[M tea.Model](ctx context.Context, m M) (M, error)
- func (p Prompt) UsesTerminal() bool
Constants ¶
This section is empty.
Variables ¶
var ErrEndOfInput = errors.New("the prompt reached the end of its input")
ErrEndOfInput is what Next returns once the input has ended, which a caller can accept as no answer at all where one is optional.
Functions ¶
func RunProgram ¶
RunProgram holds back the log while program runs, and sends program a logs.Warned after each record at WARN or above. Once program has ended, the log gets every record it held.
func Select ¶
func Select[C Candidate](ctx context.Context, p Prompt, what string, candidates []C, isDefault func(C) bool) (selected C, err error)
Select takes the only candidate without asking. Otherwise, it lets the person pick one from a list on the terminal, or, where in or out is no terminal, lists the candidates and asks until it gets the number of one. An empty answer takes the one isDefault reports, if any. An abandoned prompt is an error rather than no choice at all: a login that stored none leaves every later command without one.
Types ¶
type Prompt ¶
type Prompt struct {
// contains filtered or unexported fields
}
func New ¶
New reads in only once something asks, and then line by line for every later question, so several prompts of one command take their answers from the same input in turn.
func (Prompt) Ask ¶
func (p Prompt) Ask(ctx context.Context, what, defaultAnswer string, validate func(string) error) (string, error)
Ask takes an input that ends before an answer as an empty answer, so a script can give every answer as a flag and leave stdin empty. It fails only where validate refuses that.
func (Prompt) RunOnTerminal ¶
func (Prompt) UsesTerminal ¶
UsesTerminal is false once Next has read a line: its reader keeps reading the input, and would take the keys meant for a terminal UI.