Struct inquire::Confirm

source ·
pub struct Confirm<'a> {
    pub message: &'a str,
    pub starting_input: Option<&'a str>,
    pub default: Option<bool>,
    pub placeholder: Option<&'a str>,
    pub help_message: Option<&'a str>,
    pub formatter: BoolFormatter<'a>,
    pub parser: BoolParser<'a>,
    pub default_value_formatter: BoolFormatter<'a>,
    pub error_message: String,
    pub render_config: RenderConfig<'a>,
}
Expand description

Prompt to ask the user for simple yes/no questions, commonly known by asking the user displaying the (y/n) text.

This prompt is basically a wrapper around the behavior of CustomType prompts, providing a sensible set of defaults to ask for simple true/false questions, such as confirming an action.

Default values are formatted with the given value in uppercase, e.g. (Y/n) or (y/N). The bool parser accepts by default only the following inputs (case-insensitive): y, n, yes and no. If the user input does not match any of them, the following error message is displayed by default:

  • # Invalid answer, try typing 'y' for yes or 'n' for no.

Finally, once the answer is submitted, Confirm prompts display the bool value formatted as either “Yes”, if a true value was parsed, or “No” otherwise.

The Confirm prompt does not support custom validators because of the nature of the prompt. The user input is always parsed to true or false. If one of the two alternatives is invalid, a Confirm prompt that only allows yes or no answers does not make a lot of sense to me, but if someone provides a clear use-case I will reconsider.

Confirm prompts provide several options of configuration:

  • Prompt message: Required when creating the prompt.
  • Default value: Default value returned when the user submits an empty response.
  • Placeholder: Short hint that describes the expected value of the input.
  • Help message: Message displayed at the line below the prompt.
  • Formatter: Custom formatter in case you need to pre-process the user input before showing it as the final answer.
    • Formats true to “Yes” and false to “No”, by default.
  • Parser: Custom parser for user inputs.
    • The default bool parser returns true if the input is either "y" or "yes", in a case-insensitive comparison. Similarly, the parser returns false if the input is either "n" or "no".
  • Default value formatter: Function that formats how the default value is displayed to the user.
    • By default, displays “y/n” with the default value capitalized, e.g. “y/N”.
  • Error message: Error message to display when a value could not be parsed from the input.
    • Set to “Invalid answer, try typing ‘y’ for yes or ‘n’ for no” by default.

§Example

use inquire::Confirm;

let ans = Confirm::new("Do you live in Brazil?")
    .with_default(false)
    .with_help_message("This data is stored for good reasons")
    .prompt();

match ans {
    Ok(true) => println!("That's awesome!"),
    Ok(false) => println!("That's too bad, I've heard great things about it."),
    Err(_) => println!("Error with questionnaire, try again later"),
}

Fields§

§message: &'a str

Message to be presented to the user.

§starting_input: Option<&'a str>

Initial value of the prompt’s text input.

If you want to set a default value for the prompt, returned when the user’s submission is empty, see default.

§default: Option<bool>

Default value, returned when the user input is empty.

§placeholder: Option<&'a str>

Short hint that describes the expected value of the input.

§help_message: Option<&'a str>

Help message to be presented to the user.

§formatter: BoolFormatter<'a>

Function that formats the user input and presents it to the user as the final rendering of the prompt.

§parser: BoolParser<'a>

Function that parses the user input and returns the result value.

§default_value_formatter: BoolFormatter<'a>

Function that formats the default value to be presented to the user

§error_message: String

Error message displayed when a value could not be parsed from input.

§render_config: RenderConfig<'a>

RenderConfig to apply to the rendered interface.

Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.

When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still support NO_COLOR, you will have to do this on your end.

Implementations§

source§

impl<'a> Confirm<'a>

source

pub const DEFAULT_FORMATTER: BoolFormatter<'a> = DEFAULT_BOOL_FORMATTER

Default formatter, set to DEFAULT_BOOL_FORMATTER

source

pub const DEFAULT_PARSER: BoolParser<'a> = DEFAULT_BOOL_PARSER

Default input parser.

source

pub const DEFAULT_DEFAULT_VALUE_FORMATTER: BoolFormatter<'a> = _

Default formatter for default values, mapping true to [“Y/n”] and false to [“y/N”]

source

pub const DEFAULT_ERROR_MESSAGE: &'a str = "Invalid answer, try typing 'y' for yes or 'n' for no"

Default error message displayed when parsing fails.

source

pub fn new(message: &'a str) -> Self

Creates a Confirm with the provided message and default configuration values.

source

pub fn with_starting_input(self, message: &'a str) -> Self

Sets the initial value of the prompt’s text input.

If you want to set a default value for the prompt, returned when the user’s submission is empty, see with_default.

source

pub fn with_default(self, default: bool) -> Self

Sets the default input.

source

pub fn with_placeholder(self, placeholder: &'a str) -> Self

Sets the placeholder.

source

pub fn with_help_message(self, message: &'a str) -> Self

Sets the help message of the prompt.

source

pub fn with_formatter(self, formatter: BoolFormatter<'a>) -> Self

Sets the formatter.

source

pub fn with_parser(self, parser: BoolParser<'a>) -> Self

Sets the parser.

source

pub fn with_error_message(self, error_message: &'a str) -> Self

Sets a custom error message displayed when a submission could not be parsed to a value.

source

pub fn with_default_value_formatter(self, formatter: BoolFormatter<'a>) -> Self

Sets the default value formatter

source

pub fn with_render_config(self, render_config: RenderConfig<'a>) -> Self

Sets the provided color theme to this prompt.

Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.

When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still support NO_COLOR, you will have to do this on your end.

source

pub fn prompt_skippable(self) -> InquireResult<Option<bool>>

Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.

This method is intended for flows where the user skipping/cancelling the prompt - by pressing ESC - is considered normal behavior. In this case, it does not return Err(InquireError::OperationCanceled), but Ok(None).

Meanwhile, if the user does submit an answer, the method wraps the return type with Some.

source

pub fn prompt(self) -> InquireResult<bool>

Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.

Trait Implementations§

source§

impl<'a> Clone for Confirm<'a>

source§

fn clone(&self) -> Confirm<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> From<&'a str> for Confirm<'a>

source§

fn from(val: &'a str) -> Self

Converts to this type from the input type.
source§

impl<'a> From<Confirm<'a>> for CustomType<'a, bool>

source§

fn from(co: Confirm<'a>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for Confirm<'a>

§

impl<'a> !RefUnwindSafe for Confirm<'a>

§

impl<'a> !Send for Confirm<'a>

§

impl<'a> !Sync for Confirm<'a>

§

impl<'a> Unpin for Confirm<'a>

§

impl<'a> !UnwindSafe for Confirm<'a>

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> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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

§

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.