diff options
| author | El-BG-1970 <elouangros@hotmail.com> | 2022-04-26 21:44:56 +0200 |
|---|---|---|
| committer | El-BG-1970 <elouangros@hotmail.com> | 2022-04-26 21:44:56 +0200 |
| commit | 1c6a3107e9200fdc6441fb9c714a2e99d79c1293 (patch) | |
| tree | 546cdac9a15dd2fb3710ccfe1c98f58615c97643 | |
| download | rush-1c6a3107e9200fdc6441fb9c714a2e99d79c1293.tar.gz | |
first commit
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.lock | 7 | ||||
| -rw-r--r-- | Cargo.toml | 8 | ||||
| -rw-r--r-- | src/main.rs | 55 |
4 files changed, 71 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..71f3fed --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rush" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..dee5fca --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rush" +version = "0.1.0" +edition = "2021" + +# 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..31133da --- /dev/null +++ b/src/main.rs @@ -0,0 +1,55 @@ +use std::io::{self,stdin,stdout,Write}; +use std::str::FromStr; +use std::process::{Command}; +use std::env; +use std::path::Path; + +fn substitute_vars(word: &str) -> String { + match word { + "~" => env::var("HOME").unwrap(), + w if w.chars().nth(0) == Some('$') => String::from_str(w).unwrap(), + w => String::from_str(w).unwrap() + } +} + +fn main() -> io::Result<()> { + loop { + stdout().write_all(b"> ")?; + stdout().flush()?; + + let mut line = String::new(); + stdin().read_line(&mut line).unwrap(); + + let mut parts = line.trim().split_whitespace(); + let command = parts.next().unwrap(); + + match command { + "exit" => return Ok(()), + "cd" => { + // change directory + match parts.next() { + None => { + let home = env::var("HOME").unwrap(); + let cd = Path::new(home.as_str()); + env::set_current_dir(&cd)?; + }, + Some(p) => { + let tmp = substitute_vars(p); + let cd = Path::new(&tmp); + env::set_current_dir(&cd)?; + } + }; + }, + command => { + let child = Command::new(command) + .args(parts.map(substitute_vars)) + .spawn(); + + match child { + Ok(mut child) => { child.wait()?; }, + Err(e) => eprintln!("{}", e) + } + } + } + } +} |
