diff --git a/CHANGELOG.md b/CHANGELOG.md index a1ef8b68..06c50635 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,10 +23,12 @@ - Ctrl-b/Ctrl-f for left/right - Ctrl-j/Ctrl-g for enter/cancel - Added 'with_starting_filter_input' to both Select and MultiSelect, which allows for setting an initial value to the filter section of the prompt. +- Added starting_input for CustomType. [#194](https://github.com/mikaelmello/inquire/pull/194) - Added 'without_filtering' to both Select and MultiSelect, useful when you want to simplify the UX if the filter does not add any value, such as when the list is already short. - Added 'with_answered_prompt_prefix' to RenderConfig to allow customization of answered prompt prefix. - Revamped keybindings for DateSelect. + ### Fixes - Fixed typos in the code's comments. diff --git a/inquire/examples/confirm.rs b/inquire/examples/confirm.rs index 1967b6ea..59e8b790 100644 --- a/inquire/examples/confirm.rs +++ b/inquire/examples/confirm.rs @@ -17,6 +17,7 @@ fn main() { let ans = Confirm { message: "Are you happy?", + starting_input: None, default: Some(false), placeholder: Some("si|no"), help_message: Some("It's alright if you're not"), diff --git a/inquire/examples/custom_type.rs b/inquire/examples/custom_type.rs index 5e96b4bb..f1a54aa3 100644 --- a/inquire/examples/custom_type.rs +++ b/inquire/examples/custom_type.rs @@ -2,6 +2,7 @@ use inquire::{validator::Validation, CustomType}; fn main() { let amount = CustomType::::new("How much do you want to donate?") + .with_starting_input("10.00") .with_formatter(&|i| format!("${i:.2}")) .with_error_message("Please type a valid number") .with_help_message("Type the amount in US dollars using a decimal point as a separator") diff --git a/inquire/src/prompts/confirm/mod.rs b/inquire/src/prompts/confirm/mod.rs index 8eea0142..651e9bec 100644 --- a/inquire/src/prompts/confirm/mod.rs +++ b/inquire/src/prompts/confirm/mod.rs @@ -61,6 +61,13 @@ pub struct Confirm<'a> { /// Message to be presented to the user. pub message: &'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`]: Self::default + pub starting_input: Option<&'a str>, + /// Default value, returned when the user input is empty. pub default: Option, @@ -115,6 +122,7 @@ impl<'a> Confirm<'a> { pub fn new(message: &'a str) -> Self { Self { message, + starting_input: None, default: None, placeholder: None, help_message: None, @@ -126,6 +134,16 @@ impl<'a> Confirm<'a> { } } + /// 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`]. + /// + /// [`with_default`]: Self::with_default + pub fn with_starting_input(mut self, message: &'a str) -> Self { + self.starting_input = Some(message); + self + } + /// Sets the default input. pub fn with_default(mut self, default: bool) -> Self { self.default = Some(default); @@ -224,6 +242,7 @@ impl<'a> From> for CustomType<'a, bool> { fn from(co: Confirm<'a>) -> Self { Self { message: co.message, + starting_input: co.starting_input, default: co.default, default_value_formatter: co.default_value_formatter, placeholder: co.placeholder, diff --git a/inquire/src/prompts/custom_type/mod.rs b/inquire/src/prompts/custom_type/mod.rs index 319a05fe..98574413 100644 --- a/inquire/src/prompts/custom_type/mod.rs +++ b/inquire/src/prompts/custom_type/mod.rs @@ -42,6 +42,7 @@ use self::prompt::CustomTypePrompt; /// /// let amount_prompt: CustomType = CustomType { /// message: "How much is your travel going to cost?", +/// starting_input: None, /// formatter: &|i| format!("${:.2}", i), /// default_value_formatter: &|i| format!("${:.2}", i), /// default: None, @@ -80,6 +81,13 @@ pub struct CustomType<'a, T> { /// Message to be presented to the user. pub message: &'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`]: Self::default + pub starting_input: Option<&'a str>, + /// Default value, returned when the user input is empty. pub default: Option, @@ -134,6 +142,7 @@ where { Self { message, + starting_input: None, default: None, placeholder: None, help_message: None, @@ -146,6 +155,16 @@ where } } + /// 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`]. + /// + /// [`with_default`]: Self::with_default + pub fn with_starting_input(mut self, message: &'a str) -> Self { + self.starting_input = Some(message); + self + } + /// Sets the default input. pub fn with_default(mut self, default: T) -> Self { self.default = Some(default); diff --git a/inquire/src/prompts/custom_type/prompt.rs b/inquire/src/prompts/custom_type/prompt.rs index 3a1f4dfd..f8be8840 100644 --- a/inquire/src/prompts/custom_type/prompt.rs +++ b/inquire/src/prompts/custom_type/prompt.rs @@ -30,6 +30,13 @@ where T: Clone, { fn from(co: CustomType<'a, T>) -> Self { + let input = Input::new_with(co.starting_input.unwrap_or_default()); + let input = if let Some(placeholder) = co.placeholder { + input.with_placeholder(placeholder) + } else { + input + }; + Self { message: co.message, config: (&co).into(), @@ -40,10 +47,7 @@ where default_value_formatter: co.default_value_formatter, validators: co.validators, parser: co.parser, - input: co - .placeholder - .map(|p| Input::new().with_placeholder(p)) - .unwrap_or_else(Input::new), + input, error_message: co.error_message, } }