From 9da17eaad25f83bc1d8a3d96d2352ecb3eb7e577 Mon Sep 17 00:00:00 2001 From: Caleb Leinz Date: Sat, 29 Jun 2024 17:09:21 -0700 Subject: [PATCH] feat: Add with_default for Password --- inquire/src/prompts/password/mod.rs | 10 ++++++++++ inquire/src/prompts/password/prompt.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/inquire/src/prompts/password/mod.rs b/inquire/src/prompts/password/mod.rs index b89f3bd8..7fbb9c9c 100644 --- a/inquire/src/prompts/password/mod.rs +++ b/inquire/src/prompts/password/mod.rs @@ -101,6 +101,9 @@ pub struct Password<'a> { /// Function that formats the user input and presents it to the user as the final rendering of the prompt. pub formatter: StringFormatter<'a>, + /// Default value, returned when the user input is empty. + pub default: Option<&'a str>, + /// How the password input is displayed to the user. pub display_mode: PasswordDisplayMode, @@ -161,6 +164,7 @@ impl<'a> Password<'a> { formatter: Self::DEFAULT_FORMATTER, validators: Self::DEFAULT_VALIDATORS, render_config: get_configuration(), + default: None, } } @@ -170,6 +174,12 @@ impl<'a> Password<'a> { self } + /// Sets the default input. + pub fn with_default(mut self, message: &'a str) -> Self { + self.default = Some(message); + self + } + /// Sets the flag to enable display toggling. pub fn with_display_toggle_enabled(mut self) -> Self { self.enable_display_toggle = true; diff --git a/inquire/src/prompts/password/prompt.rs b/inquire/src/prompts/password/prompt.rs index 74d783b4..b8ae59a2 100644 --- a/inquire/src/prompts/password/prompt.rs +++ b/inquire/src/prompts/password/prompt.rs @@ -25,6 +25,7 @@ struct PasswordConfirmation<'a> { pub struct PasswordPrompt<'a> { message: &'a str, config: PasswordConfig, + default: Option<&'a str>, help_message: Option<&'a str>, input: Input, current_mode: PasswordDisplayMode, @@ -51,6 +52,7 @@ impl<'a> From> for PasswordPrompt<'a> { Self { message: so.message, config: (&so).into(), + default: so.default, help_message: so.help_message, current_mode: so.display_mode, confirmation, @@ -156,6 +158,14 @@ where (self.formatter)(answer) } + fn setup(&mut self) -> InquireResult<()> { + if let Some(val) = self.default { + self.message = val; + } + + Ok(()) + } + fn pre_cancel(&mut self) -> InquireResult { if let Some(confirmation) = &mut self.confirmation { if self.confirmation_stage {