From 96b7f57196c0a2aee460d0a2b66afb1a27547420 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Mon, 18 Dec 2023 19:41:47 +0200 Subject: [PATCH 1/6] start --- inquire/examples/confirm.rs | 3 ++- inquire/examples/custom_type.rs | 1 + inquire/examples/text_options.rs | 2 +- inquire/src/prompts/confirm/mod.rs | 19 +++++++++++++++++++ inquire/src/prompts/custom_type/mod.rs | 18 ++++++++++++++++++ inquire/src/prompts/custom_type/prompt.rs | 12 ++++++++---- 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/inquire/examples/confirm.rs b/inquire/examples/confirm.rs index 1967b6ea..41ea6b60 100644 --- a/inquire/examples/confirm.rs +++ b/inquire/examples/confirm.rs @@ -17,7 +17,8 @@ fn main() { let ans = Confirm { message: "Are you happy?", - default: Some(false), + initial_str_value: Some("si"), + default: None, placeholder: Some("si|no"), help_message: Some("It's alright if you're not"), formatter: &|ans| match ans { diff --git a/inquire/examples/custom_type.rs b/inquire/examples/custom_type.rs index 5e96b4bb..bf8a1004 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_initial_str_value("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/examples/text_options.rs b/inquire/examples/text_options.rs index 28fd3006..ac5b9a0e 100644 --- a/inquire/examples/text_options.rs +++ b/inquire/examples/text_options.rs @@ -12,7 +12,7 @@ fn main() { let _input = Text { message: "How are you feeling?", - initial_value: None, + initial_value: Some("Good"), default: None, placeholder: Some("Good"), help_message: None, diff --git a/inquire/src/prompts/confirm/mod.rs b/inquire/src/prompts/confirm/mod.rs index 8eea0142..a08a94b6 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 initial_str_value: 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, + initial_str_value: 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_initial_str_value(mut self, message: &'a str) -> Self { + self.initial_str_value = 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, + initial_str_value: co.initial_str_value, 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..68b14ed2 100644 --- a/inquire/src/prompts/custom_type/mod.rs +++ b/inquire/src/prompts/custom_type/mod.rs @@ -80,6 +80,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 initial_str_value: Option<&'a str>, + /// Default value, returned when the user input is empty. pub default: Option, @@ -134,6 +141,7 @@ where { Self { message, + initial_str_value: None, default: None, placeholder: None, help_message: None, @@ -146,6 +154,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_initial_str_value(mut self, message: &'a str) -> Self { + self.initial_str_value = 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..d175cb69 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.initial_str_value.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, } } From 3c4f0aed1d45a4ca24b994bf7236e61158ce48c3 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Mon, 18 Dec 2023 20:45:21 +0200 Subject: [PATCH 2/6] fixed --- inquire/examples/confirm.rs | 4 ++-- inquire/examples/text_options.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/inquire/examples/confirm.rs b/inquire/examples/confirm.rs index 41ea6b60..8f0ab017 100644 --- a/inquire/examples/confirm.rs +++ b/inquire/examples/confirm.rs @@ -17,8 +17,8 @@ fn main() { let ans = Confirm { message: "Are you happy?", - initial_str_value: Some("si"), - default: None, + initial_str_value: None, + default: Some(false), placeholder: Some("si|no"), help_message: Some("It's alright if you're not"), formatter: &|ans| match ans { diff --git a/inquire/examples/text_options.rs b/inquire/examples/text_options.rs index ac5b9a0e..28fd3006 100644 --- a/inquire/examples/text_options.rs +++ b/inquire/examples/text_options.rs @@ -12,7 +12,7 @@ fn main() { let _input = Text { message: "How are you feeling?", - initial_value: Some("Good"), + initial_value: None, default: None, placeholder: Some("Good"), help_message: None, From db397ed14957d1b5168e0e803df307d9905b236f Mon Sep 17 00:00:00 2001 From: FroVolod <36816899+FroVolod@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:39:51 +0200 Subject: [PATCH 3/6] fixed test --- inquire/src/prompts/custom_type/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/inquire/src/prompts/custom_type/mod.rs b/inquire/src/prompts/custom_type/mod.rs index 68b14ed2..1cc53947 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?", +/// initial_str_value: None, /// formatter: &|i| format!("${:.2}", i), /// default_value_formatter: &|i| format!("${:.2}", i), /// default: None, From 02c67a4723ac007e73182003f0c845ac1b0f7f93 Mon Sep 17 00:00:00 2001 From: FroVolod <36816899+FroVolod@users.noreply.github.com> Date: Mon, 18 Dec 2023 22:03:45 +0200 Subject: [PATCH 4/6] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f860f3d..5fb67ab9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - 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 initial_str_value for CustomType. [#194](https://github.com/mikaelmello/inquire/pull/194) ### Fixes From 085ae5c29402ca49395d1dfce4e049e8767210d9 Mon Sep 17 00:00:00 2001 From: FroVolod <36816899+FroVolod@users.noreply.github.com> Date: Tue, 19 Dec 2023 10:12:37 +0200 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fb67ab9..17e3ca00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ - 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 initial_str_value for CustomType. [#194](https://github.com/mikaelmello/inquire/pull/194) +- **Breaking** Added initial_str_value for CustomType. [#194](https://github.com/mikaelmello/inquire/pull/194) ### Fixes From e59ee390e6a3c3f6775da53b94fc18cad271e545 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Wed, 27 Dec 2023 11:14:32 +0200 Subject: [PATCH 6/6] renamed: initial_str_value to starting_input, with_initial_str_value to with_starting_input --- CHANGELOG.md | 2 +- inquire/examples/confirm.rs | 2 +- inquire/examples/custom_type.rs | 2 +- inquire/src/prompts/confirm/mod.rs | 10 +++++----- inquire/src/prompts/custom_type/mod.rs | 10 +++++----- inquire/src/prompts/custom_type/prompt.rs | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17e3ca00..95e6d6c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ - 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. -- **Breaking** Added initial_str_value for CustomType. [#194](https://github.com/mikaelmello/inquire/pull/194) +- Added starting_input for CustomType. [#194](https://github.com/mikaelmello/inquire/pull/194) ### Fixes diff --git a/inquire/examples/confirm.rs b/inquire/examples/confirm.rs index 8f0ab017..59e8b790 100644 --- a/inquire/examples/confirm.rs +++ b/inquire/examples/confirm.rs @@ -17,7 +17,7 @@ fn main() { let ans = Confirm { message: "Are you happy?", - initial_str_value: None, + 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 bf8a1004..f1a54aa3 100644 --- a/inquire/examples/custom_type.rs +++ b/inquire/examples/custom_type.rs @@ -2,7 +2,7 @@ use inquire::{validator::Validation, CustomType}; fn main() { let amount = CustomType::::new("How much do you want to donate?") - .with_initial_str_value("10.00") + .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 a08a94b6..651e9bec 100644 --- a/inquire/src/prompts/confirm/mod.rs +++ b/inquire/src/prompts/confirm/mod.rs @@ -66,7 +66,7 @@ pub struct Confirm<'a> { /// 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 initial_str_value: Option<&'a str>, + pub starting_input: Option<&'a str>, /// Default value, returned when the user input is empty. pub default: Option, @@ -122,7 +122,7 @@ impl<'a> Confirm<'a> { pub fn new(message: &'a str) -> Self { Self { message, - initial_str_value: None, + starting_input: None, default: None, placeholder: None, help_message: None, @@ -139,8 +139,8 @@ impl<'a> Confirm<'a> { /// 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_initial_str_value(mut self, message: &'a str) -> Self { - self.initial_str_value = Some(message); + pub fn with_starting_input(mut self, message: &'a str) -> Self { + self.starting_input = Some(message); self } @@ -242,7 +242,7 @@ impl<'a> From> for CustomType<'a, bool> { fn from(co: Confirm<'a>) -> Self { Self { message: co.message, - initial_str_value: co.initial_str_value, + 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 1cc53947..98574413 100644 --- a/inquire/src/prompts/custom_type/mod.rs +++ b/inquire/src/prompts/custom_type/mod.rs @@ -42,7 +42,7 @@ use self::prompt::CustomTypePrompt; /// /// let amount_prompt: CustomType = CustomType { /// message: "How much is your travel going to cost?", -/// initial_str_value: None, +/// starting_input: None, /// formatter: &|i| format!("${:.2}", i), /// default_value_formatter: &|i| format!("${:.2}", i), /// default: None, @@ -86,7 +86,7 @@ pub struct CustomType<'a, T> { /// 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 initial_str_value: Option<&'a str>, + pub starting_input: Option<&'a str>, /// Default value, returned when the user input is empty. pub default: Option, @@ -142,7 +142,7 @@ where { Self { message, - initial_str_value: None, + starting_input: None, default: None, placeholder: None, help_message: None, @@ -160,8 +160,8 @@ where /// 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_initial_str_value(mut self, message: &'a str) -> Self { - self.initial_str_value = Some(message); + pub fn with_starting_input(mut self, message: &'a str) -> Self { + self.starting_input = Some(message); self } diff --git a/inquire/src/prompts/custom_type/prompt.rs b/inquire/src/prompts/custom_type/prompt.rs index d175cb69..f8be8840 100644 --- a/inquire/src/prompts/custom_type/prompt.rs +++ b/inquire/src/prompts/custom_type/prompt.rs @@ -30,7 +30,7 @@ where T: Clone, { fn from(co: CustomType<'a, T>) -> Self { - let input = Input::new_with(co.initial_str_value.unwrap_or_default()); + let input = Input::new_with(co.starting_input.unwrap_or_default()); let input = if let Some(placeholder) = co.placeholder { input.with_placeholder(placeholder) } else {