prompt

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package prompt asks one-shot questions: selects, confirms, inputs.

The one-shot engine is deliberately plain: print the bullet question, read one line, parse the answer. No cursor tracking, no re-renders, no second render pass — so output cannot duplicate on any terminal, legacy consoles included. Numbered lists replace arrow-key menus; masked passwords use the OS no-echo read. Ctrl+C cancels through ctx.

Every prompt takes a Preset (the already-parsed flag value). A valid preset returns immediately without touching the terminal, so flags always win. Without a preset the terminal must be interactive; otherwise the prompt fails naming the required flag instead of hanging on a pipe.

Editor is the opt-in exception for repl-style input: liner-backed line editing with history, ctrl+r search, and tab completion. It lends the console to each Line call and hands it back, so one-shot prompts and render output behave normally in between; on pipes and dumb terminals Line degrades to a plain read.

Render pausing is the caller's job: pause live drawing before prompting and resume after, or the menu and the tree garble each other. See app.Context PauseDrawing and ResumeDrawing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Confirm

func Confirm(ctx context.Context, io IO, opts ConfirmOptions) (bool, error)

Confirm asks yes/no.

func Input

func Input(ctx context.Context, io IO, opts InputOptions) (string, error)

Input reads one line, echoing input.

func MultiSelect

func MultiSelect(ctx context.Context, io IO, opts MultiSelectOptions) ([]string, error)

MultiSelect picks options by comma-separated numbers.

func Password

func Password(ctx context.Context, io IO, opts PasswordOptions) (string, error)

Password reads one line without echoing.

func Select

func Select(ctx context.Context, io IO, opts SelectOptions) (string, error)

Select picks one option from a numbered list.

Types

type Completer added in v1.2.1

type Completer func(line string) []string

Completer returns completion candidates for the text left of the cursor.

type ConfirmOptions

type ConfirmOptions struct {
	Message string
	Default bool
	// Preset returns unprompted when non-nil.
	Preset *bool
	Flag   string
}

ConfirmOptions tunes a Confirm call.

type Editor added in v1.2.1

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

Editor is an opt-in line editor for repl-style input: cursor and word editing, kill/yank, history, ctrl+r reverse search, tab completion.

It borrows the process console for the duration of each Line call and hands it back before returning, so one-shot prompts and render output keep behaving normally between calls. Interactive editing always runs on os.Stdin/os.Stdout; IO is used by the plain fallback that Line degrades to on pipes, redirected output, and dumb terminals. Line returns surrounding whitespace as typed, unlike the trimmed one-shot prompts. Editor is not safe for concurrent use.

func NewEditor added in v1.2.1

func NewEditor(stdio IO) *Editor

NewEditor builds an editor over stdio. Pass StdIO in real apps.

func (*Editor) AppendHistory added in v1.2.1

func (e *Editor) AppendHistory(item string)

AppendHistory records an entry, collapsing immediate repeats and capping retained entries.

func (*Editor) ClearHistory added in v1.2.1

func (e *Editor) ClearHistory()

ClearHistory forgets all entries.

func (*Editor) Line added in v1.2.1

func (e *Editor) Line(ctx context.Context, opts LineOptions) (string, error)

Line reads one edited line, newline excluded. Ctrl+C returns context.Canceled so repls can drop the line and continue; Ctrl+D and end of piped input return io.EOF. Each non-empty line joins history.

func (*Editor) ReadHistory added in v1.2.1

func (e *Editor) ReadHistory(r io.Reader) (int, error)

ReadHistory loads newline-separated history from r, returning the number of lines read. Invalid utf-8 lines are rejected.

func (*Editor) SetBeep added in v1.2.1

func (e *Editor) SetBeep(enabled bool)

SetBeep toggles the terminal bell (on by default).

func (*Editor) SetCompleter added in v1.2.1

func (e *Editor) SetCompleter(f Completer)

SetCompleter sets tab completion candidates.

func (*Editor) SetMultiLine added in v1.2.1

func (e *Editor) SetMultiLine(enabled bool)

SetMultiLine wraps long lines across rows instead of scrolling them.

func (*Editor) SetTabCompletionStyle added in v1.2.1

func (e *Editor) SetTabCompletionStyle(style TabStyle)

SetTabCompletionStyle selects cycling or bash-style listed candidates.

func (*Editor) SetWordCompleter added in v1.2.1

func (e *Editor) SetWordCompleter(f WordCompleter)

SetWordCompleter sets word-aware tab completion.

func (*Editor) WriteHistory added in v1.2.1

func (e *Editor) WriteHistory(w io.Writer) (int, error)

WriteHistory writes newline-separated history to w, returning the number of entries written.

type FileReader added in v1.1.1

type FileReader interface {
	io.Reader
	Fd() uintptr
}

FileReader is a readable file descriptor: consoles, pipes, files.

type FileWriter added in v1.1.1

type FileWriter interface {
	io.Writer
	Fd() uintptr
}

FileWriter is a writable file descriptor.

type IO

type IO struct {
	In  FileReader
	Out FileWriter
	Err io.Writer
	// NoColor strips prompt styling (dumb terminals, --no-color).
	// The bullet glyph itself always renders.
	NoColor bool
}

IO carries stdio for prompts. Use StdIO in real apps; tests pass pipes.

func StdIO

func StdIO() IO

StdIO wires the process standard streams for prompts.

type InputOptions

type InputOptions struct {
	Message string
	Default string
	// Preset returns unprompted when non-nil (empty string is valid input).
	Preset *string
	Flag   string
}

InputOptions tunes an Input call.

type LineOptions added in v1.2.1

type LineOptions struct {
	// Prefix is the text printed before the input, e.g. "you> ". The
	// interactive editor rejects control characters, so keep prefixes
	// plain (no ANSI colors).
	Prefix string
	// Initial is editable text the line starts with; ignored without a
	// terminal, where piped input supplies the line.
	Initial string
}

LineOptions tunes a Line call.

type MultiSelectOptions

type MultiSelectOptions struct {
	Message string
	Options []string
	// Default preselects (subset of Options).
	Default []string
	// Preset returns unprompted when non-nil (validated subset).
	Preset []string
	Flag   string
}

MultiSelectOptions tunes a MultiSelect call.

type PasswordOptions

type PasswordOptions struct {
	Message string
	// Preset returns unprompted when non-nil.
	Preset *string
	Flag   string
}

PasswordOptions tunes a Password call.

type SelectOptions

type SelectOptions struct {
	// Message is the question, e.g. "Target environment?".
	Message string
	// Options are the choices in order.
	Options []string
	// Default preselects when prompting (must be one of Options).
	Default string
	// Preset is the parsed flag value: validated and returned unprompted.
	Preset string
	// Flag names the flag for errors, e.g. "--env".
	Flag string
}

SelectOptions tunes a Select call.

type TabStyle added in v1.2.1

type TabStyle int

TabStyle selects how tab completions are shown.

const (
	// TabCircular cycles through candidates on the prompt line.
	TabCircular TabStyle = iota
	// TabPrints lists candidates bash-style after a second tab.
	TabPrints
)

type WordCompleter added in v1.2.1

type WordCompleter func(line string, pos int) (head string, completions []string, tail string)

WordCompleter splits the line around the word under the cursor. The line "Hello, wo!!!" with the cursor after "wo" arrives as ("Hello, ", ["world", "Word"], "!!!") for candidate "Hello, world!!!".

Jump to

Keyboard shortcuts

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