From 8a3d0458554500bd8324f1313e0dfffdabf4aad0 Mon Sep 17 00:00:00 2001 From: kbjakex <90567676+kbjakex@users.noreply.github.com> Date: Sun, 23 Oct 2022 19:46:20 +0300 Subject: [PATCH] Oops #1 --- src/examples/example.rs | 98 ----------------------------------- src/examples/read_password.rs | 43 --------------- 2 files changed, 141 deletions(-) delete mode 100644 src/examples/example.rs delete mode 100644 src/examples/read_password.rs diff --git a/src/examples/example.rs b/src/examples/example.rs deleted file mode 100644 index b98e9463f..000000000 --- a/src/examples/example.rs +++ /dev/null @@ -1,98 +0,0 @@ -use std::borrow::Cow::{self, Borrowed, Owned}; - -use rustyline::completion::FilenameCompleter; -use rustyline::error::ReadlineError; -use rustyline::highlight::{Highlighter, MatchingBracketHighlighter}; -use rustyline::hint::HistoryHinter; -use rustyline::validate::MatchingBracketValidator; -use rustyline::{Cmd, CompletionType, Config, EditMode, Editor, KeyEvent}; -use rustyline_derive::{Completer, Helper, Hinter, Validator}; - -#[derive(Helper, Completer, Hinter, Validator)] -struct MyHelper { - #[rustyline(Completer)] - completer: FilenameCompleter, - highlighter: MatchingBracketHighlighter, - #[rustyline(Validator)] - validator: MatchingBracketValidator, - #[rustyline(Hinter)] - hinter: HistoryHinter, - colored_prompt: String, -} - -impl Highlighter for MyHelper { - fn highlight_prompt<'b, 's: 'b, 'p: 'b>( - &'s self, - prompt: &'p str, - default: bool, - ) -> Cow<'b, str> { - if default { - Borrowed(&self.colored_prompt) - } else { - Borrowed(prompt) - } - } - - fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { - Owned("\x1b[1m".to_owned() + hint + "\x1b[m") - } - - fn highlight<'l>(&self, line: &'l str, pos: usize, submit: bool) -> Cow<'l, str> { - self.highlighter.highlight(line, pos, submit) - } - - fn highlight_char(&self, line: &str, pos: usize) -> bool { - self.highlighter.highlight_char(line, pos) - } -} - -// To debug rustyline: -// RUST_LOG=rustyline=debug cargo run --example example 2> debug.log -fn main() -> rustyline::Result<()> { - env_logger::init(); - let config = Config::builder() - .history_ignore_space(true) - .completion_type(CompletionType::List) - .edit_mode(EditMode::Emacs) - .build(); - let h = MyHelper { - completer: FilenameCompleter::new(), - highlighter: MatchingBracketHighlighter::new(), - hinter: HistoryHinter {}, - colored_prompt: "".to_owned(), - validator: MatchingBracketValidator::new(), - }; - let mut rl = Editor::with_config(config)?; - rl.set_helper(Some(h)); - rl.bind_sequence(KeyEvent::alt('n'), Cmd::HistorySearchForward); - rl.bind_sequence(KeyEvent::alt('p'), Cmd::HistorySearchBackward); - if rl.load_history("history.txt").is_err() { - println!("No previous history."); - } - let mut count = 1; - loop { - let p = format!("{}> ", count); - rl.helper_mut().expect("No helper").colored_prompt = format!("\x1b[1;32m{}\x1b[0m", p); - let readline = rl.readline(&p); - match readline { - Ok(line) => { - rl.add_history_entry(line.as_str()); - println!("Line: {}", line); - } - Err(ReadlineError::Interrupted) => { - println!("Interrupted"); - break; - } - Err(ReadlineError::Eof) => { - println!("Encountered Eof"); - break; - } - Err(err) => { - println!("Error: {:?}", err); - break; - } - } - count += 1; - } - rl.append_history("history.txt") -} diff --git a/src/examples/read_password.rs b/src/examples/read_password.rs deleted file mode 100644 index dbd26c365..000000000 --- a/src/examples/read_password.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::borrow::Cow::{self, Borrowed, Owned}; - -use rustyline::config::Configurer; -use rustyline::highlight::Highlighter; -use rustyline::{ColorMode, Editor, Result}; -use rustyline_derive::{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, _submit: bool) -> 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); // force masking - rl.set_auto_add_history(false); // make sure password is not added to history - let passwd = rl.readline("Password:")?; - println!("Secret: {}", passwd); - Ok(()) -}