nocomments.rs 3.42 KB
Newer Older
MovingtoMars's avatar
MovingtoMars committed
1 2
extern crate liner;
extern crate termion;
3
extern crate regex;
MovingtoMars's avatar
MovingtoMars committed
4 5

use std::mem::replace;
6
use std::env::{args, current_dir};
7
use std::io;
MovingtoMars's avatar
MovingtoMars committed
8 9

use liner::{Context, CursorPosition, Event, EventKind, FilenameCompleter};
10 11 12
use termion::color;
use regex::Regex;

Sag0Sag0's avatar
Sag0Sag0 committed
13
fn highlight_dodo(s: &str) -> String {
14 15
    let reg_exp = Regex::new("(?P<k>dodo)").unwrap();
    let format = format!("{}$k{}", color::Fg(color::Red), color::Fg(color::Reset));
Sag0Sag0's avatar
Sag0Sag0 committed
16
    reg_exp.replace_all(s, format.as_str()).to_string()
17
}
MovingtoMars's avatar
MovingtoMars committed
18

19
fn main() {
MovingtoMars's avatar
MovingtoMars committed
20 21
    let mut con = Context::new();

22 23 24 25 26 27 28 29 30 31
    let history_file = match args().nth(1) {
        Some(file_name) => {
            println!("History file: {}", file_name);
            file_name
        }
        None => {
            eprintln!("No history file provided. Ending example early.");
            return;
        }
    };
32

33
    con.history.set_file_name_and_load_history(history_file).unwrap();
Sag0Sag0's avatar
Sag0Sag0 committed
34

MovingtoMars's avatar
MovingtoMars committed
35
    loop {
Sag0Sag0's avatar
Sag0Sag0 committed
36
        let res = con.read_line("[prompt]$ ",
Sag0Sag0's avatar
Sag0Sag0 committed
37
                                Some(Box::new(highlight_dodo)),
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
                                &mut |Event { editor, kind }| {
            if let EventKind::BeforeComplete = kind {
                let (_, pos) = editor.get_words_and_cursor_position();

                // Figure out of we are completing a command (the first word) or a filename.
                let filename = match pos {
                    CursorPosition::InWord(i) => i > 0,
                    CursorPosition::InSpace(Some(_), _) => true,
                    CursorPosition::InSpace(None, _) => false,
                    CursorPosition::OnWordLeftEdge(i) => i >= 1,
                    CursorPosition::OnWordRightEdge(i) => i >= 1,
                };

                if filename {
                    let completer = FilenameCompleter::new(Some(current_dir().unwrap()));
                    replace(&mut editor.context().completer, Some(Box::new(completer)));
                } else {
                    replace(&mut editor.context().completer, None);
                }
            }
        });

        match res {
            Ok(res) => {
                match res.as_str() {
                    "emacs" => {
                        con.key_bindings = liner::KeyBindings::Emacs;
                        println!("emacs mode");
                    }
                    "vi" => {
                        con.key_bindings = liner::KeyBindings::Vi;
                        println!("vi mode");
                    }
                    "exit" | "" => {
                        println!("exiting...");
                        break;
MovingtoMars's avatar
MovingtoMars committed
74
                    }
75
                    _ => {}
MovingtoMars's avatar
MovingtoMars committed
76 77
                }

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                if res.is_empty() {
                    break;
                }

                con.history.push(res.into()).unwrap();
            }
            Err(e) => {
                match e.kind() {
                    // ctrl-c pressed
                    io::ErrorKind::Interrupted => {}
                    // ctrl-d pressed
                    io::ErrorKind::UnexpectedEof => {
                        println!("exiting...");
                        break;
                    }
Michael Aaron Murphy's avatar
Michael Aaron Murphy committed
93 94 95 96 97
                    _ => {
                        // Ensure that all writes to the history file
                        // are written before exiting.
                        panic!("error: {:?}", e)
                    },
98 99
                }
            }
MovingtoMars's avatar
MovingtoMars committed
100 101
        }
    }
Michael Aaron Murphy's avatar
Michael Aaron Murphy committed
102
    // Ensure that all writes to the history file are written before exiting.
103 104
    con.history.commit_to_file();
}