-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
56 lines (52 loc) · 1.86 KB
/
main.rs
File metadata and controls
56 lines (52 loc) · 1.86 KB
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
use std::{
env,
io::{self, stdin, stdout, Write},
path::Path,
};
fn main() {
loop {
print!(
"\x1b[1;31mtiny-shell\x1b[0m 🤏 in \x1b[1;34m{}\x1b[0m\n$ ",
env::current_dir().unwrap().display()
);
stdout().flush().unwrap();
let mut input = String::new();
if stdin().read_line(&mut input).unwrap() == 0 {
println!();
return;
}
let mut args = input.trim().split_ascii_whitespace();
let command = match args.next() {
Some(command) => command,
None => continue,
};
match command {
"help" => println!("The following commands are available: help, cd, clear, exit"),
"cd" => {
// todo revisse
let new_dir = args.peekable().peek().map_or("/", |x| *x);
let root = Path::new(new_dir);
if let Err(error) = env::set_current_dir(&root) {
eprintln!("{}", error);
}
}
"clear" => print!("\x1b[2J\x1b[1;1H"),
"exit" => return,
command => {
match std::process::Command::new(command).args(args).spawn() {
Ok(mut child) => match child.wait().unwrap().code() {
Some(0) => println!("✅"),
Some(code) => println!("❌ Child exited with status code: {}", code),
None => println!("Process terminated by signal"),
},
Err(error) => match error.kind() {
io::ErrorKind::NotFound => {
eprintln!("tiny-shell: {} command not found!", command)
}
_ => eprintln!("{}", error),
},
};
}
}
}
}