Skip to content
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
1 change: 1 addition & 0 deletions examples/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fn main() {
let password = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Password")
.with_confirmation("Repeat password", "Error: the passwords don't match.")
// .with_mask('*') // uncomment to mask the password with a given character
.validate_with(|input: &String| -> Result<(), &str> {
if input.chars().count() > 3 {
Ok(())
Expand Down
67 changes: 63 additions & 4 deletions src/prompts/password.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{io, sync::Arc};

use console::Term;
use console::{Key, Term};
use zeroize::Zeroizing;

use crate::{
Expand Down Expand Up @@ -36,6 +36,7 @@ pub struct Password<'a> {
allow_empty_password: bool,
confirmation_prompt: Option<(String, String)>,
validator: Option<PasswordValidatorCallback<'a>>,
mask: Option<char>,
}

impl Default for Password<'static> {
Expand Down Expand Up @@ -84,6 +85,32 @@ impl Password<'_> {
self
}

/// Sets the character used to mask each typed character.
///
/// By default no mask is used and the input is fully hidden (nothing is
/// echoed). When a mask is set, one mask character is echoed for every
/// character typed.
///
/// ## Example
///
/// ```rust,no_run
/// use dialoguer::Password;
///
/// fn main() {
/// let password = Password::new()
/// .with_prompt("Enter password")
/// .with_mask('*')
/// .interact()
/// .unwrap();
///
/// println!("Your password length is: {}", password.len());
/// }
/// ```
pub fn with_mask(mut self, mask: char) -> Self {
self.mask = Some(mask);
self
}

/// Enables user interaction and returns the result.
///
/// If the user confirms the result is `Ok()`, `Err()` otherwise.
Expand Down Expand Up @@ -136,12 +163,43 @@ impl Password<'_> {
render.password_prompt(prompt)?;
render.term().flush()?;

let input = render.term().read_secure_line()?;
let Some(mask) = self.mask else {
let input = render.term().read_secure_line()?;
render.add_line();

if !input.is_empty() || self.allow_empty_password {
return Ok(input);
}

continue;
};

let term = render.term();
let mut password = String::new();

loop {
match term.read_key()? {
Key::Char(chr) if !chr.is_ascii_control() => {
password.push(chr);
term.write_str(mask.encode_utf8(&mut [0; 4]))?;
term.flush()?;
}
Key::Backspace if !password.is_empty() => {
password.pop();
term.clear_chars(1)?;
term.flush()?;
}
Key::Enter => break,
_ => {}
}
}

term.write_line("")?;

render.add_line();

if !input.is_empty() || self.allow_empty_password {
return Ok(input);
if !password.is_empty() || self.allow_empty_password {
return Ok(password);
}
}
}
Expand Down Expand Up @@ -213,6 +271,7 @@ impl<'a> Password<'a> {
allow_empty_password: false,
confirmation_prompt: None,
validator: None,
mask: None,
}
}
}
Expand Down
Loading