Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added initial_str_value for CustomType #194

Merged
merged 8 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions inquire/examples/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions inquire/examples/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use inquire::{validator::Validation, CustomType};

fn main() {
let amount = CustomType::<f64>::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")
Expand Down
19 changes: 19 additions & 0 deletions inquire/src/prompts/confirm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,

Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -224,6 +242,7 @@ impl<'a> From<Confirm<'a>> 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,
Expand Down
19 changes: 19 additions & 0 deletions inquire/src/prompts/custom_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use self::prompt::CustomTypePrompt;
///
/// let amount_prompt: CustomType<f64> = 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,
Expand Down Expand Up @@ -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<T>,

Expand Down Expand Up @@ -134,6 +142,7 @@ where
{
Self {
message,
starting_input: None,
default: None,
placeholder: None,
help_message: None,
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions inquire/src/prompts/custom_type/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
}
}
Expand Down
Loading