From 1c6a3107e9200fdc6441fb9c714a2e99d79c1293 Mon Sep 17 00:00:00 2001 From: El-BG-1970 Date: Tue, 26 Apr 2022 21:44:56 +0200 Subject: first commit --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 8 ++++++++ src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs 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) + } + } + } + } +} -- cgit v1.2.3