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 ¶
- func Confirm(action, promptText string) (bool, error)
- func ErrConfirmationRequired(action string) error
- func ErrInputRequired(what, hint string) error
- func ErrInputsRequired(problems []string) error
- func IsInteractive() bool
- func Select(what, hint, promptText string, options []string) (string, error)
- func TextInput(what, hint, promptText string) (string, error)
- type PromptError
- type Prompter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Confirm ¶
Confirm is Prompter.Confirm on the default (ambient-stdin) Prompter, for command code without an injected Prompter.
func ErrConfirmationRequired ¶
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 ¶
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 ¶
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().
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 ¶
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) Confirm ¶
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.