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

WIP Add with_default for Password #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading