aboutsummaryrefslogtreecommitdiffstats
path: root/src/run/builtin.rs
blob: feba7f971b1ae4ce8652970557d775cb8be63866 (plain) (blame)
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
use super::Builtin;
use crate::*;

pub struct Cd;
impl Builtin for Cd {
    fn name(&self) -> &str {
        "cd"
    }
    fn mod_session(&self, _: &mut Session, args: &[BString]) {
        let target: &Path = match args.get(0).map(|v| &v[..]) {
            Some(b"-") => todo!("prev"),
            Some(path) => OsStr::from_bytes(path).as_ref(),
            None => todo!("homedir"),
        };

        // TODO: let mod_session builtins return nonzero exit code
        let _ = std::env::set_current_dir(target);
    }
}

pub struct Clear;
impl Builtin for Clear {
    fn name(&self) -> &str {
        "clear"
    }
    fn mod_session(&self, _: &mut Session, _: &[BString]) {
        print!("\x1B[2J\x1B[1;1H");
    }
}

/// restart shell
pub struct Re;
impl Builtin for Re {
    fn name(&self) -> &str {
        "re"
    }

    fn mod_session(&self, session: &mut Session, _args: &[BString]) {
        session.raw.disable();
        let _ = Command::new("cargo").arg("run").status();
        session.raw.disable();
        std::process::exit(0);
    }
}


// TODO
// from" => todo!("read from file"),
// to" | b"into" => todo!("write into file"),
// append" => todo!("append to file"),