Skip to content
Snippets Groups Projects
example.rs 1.37 KiB
Newer Older
extern crate rustyline;

use rustyline::completion::FilenameCompleter;
use rustyline::error::ReadlineError;
gwenn's avatar
gwenn committed
use rustyline::{Config, CompletionType, Editor};
// 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 = ">> ";

gwenn's avatar
gwenn committed
    let config = Config::builder()
        .history_ignore_space(true)
gwenn's avatar
gwenn committed
        .completion_type(CompletionType::List)
gwenn's avatar
gwenn committed
        .build();
    let c = FilenameCompleter::new();
    let mut rl = Editor::with_config(config);
    rl.set_completer(Some(c));
gwenn's avatar
gwenn committed
    if rl.load_history("history.txt").is_err() {
        println!("No previous history.");
    }
    loop {
        let readline = rl.readline(PROMPT);
gwenn's avatar
gwenn committed
                rl.add_history_entry(line.as_ref());
gwenn's avatar
gwenn committed
            }
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
gwenn's avatar
gwenn committed
                break;
            }
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
gwenn's avatar
gwenn committed
                break;
            }
            Err(err) => {
                println!("Error: {:?}", err);
gwenn's avatar
gwenn committed
                break;
    rl.save_history("history.txt").unwrap();