aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 24aa9b8cbe4cc37276ed8f3a5e97cf2e76070e0e (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
54
55
56
57
58
59
60
61
62
63
use rustyline::error::ReadlineError;
use rustyline::Editor;

mod builtins;
mod parser;
mod prompt;

fn main() {
    match std::fs::read_to_string("/etc/motd") {
        Ok(motd) => print!("{}", motd),
        Err(_) => {}
    }

    let prompt = prompt::Prompt::new().unwrap();
    let mut cwd = std::env::current_dir().unwrap();
    let mut status = None; //: std::process::ExitStatus;

    // `()` can be used when no completer is required
    let mut rl = Editor::<()>::new();
    /*if rl.load_history("history.txt").is_err() {
        println!("No previous history.");
    }*/
    'repl: loop {
        let readline = rl.readline(&prompt.print(&cwd));
        match readline {
            Ok(line) => {
                rl.add_history_entry(line.as_str());
                let commands = parser::parse(&line);
                match commands[0] {
                    "!" => println!("{:?}", status),
                    "cd" => match builtins::cd(&commands[1..], &prompt.home) {
                        Ok(p) => cwd = p,
                        Err(e) => eprintln!("Error: {}", e),
                    },
                    "exit" => {
                        break 'repl;
                    }
                    _ => match builtins::run(commands[0], &commands[1..]) {
                        Ok(s) => { 
                            println!("Ok({})", s);
                            status = Some(s);
                        },
                        Err(e) => println!("Err({})", e),
                    },
                }
                //println!("Line: {}", line);
            }
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                continue;
            }
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break;
            }
            Err(err) => {
                println!("Error: {:?}", err);
                break;
            }
        }
    }
    //rl.save_history("history.txt").unwrap();
}