inquire/prompts/confirm/
mod.rs

1mod action;
2#[cfg(test)]
3mod test;
4
5pub use action::*;
6
7use crate::{
8    config::get_configuration,
9    error::{InquireError, InquireResult},
10    formatter::{BoolFormatter, DEFAULT_BOOL_FORMATTER},
11    parser::{BoolParser, DEFAULT_BOOL_PARSER},
12    terminal::get_default_terminal,
13    ui::{Backend, CustomTypeBackend, RenderConfig},
14    CustomType,
15};
16
17/// Prompt to ask the user for simple yes/no questions, commonly known by asking the user displaying the `(y/n)` text.
18///
19/// 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.
20///
21/// 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:
22/// - `# Invalid answer, try typing 'y' for yes or 'n' for no`.
23///
24/// 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.
25///
26/// 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.
27///
28/// Confirm prompts provide several options of configuration:
29///
30/// - **Prompt message**: Required when creating the prompt.
31/// - **Default value**: Default value returned when the user submits an empty response.
32/// - **Placeholder**: Short hint that describes the expected value of the input.
33/// - **Help message**: Message displayed at the line below the prompt.
34/// - **Formatter**: Custom formatter in case you need to pre-process the user input before showing it as the final answer.
35///   - Formats `true` to "Yes" and `false` to "No", by default.
36/// - **Parser**: Custom parser for user inputs.
37///   - 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"`.
38/// - **Default value formatter**: Function that formats how the default value is displayed to the user.
39///   - By default, displays "y/n" with the default value capitalized, e.g. "y/N".
40/// - **Error message**: Error message to display when a value could not be parsed from the input.
41///   - Set to "Invalid answer, try typing 'y' for yes or 'n' for no" by default.
42///
43/// # Example
44///
45/// ```no_run
46/// use inquire::Confirm;
47///
48/// let ans = Confirm::new("Do you live in Brazil?")
49///     .with_default(false)
50///     .with_help_message("This data is stored for good reasons")
51///     .prompt();
52///
53/// match ans {
54///     Ok(true) => println!("That's awesome!"),
55///     Ok(false) => println!("That's too bad, I've heard great things about it."),
56///     Err(_) => println!("Error with questionnaire, try again later"),
57/// }
58/// ```
59///
60/// [`Confirm`]: crate::Confirm
61#[derive(Clone)]
62pub struct Confirm<'a> {
63    /// Message to be presented to the user.
64    pub message: &'a str,
65
66    /// Initial value of the prompt's text input.
67    ///
68    /// If you want to set a default value for the prompt, returned when the user's submission is empty, see [`default`].
69    ///
70    /// [`default`]: Self::default
71    pub starting_input: Option<&'a str>,
72
73    /// Default value, returned when the user input is empty.
74    pub default: Option<bool>,
75
76    /// Short hint that describes the expected value of the input.
77    pub placeholder: Option<&'a str>,
78
79    /// Help message to be presented to the user.
80    pub help_message: Option<&'a str>,
81
82    /// Function that formats the user input and presents it to the user as the final rendering of the prompt.
83    pub formatter: BoolFormatter<'a>,
84
85    /// Function that parses the user input and returns the result value.
86    pub parser: BoolParser<'a>,
87
88    /// Function that formats the default value to be presented to the user
89    pub default_value_formatter: BoolFormatter<'a>,
90
91    /// Error message displayed when a value could not be parsed from input.
92    pub error_message: String,
93
94    /// RenderConfig to apply to the rendered interface.
95    ///
96    /// Note: The default render config considers if the NO_COLOR environment variable
97    /// is set to decide whether to render the colored config or the empty one.
98    ///
99    /// When overriding the config in a prompt, NO_COLOR is no longer considered and your
100    /// config is treated as the only source of truth. If you want to customize colors
101    /// and still support NO_COLOR, you will have to do this on your end.
102    pub render_config: RenderConfig<'a>,
103}
104
105impl<'a> Confirm<'a> {
106    /// Default formatter, set to [DEFAULT_BOOL_FORMATTER](crate::formatter::DEFAULT_BOOL_FORMATTER)
107    pub const DEFAULT_FORMATTER: BoolFormatter<'a> = DEFAULT_BOOL_FORMATTER;
108
109    /// Default input parser.
110    pub const DEFAULT_PARSER: BoolParser<'a> = DEFAULT_BOOL_PARSER;
111
112    /// Default formatter for default values, mapping [true] to ["Y/n"] and
113    /// [false] to ["y/N"]
114    pub const DEFAULT_DEFAULT_VALUE_FORMATTER: BoolFormatter<'a> = &|ans| match ans {
115        true => String::from("Y/n"),
116        false => String::from("y/N"),
117    };
118
119    /// Default error message displayed when parsing fails.
120    pub const DEFAULT_ERROR_MESSAGE: &'a str =
121        "Invalid answer, try typing 'y' for yes or 'n' for no";
122
123    /// Creates a [Confirm] with the provided message and default configuration values.
124    pub fn new(message: &'a str) -> Self {
125        Self {
126            message,
127            starting_input: None,
128            default: None,
129            placeholder: None,
130            help_message: None,
131            formatter: Self::DEFAULT_FORMATTER,
132            parser: Self::DEFAULT_PARSER,
133            default_value_formatter: Self::DEFAULT_DEFAULT_VALUE_FORMATTER,
134            error_message: String::from(Self::DEFAULT_ERROR_MESSAGE),
135            render_config: get_configuration(),
136        }
137    }
138
139    /// Sets the initial value of the prompt's text input.
140    ///
141    /// If you want to set a default value for the prompt, returned when the user's submission is empty, see [`with_default`].
142    ///
143    /// [`with_default`]: Self::with_default
144    pub fn with_starting_input(mut self, message: &'a str) -> Self {
145        self.starting_input = Some(message);
146        self
147    }
148
149    /// Sets the default input.
150    pub fn with_default(mut self, default: bool) -> Self {
151        self.default = Some(default);
152        self
153    }
154
155    /// Sets the placeholder.
156    pub fn with_placeholder(mut self, placeholder: &'a str) -> Self {
157        self.placeholder = Some(placeholder);
158        self
159    }
160
161    /// Sets the help message of the prompt.
162    pub fn with_help_message(mut self, message: &'a str) -> Self {
163        self.help_message = Some(message);
164        self
165    }
166
167    /// Sets the formatter.
168    pub fn with_formatter(mut self, formatter: BoolFormatter<'a>) -> Self {
169        self.formatter = formatter;
170        self
171    }
172
173    /// Sets the parser.
174    pub fn with_parser(mut self, parser: BoolParser<'a>) -> Self {
175        self.parser = parser;
176        self
177    }
178
179    /// Sets a custom error message displayed when a submission could not be parsed to a value.
180    pub fn with_error_message(mut self, error_message: &'a str) -> Self {
181        self.error_message = String::from(error_message);
182        self
183    }
184
185    /// Sets the default value formatter
186    pub fn with_default_value_formatter(mut self, formatter: BoolFormatter<'a>) -> Self {
187        self.default_value_formatter = formatter;
188        self
189    }
190
191    /// Sets the provided color theme to this prompt.
192    ///
193    /// Note: The default render config considers if the NO_COLOR environment variable
194    /// is set to decide whether to render the colored config or the empty one.
195    ///
196    /// When overriding the config in a prompt, NO_COLOR is no longer considered and your
197    /// config is treated as the only source of truth. If you want to customize colors
198    /// and still support NO_COLOR, you will have to do this on your end.
199    pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
200        self.render_config = render_config;
201        self
202    }
203
204    /// Parses the provided behavioral and rendering options and prompts
205    /// the CLI user for input according to the defined rules.
206    ///
207    /// This method is intended for flows where the user skipping/cancelling
208    /// the prompt - by pressing ESC - is considered normal behavior. In this case,
209    /// it does not return `Err(InquireError::OperationCanceled)`, but `Ok(None)`.
210    ///
211    /// Meanwhile, if the user does submit an answer, the method wraps the return
212    /// type with `Some`.
213    pub fn prompt_skippable(self) -> InquireResult<Option<bool>> {
214        match self.prompt() {
215            Ok(answer) => Ok(Some(answer)),
216            Err(InquireError::OperationCanceled) => Ok(None),
217            Err(err) => Err(err),
218        }
219    }
220
221    /// Parses the provided behavioral and rendering options and prompts
222    /// the CLI user for input according to the defined rules.
223    pub fn prompt(self) -> InquireResult<bool> {
224        let (input_reader, terminal) = get_default_terminal()?;
225        let mut backend = Backend::new(input_reader, terminal, self.render_config)?;
226        self.prompt_with_backend(&mut backend)
227    }
228
229    pub(crate) fn prompt_with_backend<B: CustomTypeBackend>(
230        self,
231        backend: &mut B,
232    ) -> InquireResult<bool> {
233        CustomType::from(self).prompt_with_backend(backend)
234    }
235}
236
237impl<'a> From<&'a str> for Confirm<'a> {
238    fn from(val: &'a str) -> Self {
239        Confirm::new(val)
240    }
241}
242
243impl<'a> From<Confirm<'a>> for CustomType<'a, bool> {
244    fn from(co: Confirm<'a>) -> Self {
245        Self {
246            message: co.message,
247            starting_input: co.starting_input,
248            default: co.default,
249            default_value_formatter: co.default_value_formatter,
250            placeholder: co.placeholder,
251            help_message: co.help_message,
252            formatter: co.formatter,
253            parser: co.parser,
254            validators: vec![],
255            error_message: co.error_message,
256            render_config: co.render_config,
257        }
258    }
259}