Help us shape the future of Go! Take the 2026 Go Developer Survey

prompt

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func RunProgram(ctx context.Context, program *tea.Program) (tea.Model, error)

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 Candidate

type Candidate interface {
	Label() string
}

type Prompt

type Prompt struct {
	// contains filtered or unexported fields
}

func New

func New(in io.Reader, out io.Writer) Prompt

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) Confirm added in v0.6.0

func (p Prompt) Confirm(ctx context.Context, question string) (bool, error)

func (Prompt) Next

func (p Prompt) Next(ctx context.Context, what string) (string, error)

func (Prompt) Printf

func (p Prompt) Printf(format string, args ...any) (err error)

func (Prompt) RunOnTerminal

func (p Prompt) RunOnTerminal[M tea.Model](ctx context.Context, m M) (M, error)

func (Prompt) UsesTerminal

func (p Prompt) UsesTerminal() bool

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.

Jump to

Keyboard shortcuts

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