aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: ca5dc002cddc222beaae569a518bce2ecda770cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use rustyline::error::ReadlineError;
use rustyline::Editor;

mod commandline;
use commandline::CommandLine;
mod prompt;

fn main() -> Result<(), std::io::Error>{
    if let Ok(motd) = std::fs::read_to_string("/etc/motd") {
        print!("{}", motd)
    }

    // TODO: [completer] `()` can be used when no completer is required
    let mut rl = Editor::<()>::new();
    let prompt = prompt::Prompt::new()?;

    let mut status = None;  // exit status of last command
    // map of variables

    /*if rl.load_history("history.txt").is_err() {
        println!("No previous history.");
    }*/

    'repl: loop {
        match rl.readline(&prompt.print()) {
            Ok(line) => {
                rl.add_history_entry(line.as_str());

                let cmd = CommandLine::new(&line);
                match cmd.run(&prompt.home, status) {
                    Ok(s) => status = s,
                    Err(e) => eprintln!("{}", e),
                }
            }
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                continue;
            }
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break 'repl;
            }
            Err(err) => {
                println!("Error: {:?}", err);
                break 'repl;
            }
        }
    }

    //rl.save_history("history.txt").unwrap();

    Ok(())
}