prompt

package
v1.109.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

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

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

func NewInputField(opts InputOpts, value *string) *huh.Input

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

func NewMultiSelectField(title string, options []string, value *[]string) *huh.MultiSelect[string]

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

func New(lookupEnv func(string) (string, bool)) *Prompter

New builds a prompter over the real terminal, in plain-text mode when the environment asks for it.

func NewForStreams

func NewForStreams(in io.Reader, out io.Writer) *Prompter

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

func (p *Prompter) Accessible() bool

Accessible reports whether prompts are drawn as plain numbered text.

func (*Prompter) Input

func (p *Prompter) Input(opts InputOpts) (string, error)

Input asks the user to type a value.

func (*Prompter) MultiSelect

func (p *Prompter) MultiSelect(title string, options, defaults []string) ([]string, error)

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.

Jump to

Keyboard shortcuts

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