Skip to content

Commit

Permalink
feat: Add with_default for Password
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-leinz committed Jun 30, 2024
1 parent 0e39b45 commit 9da17ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions inquire/src/prompts/password/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -161,6 +164,7 @@ impl<'a> Password<'a> {
formatter: Self::DEFAULT_FORMATTER,
validators: Self::DEFAULT_VALIDATORS,
render_config: get_configuration(),
default: None,
}
}

Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions inquire/src/prompts/password/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -51,6 +52,7 @@ impl<'a> From<Password<'a>> for PasswordPrompt<'a> {
Self {
message: so.message,
config: (&so).into(),
default: so.default,
help_message: so.help_message,
current_mode: so.display_mode,
confirmation,
Expand Down Expand Up @@ -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<bool> {
if let Some(confirmation) = &mut self.confirmation {
if self.confirmation_stage {
Expand Down

0 comments on commit 9da17ea

Please sign in to comment.