forked from kkawakam/rustyline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hack in realtime verification support
Please use this in production :-)
- Loading branch information
Showing
10 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters