Newer
Older
use log::{LogLevel, LogLevelFilter, LogMetadata, LogRecord, SetLoggerError};
use rustyline::error::ReadlineError;
use rustyline::{Cmd, CompletionType, Config, EditMode, Editor, KeyPress};
// On unix platforms you can use ANSI escape sequences
#[cfg(unix)]
static PROMPT: &'static str = "\x1b[1;32m>>\x1b[0m ";
// Windows consoles typically don't support ANSI escape sequences out
// of the box
#[cfg(windows)]
static PROMPT: &'static str = ">> ";
struct Hints {}
impl Hinter for Hints {
fn hint(&mut self, line: &str, _pos: usize) -> Option<String> {
if line == "hello" {
Some(" \x1b[1mWorld\x1b[m".to_owned())
} else {
None
}
}
}
let config = Config::builder()
.history_ignore_space(true)
let mut rl = Editor::with_config(config);
rl.set_completer(Some(Rc::new(RefCell::new(c))));
rl.bind_sequence(KeyPress::Meta('N'), Cmd::HistorySearchForward);
rl.bind_sequence(KeyPress::Meta('P'), Cmd::HistorySearchBackward);
println!("No previous history.");
}
loop {
let readline = rl.readline(PROMPT);
match readline {
Ok(line) => {
println!("Line: {}", line);
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
Err(ReadlineError::Eof) => {
println!("CTRL-D");
Err(err) => {
println!("Error: {:?}", err);
}
}
rl.save_history("history.txt").unwrap();
struct Logger;
impl log::Log for Logger {
fn enabled(&self, metadata: &LogMetadata) -> bool {
metadata.level() <= LogLevel::Debug
}
fn log(&self, record: &LogRecord) {
if self.enabled(record.metadata()) {
writeln!(io::stderr(), "{} - {}", record.level(), record.args()).unwrap();
}
}
}
fn init_logger() -> Result<(), SetLoggerError> {
log::set_logger(|max_log_level| {