pub struct Prompter<T> { /* private fields */ }Expand description
A terminal-agnostic prompt driver.
Generic over its Terminal so tests can bind a
ScriptedTerminal directly while
Prompter::from_env erases the concrete terminal behind a Box<dyn Terminal>.
Implementations§
Source§impl Prompter<Box<dyn Terminal>>
impl Prompter<Box<dyn Terminal>>
Sourcepub fn from_env(color: ColorChoice) -> Self
pub fn from_env(color: ColorChoice) -> Self
Build a prompter bound to the process environment.
The PromptMode follows whether both stdin and stderr are terminals,
and the Palette follows color against stderr, so interactivity and
styling both honour redirection and NO_COLOR. Prompts render to stderr,
so a redirected stderr (e.g. cmd 2>log) forces PromptMode::NonInteractive
rather than blocking on input behind an invisible prompt. When the
interactive feature is compiled and both streams are terminals, a rich
raw-mode terminal is selected for arrow-key navigation; otherwise a line
terminal is used.
Source§impl<T: Terminal> Prompter<T>
impl<T: Terminal> Prompter<T>
Sourcepub const fn new(terminal: T, mode: PromptMode, palette: Palette) -> Self
pub const fn new(terminal: T, mode: PromptMode, palette: Palette) -> Self
Build a prompter from an explicit terminal, mode, and palette.
Glyphs default to the ASCII fallback for byte-clean, deterministic tests;
override with Prompter::with_glyphs.
Sourcepub const fn with_glyphs(self, glyphs: Glyphs) -> Self
pub const fn with_glyphs(self, glyphs: Glyphs) -> Self
Override the glyph set (Unicode symbols vs ASCII fallback).
Sourcepub const fn mode(&self) -> PromptMode
pub const fn mode(&self) -> PromptMode
The resolved interaction mode.
Sourcepub const fn terminal(&self) -> &T
pub const fn terminal(&self) -> &T
The bound terminal, for inspecting captured output in tests.
Sourcepub fn select(
&mut self,
prompt: &str,
choices: &[Choice],
) -> AppResult<ChoiceId>
pub fn select( &mut self, prompt: &str, choices: &[Choice], ) -> AppResult<ChoiceId>
Ask for exactly one choice.
In PromptMode::NonInteractive this resolves to the recommended choice;
with none it is a typed error. A key-driven terminal shows an arrow-key
radio list; a line-driven terminal shows a numbered list.
§Errors
Returns an error when choices is empty, when a non-interactive prompt has
no recommended default, when the user cancels, or when input closes early.
Sourcepub fn multi_select(
&mut self,
prompt: &str,
choices: &[Choice],
) -> AppResult<Vec<ChoiceId>>
pub fn multi_select( &mut self, prompt: &str, choices: &[Choice], ) -> AppResult<Vec<ChoiceId>>
Ask for zero or more choices.
The default answer is the set of recommended choices, which may be empty. A key-driven terminal shows an arrow-key checkbox list; a line-driven terminal accepts a comma-separated list of numbers.
§Errors
Returns an error when choices is empty, when the user cancels, or when
input closes early.
Sourcepub fn confirm(&mut self, prompt: &str, default: bool) -> AppResult<bool>
pub fn confirm(&mut self, prompt: &str, default: bool) -> AppResult<bool>
Ask a yes/no question with an explicit default.
§Errors
Returns an error when the user cancels or when input closes early.
Sourcepub fn text(&mut self, prompt: &str, default: Option<&str>) -> AppResult<String>
pub fn text(&mut self, prompt: &str, default: Option<&str>) -> AppResult<String>
Ask for freeform text with an optional default.
In PromptMode::NonInteractive this resolves to default; with none it
is a typed error.
§Errors
Returns an error when a non-interactive prompt has no default, when the user cancels, or when input closes early.
Sourcepub fn text_with(
&mut self,
prompt: &str,
default: Option<&str>,
validator: &dyn Validator,
) -> AppResult<String>
pub fn text_with( &mut self, prompt: &str, default: Option<&str>, validator: &dyn Validator, ) -> AppResult<String>
Ask for freeform text validated by validator, re-asking on rejection.
In PromptMode::NonInteractive a rejected default is a typed error
rather than a silent bad value.
§Errors
Returns an error when a non-interactive prompt has no default or a rejected default, when the user cancels, or when input closes early.