grsh/src/main.rs

40 lines
989 B
Rust

use std::io::{self, stdout, Write};
use std::env;
use std::process::Command;
use std::path::Path;
fn main() {
loop {
print!("$ ");
io::stdout().flush();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let mut parts = input.trim().split_whitespace();
let command = parts.next().unwrap();
let args = parts;
match command {
"cd" => {
let new_dir = args.peekable().peek().map_or("/", |x| *x);
let root = Path::new(new_dir);
if let Err(e) = env::set_current_dir(&root) {
eprintln!("{}", e);
}
},
"exit" => return,
command => {
let mut child = Command::new(command)
.args(args)
.spawn();
match child {
Ok(mut child) => { child.wait(); },
Err(e) => eprintln!("{}", e),
};
}
}
}
}