Skip to main content

Prompter

Struct Prompter 

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

Source

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>

Source

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.

Source

pub const fn with_glyphs(self, glyphs: Glyphs) -> Self

Override the glyph set (Unicode symbols vs ASCII fallback).

Source

pub const fn mode(&self) -> PromptMode

The resolved interaction mode.

Source

pub const fn terminal(&self) -> &T

The bound terminal, for inspecting captured output in tests.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<T> Freeze for Prompter<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Prompter<T>
where T: RefUnwindSafe,

§

impl<T> Send for Prompter<T>
where T: Send,

§

impl<T> Sync for Prompter<T>
where T: Sync,

§

impl<T> Unpin for Prompter<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Prompter<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Prompter<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.