interactive

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package interactive owns every interactive prompt in the CLI: it detects whether stdin is attached to a terminal, executes pterm confirm/select/text prompts when it is, and fails fast with actionable errors when it is not.

Interactive prompts read keystrokes from the terminal. In a non-interactive shell — an AI agent's bash tool, CI, or any piped stdin — they never return (the underlying keyboard listener spins forever), so prompt execution is centralized here behind the TTY gate. Command code must prompt through a Prompter (or the package-level helpers backed by the default Prompter) instead of pterm's interactive printers; no direct pterm interactive calls should exist outside this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Confirm

func Confirm(action, promptText string) (bool, error)

Confirm is Prompter.Confirm on the default (ambient-stdin) Prompter, for command code without an injected Prompter.

func ErrConfirmationRequired

func ErrConfirmationRequired(action string) error

ErrConfirmationRequired builds the fail-fast error for confirmation prompts. action describes what would have been confirmed, e.g. "delete profile 'foo'". The resulting error tells the caller (often an AI agent) to re-run with --yes.

func ErrInputRequired

func ErrInputRequired(what, hint string) error

ErrInputRequired builds the fail-fast error for a single text/select prompt. what describes the input that would have been prompted for, e.g. "app name"; hint names the flag(s) to pass instead, e.g. "pass --name to set the app name".

func ErrInputsRequired

func ErrInputsRequired(problems []string) error

ErrInputsRequired builds the fail-fast error for one or more missing or invalid inputs. Commands with several promptable inputs should validate them all up front and report every problem in a single error, so a non-interactive caller can fix everything in one retry instead of discovering problems one invocation at a time.

func IsInteractive

func IsInteractive() bool

IsInteractive reports whether stdin is attached to a terminal, i.e. whether interactive prompts can be shown. Equivalent to NewPrompter().CanPrompt().

func Select

func Select(what, hint, promptText string, options []string) (string, error)

Select is Prompter.Select on the default (ambient-stdin) Prompter, for command code without an injected Prompter.

func TextInput

func TextInput(what, hint, promptText string) (string, error)

TextInput is Prompter.TextInput on the default (ambient-stdin) Prompter, for command code without an injected Prompter.

Types

type PromptError

type PromptError struct {
	// What names what would have been prompted for, e.g. "input" or
	// "confirmation to delete profile 'foo'".
	What string
	// Problems lists the fixes, e.g. "--name is required" or "re-run with
	// --yes to skip the confirmation prompt". Always at least one entry.
	Problems []string
}

PromptError is returned instead of showing an interactive prompt when stdin is not a terminal. It carries every problem the caller must fix so a single retry can succeed. The CLI's top-level error handler renders it via Display without width-based re-wrapping, so flag tokens such as --yes or --template are never split across lines.

func (*PromptError) Display

func (e *PromptError) Display() string

Display renders the problems for terminal output, one per line, so each fix instruction stays an intact, greppable token sequence regardless of terminal width.

func (*PromptError) Error

func (e *PromptError) Error() string

Error renders the problems inline on a single line, following the Go convention that error strings do not contain newlines.

type Prompter

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

Prompter executes interactive prompts against a terminal capability. Each value owns its capability — there is no package-level mutable state — so tests can construct a Prompter with a fixed capability without affecting other goroutines or invocations.

The zero value is equivalent to NewPrompter(): it detects the terminal from stdin at prompt time.

func NewPrompter

func NewPrompter() Prompter

NewPrompter returns a Prompter that detects terminal capability from stdin at prompt time.

func NewPrompterWithTerminal

func NewPrompterWithTerminal(isTTY bool) Prompter

NewPrompterWithTerminal returns a Prompter with a fixed terminal capability. Tests use NewPrompterWithTerminal(false) to exercise the fail-fast paths deterministically, regardless of the harness's stdin.

func (Prompter) CanPrompt

func (p Prompter) CanPrompt() bool

CanPrompt reports whether this Prompter can show interactive prompts.

func (Prompter) Confirm

func (p Prompter) Confirm(action, promptText string) (bool, error)

Confirm shows a yes/no confirmation prompt with the given prompt text and reports the choice. When the Prompter cannot prompt it fails fast with ErrConfirmationRequired(action) instead.

func (Prompter) Select

func (p Prompter) Select(what, hint, promptText string, options []string) (string, error)

Select shows a select prompt over options and returns the chosen option. When the Prompter cannot prompt it fails fast with ErrInputRequired(what, hint) instead.

func (Prompter) TextInput

func (p Prompter) TextInput(what, hint, promptText string) (string, error)

TextInput shows a free-text prompt and returns the entered text. When the Prompter cannot prompt it fails fast with ErrInputRequired(what, hint) instead.

Jump to

Keyboard shortcuts

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