use std::borrow::Cow::{self, Borrowed, Owned};
use rustyline::config::Configurer;
use rustyline::highlight::Highlighter;
use rustyline::{ColorMode, Editor, Result};
use rustyline::{Completer, Helper, Hinter, Validator};
#[derive(Completer, Helper, Hinter, Validator)]
struct MaskingHighlighter {
masking: bool,
}
impl Highlighter for MaskingHighlighter {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
use unicode_width::UnicodeWidthStr;
if self.masking {
Owned("*".repeat(line.width()))
} else {
Borrowed(line)
}
}
fn highlight_char(&self, _line: &str, _pos: usize) -> bool {
self.masking
}
}
fn main() -> Result<()> {
println!("This is just a hack. Reading passwords securely requires more than that.");
let h = MaskingHighlighter { masking: false };
let mut rl = Editor::new()?;
rl.set_helper(Some(h));
let username = rl.readline("Username:")?;
println!("Username: {username}");
rl.helper_mut().expect("No helper").masking = true;
rl.set_color_mode(ColorMode::Forced); rl.set_auto_add_history(false); let passwd = rl.readline("Password:")?;
println!("Secret: {passwd}");
Ok(())
}