commit 8439057884c83309d740ca47bdf159d676d9dec3 Author: g1n Date: Mon Jul 19 11:59:20 2021 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6b64b6a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "grsh" +version = "0.1.0" +authors = ["g1n "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a3029ac --- /dev/null +++ b/src/main.rs @@ -0,0 +1,39 @@ +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), + }; + } + } + } +}