Skip to main content

PromptBase

Struct PromptBase 

Source
pub struct PromptBase {
    pub prompt: String,
    pub console: Option<Console>,
    pub password: bool,
    pub choices: Option<Vec<String>>,
    pub case_sensitive: bool,
    pub show_default: bool,
    pub show_choices: bool,
}
Expand description

Base configuration for all prompt types.

Holds common fields like the prompt text, optional console, password mode, choices list, case sensitivity, and display flags.

Fields§

§prompt: String

The prompt text to display.

§console: Option<Console>

An optional Console for styled output. If None, writes directly to std::io::stdout().

§password: bool

When true, input characters are masked with *.

§choices: Option<Vec<String>>

Optional list of valid choices. When set, the user’s response is validated against this list.

§case_sensitive: bool

Whether choice matching is case-sensitive (default false).

§show_default: bool

Whether to show the default value in the prompt string.

§show_choices: bool

Whether to show the list of choices in the prompt string.

Implementations§

Source§

impl PromptBase

Source

pub fn new(prompt: impl Into<String>) -> Self

Create a new PromptBase with the given prompt text.

Source

pub fn console(self, console: Console) -> Self

Builder: set the console.

Source

pub fn password(self, yes: bool) -> Self

Builder: enable or disable password mode.

Source

pub fn choices(self, choices: Vec<String>) -> Self

Builder: set the valid choices.

Source

pub fn case_sensitive(self, yes: bool) -> Self

Builder: set case sensitivity for choice validation.

Source

pub fn show_default(self, yes: bool) -> Self

Builder: show or hide the default value.

Source

pub fn show_choices(self, yes: bool) -> Self

Builder: show or hide the choices list.

Source

pub fn render_default(&self, default: &str) -> String

Format the default value for display.

Returns " (default: value)" wrapped in the prompt.default style, or an empty string if show_default is false.

Source

pub fn make_prompt(&self) -> String

Build the full prompt string including choices and default.

Returns a string like: "Enter choice [a/b/c] (default: x): "

Source

pub fn check_choice(&self, value: &str) -> bool

Check whether value is a valid choice.

If choices is None, returns true. Otherwise returns true only if value (optionally case-insensitive) matches one of the allowed choices.

Auto Trait Implementations§

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.