From 967cec34af3505eb4b49fd45cafa747d03254191 Mon Sep 17 00:00:00 2001 From: Justine Date: Tue, 13 Dec 2022 23:42:39 +0100 Subject: [PATCH] Is a lib now --- Cargo.lock | 18 +-- Cargo.toml | 2 +- README.md | 9 +- src/lib.rs | 304 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 307 +--------------------------------------------------- 5 files changed, 320 insertions(+), 320 deletions(-) create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 1a6cc82..5b25ae6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "Rshell" -version = "0.1.0" -dependencies = [ - "dirs", - "gethostname", - "users", -] - [[package]] name = "bitflags" version = "1.3.2" @@ -117,6 +108,15 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rshell" +version = "0.1.0" +dependencies = [ + "dirs", + "gethostname", + "users", +] + [[package]] name = "syn" version = "1.0.105" diff --git a/Cargo.toml b/Cargo.toml index 46a0b51..1f8696b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "Rshell" +name = "rshell" version = "0.1.0" edition = "2021" diff --git a/README.md b/README.md index 9257bba..9056a65 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,10 @@ Later, I can even make something useful out of it. For example, I could make it * A basic history system. Writes to ~/history.rshell * Tests for this history system * Added the ! function - +* Order the file (modules...) => Ok, we doing a lib. + # Todo -* Add a function that gets the path of the history file, rather than copying it every time -* Other tests for the rest of the features -* Add a builtin history command -* Get some sleep -* Order the file (modules...) +* Use termion to capture the TAB and autocomplete # Annotated process_line ```rust diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..57669a6 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,304 @@ +pub mod shell { + use std::process::Command; + use std::io::stdin; + use std::io::stdout; + use std::io::Write; + use std::io::ErrorKind; + use std::path::Path; + use std::env; + use std::process::Child; + use std::process::Stdio; + use std::fs::File; + use std::fs::OpenOptions; + use std::fs; + use std::io::{self, prelude::*, BufReader}; + use users::{get_user_by_uid, get_current_uid}; + use gethostname::gethostname; + + //used for home directory + extern crate dirs; + + + fn process_line(input: String) -> bool { + + let mut commands = input.trim().split(" | ").peekable(); + let mut previous_command = None; + + while let Some(command) = commands.next() { + let mut parts = command.trim().split_whitespace(); + let mut 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); + } + + previous_command = None; + }, + "exit" => return true, + "history" => { + let stdout = Stdio::inherit(); + print_history(); + }, + command => { + let stdin = previous_command.map_or( + Stdio::inherit(), + |output: Child| Stdio::from(output.stdout.unwrap()) + ); + + let stdout = if commands.peek().is_some() { + Stdio::piped() + } else { + Stdio::inherit() + }; + + let output = Command::new(command) + .args(args) + .stdin(stdin) + .stdout(stdout) + .spawn(); + + match output { + Ok(output) => { previous_command = Some(output); }, + Err(e) => { + previous_command = None; + eprintln!("{}", e); + }, + }; + } + } + } + + if let Some(mut final_command) = previous_command { + // block until the final command has finished + final_command.wait(); + } + return false; + } + + fn write_to_history(command: &str) -> Result<(), std::io::Error> { + //Write a command to history + let filepath = dirs::home_dir().unwrap().join("history.rshell"); + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open(&filepath) + .unwrap(); + + let number = match get_history_number() { + Ok(n) => n + 1, + Err(_) => 1 + }; + + writeln!(file, "{} {}", number, command.trim())?; + return Ok(()) + } + + fn get_history_number() -> Result { + //Get the latest number found in history file + let filepath = dirs::home_dir().unwrap().join("history.rshell"); + let file = File::open(filepath)?; + let mut reader = BufReader::new(file).lines(); + let myline = String::from(reader.last().unwrap_or(Ok(String::from("1")))?); + let mut number = myline.split(' ').next().expect("???").parse().unwrap(); + return Ok(number); + } + + fn print_history() -> Result<(), std::io::Error> { + let filepath = dirs::home_dir().unwrap().join("history.rshell"); + let file = File::open(filepath)?; + let contents = BufReader::new(file).lines(); + for line in contents { + println!("{}", line.unwrap()); + } + return Ok(()); + } + + fn get_hist_from_number(number: &i32) -> Option { + //Returns a command from its number in hist file + let filepath = dirs::home_dir().unwrap().join("history.rshell"); + + if filepath.exists() == false { + return None; + } + + let file = File::open(filepath) + .expect("Error opening history file..."); + + if file.metadata().unwrap().len() < 1 { + return None; + } + + let mut reader = BufReader::new(file).lines(); + + for line in reader { + let current_line = line + .expect("Empty history file ? Please rm it"); + let mut s = current_line.split_whitespace(); + let n: i32 = s.next() + .expect("Error reading a line in hist file") + .parse() + .unwrap(); + let c = s.next().expect("No command!"); + if &n == number { + return Some(String::from(c)); + } + } + return None; + } + + fn treat_history_callback(line: &str) -> Option { + let mut mystring = String::from(line); + mystring = mystring.trim().to_string(); + let mut chars = mystring.chars(); + //Skip the ! + chars.next(); + let mynbr: i32 = chars.as_str().parse().unwrap(); + get_hist_from_number(&mynbr) + } + + fn build_prompt() -> String { + let user = String::from( + get_user_by_uid(get_current_uid()) + .unwrap() + .name() + .to_str() + .unwrap() + ); + + let host = String::from( + gethostname::gethostname() + .to_str() + .unwrap() + ); + + let current_dir = String::from( + env::current_dir() + .unwrap() + .to_str() + .unwrap() + ); + + let s = current_dir.split('/'); + let dir_last = String::from( + s.last() + .unwrap() + ); + + let prompt = String::from(format!("[{}@{} in {}] ", user, host, dir_last)); + return prompt; + } + + fn replace_signs(line: String) -> String { + let mut ayo = String::from(&line); + if ayo.contains('~') { + let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap(); + let result = str::replace(&ayo, "~", &homedir); + ayo = String::from(result); + } + + if ayo.contains("!!") { + let prev_comm = get_hist_from_number(&get_history_number().unwrap()).unwrap(); + let result = str::replace(&ayo, "!!", &prev_comm); + ayo = String::from(result); + } + + return ayo; + } + + pub fn run(){ + loop { + print!("{}", build_prompt()); + stdout().flush(); + let mut input = String::new(); + let bytes = stdin().read_line(&mut input).unwrap(); + input = replace_signs(input); + + //history callback + if (bytes > 0) && (input != String::from("\n")) && (input.starts_with('!')) { + let command_found = treat_history_callback(&input); + match command_found { + Some(c) => { + write_to_history(&c); + process_line(c); + }, + None => () + }; + //Regular command + } else if (bytes > 0) && (input != String::from("\n")) { + write_to_history(&input); + if process_line(input) { + return + } + //Nothing + } else { + //Command was empty, new line + continue; + } + } + } + + #[cfg(test)] + mod tests { + use super::*; + + fn inittests() { + let filepath = dirs::home_dir().unwrap().join("history.rshell"); + + let file = File::create(&filepath).expect("Could not create test history"); + writeln!(&file, "1 ls"); + writeln!(&file, "2 pwd"); + } + + #[test] + fn test_gethistnumber() { + inittests(); + let mynumber = get_history_number().unwrap(); + assert_eq!(mynumber, 2); + } + + #[test] + fn test_gethistfromnumber() { + inittests(); + let mycmd = get_hist_from_number(&1).unwrap(); + assert_eq!(mycmd, String::from("ls")); + } + + #[test] + fn test_history_callback() { + inittests(); + let expected = String::from("ls"); + assert_eq!(treat_history_callback("!1"), Some(expected)); + } + + #[test] + fn test_replace_signs() { + inittests(); + let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap(); + assert_eq!(replace_signs(String::from("ls ~ && echo !!")), String::from(format!("ls {} && echo pwd", homedir))); + } + + #[test] + fn test_writehistline() { + inittests(); + let towrite = String::from("pwd"); + write_to_history(&towrite); + + //Now check directly against the file + let filepath = dirs::home_dir().unwrap().join("history.rshell"); + let file = File::open(filepath).expect("Error"); + let mut reader = BufReader::new(file).lines(); + let myline = String::from(reader.last().unwrap().expect("Error")); + let expected = String::from("3 pwd"); + + assert_eq!(myline.trim(), expected.trim()); + } + } +} + + diff --git a/src/main.rs b/src/main.rs index 17df5ba..8f53b36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,307 +1,6 @@ -use std::process::Command; -use std::io::stdin; -use std::io::stdout; -use std::io::Write; -use std::io::ErrorKind; -use std::path::Path; -use std::env; -use std::process::Child; -use std::process::Stdio; -use std::fs::File; -use std::fs::OpenOptions; -use std::fs; -use std::io::{self, prelude::*, BufReader}; -use users::{get_user_by_uid, get_current_uid}; -use gethostname::gethostname; +pub use ::rshell::shell::run; -//used for home directory -extern crate dirs; - -fn process_line(input: String) -> bool { - - let mut commands = input.trim().split(" | ").peekable(); - let mut previous_command = None; - - while let Some(command) = commands.next() { - let mut parts = command.trim().split_whitespace(); - let mut 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); - } - - previous_command = None; - }, - "exit" => return true, - "history" => { - let stdout = Stdio::inherit(); - print_history(); - }, - command => { - let stdin = previous_command.map_or( - Stdio::inherit(), - |output: Child| Stdio::from(output.stdout.unwrap()) - ); - - let stdout = if commands.peek().is_some() { - Stdio::piped() - } else { - Stdio::inherit() - }; - - let output = Command::new(command) - .args(args) - .stdin(stdin) - .stdout(stdout) - .spawn(); - - match output { - Ok(output) => { previous_command = Some(output); }, - Err(e) => { - previous_command = None; - eprintln!("{}", e); - }, - }; - } - } - } - - if let Some(mut final_command) = previous_command { - // block until the final command has finished - final_command.wait(); - } - return false; +fn main() { + run(); } - -fn write_to_history(command: &str) -> Result<(), std::io::Error> { - //Write a command to history - let filepath = dirs::home_dir().unwrap().join("history.rshell"); - let mut file = OpenOptions::new() - .write(true) - .append(true) - .create(true) - .open(&filepath) - .unwrap(); - - let number = match get_history_number() { - Ok(n) => n + 1, - Err(_) => 1 - }; - - writeln!(file, "{} {}", number, command.trim())?; - return Ok(()) -} - -fn get_history_number() -> Result { - //Get the latest number found in history file - let filepath = dirs::home_dir().unwrap().join("history.rshell"); - let file = File::open(filepath)?; - let mut reader = BufReader::new(file).lines(); - let myline = String::from(reader.last().unwrap_or(Ok(String::from("1")))?); - let mut number = myline.split(' ').next().expect("???").parse().unwrap(); - return Ok(number); -} - -fn print_history() -> Result<(), std::io::Error> { - let filepath = dirs::home_dir().unwrap().join("history.rshell"); - let file = File::open(filepath)?; - let contents = BufReader::new(file).lines(); - for line in contents { - println!("{}", line.unwrap()); - } - return Ok(()); -} - -fn get_hist_from_number(number: &i32) -> Option { - //Returns a command from its number in hist file - let filepath = dirs::home_dir().unwrap().join("history.rshell"); - - if filepath.exists() == false { - return None; - } - - let file = File::open(filepath) - .expect("Error opening history file..."); - - if file.metadata().unwrap().len() < 1 { - return None; - } - - let mut reader = BufReader::new(file).lines(); - - for line in reader { - let current_line = line - .expect("Empty history file ? Please rm it"); - let mut s = current_line.split_whitespace(); - let n: i32 = s.next() - .expect("Error reading a line in hist file") - .parse() - .unwrap(); - let c = s.next().expect("No command!"); - if &n == number { - return Some(String::from(c)); - } - } - return None; -} - -fn treat_history_callback(line: &str) -> Option { - let mut mystring = String::from(line); - mystring = mystring.trim().to_string(); - let mut chars = mystring.chars(); - //Skip the ! - chars.next(); - let mynbr: i32 = chars.as_str().parse().unwrap(); - get_hist_from_number(&mynbr) -} - -fn build_prompt() -> String { - let user = String::from( - get_user_by_uid(get_current_uid()) - .unwrap() - .name() - .to_str() - .unwrap() - ); - - let host = String::from( - gethostname::gethostname() - .to_str() - .unwrap() - ); - - let current_dir = String::from( - env::current_dir() - .unwrap() - .to_str() - .unwrap() - ); - - let s = current_dir.split('/'); - let dir_last = String::from( - s.last() - .unwrap() - ); - - let prompt = String::from(format!("[{}@{} in {}] ", user, host, dir_last)); - return prompt; -} - -fn replace_signs(line: String) -> String { - let mut ayo = String::from(&line); - if ayo.contains('~') { - let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap(); - let result = str::replace(&ayo, "~", &homedir); - ayo = String::from(result); - } - - if ayo.contains("!!") { - let prev_comm = get_hist_from_number(&get_history_number().unwrap()).unwrap(); - let result = str::replace(&ayo, "!!", &prev_comm); - ayo = String::from(result); - } - - return ayo; -} - -fn main(){ - loop { - print!("{}", build_prompt()); - stdout().flush(); - let mut input = String::new(); - let bytes = stdin().read_line(&mut input).unwrap(); - input = replace_signs(input); - - //history callback - if (bytes > 0) && (input != String::from("\n")) && (input.starts_with('!')) { - let command_found = treat_history_callback(&input); - match command_found { - Some(c) => { - write_to_history(&c); - process_line(c); - }, - None => () - }; - //Regular command - } else if (bytes > 0) && (input != String::from("\n")) { - write_to_history(&input); - if process_line(input) { - return - } - //Nothing - } else { - //Command was empty, new line - continue; - } - } -} - - - -#[cfg(test)] -mod tests { - use super::*; - - fn inittests() { - let filepath = dirs::home_dir().unwrap().join("history.rshell"); - - let file = File::create(&filepath).expect("Could not create test history"); - writeln!(&file, "1 ls"); - writeln!(&file, "2 pwd"); - } - - #[test] - fn test_gethistnumber() { - inittests(); - let mynumber = get_history_number().unwrap(); - assert_eq!(mynumber, 2); - } - - #[test] - fn test_gethistfromnumber() { - inittests(); - let mycmd = get_hist_from_number(&1).unwrap(); - assert_eq!(mycmd, String::from("ls")); - } - - #[test] - fn test_history_callback() { - inittests(); - let expected = String::from("ls"); - assert_eq!(treat_history_callback("!1"), Some(expected)); - } - - #[test] - fn test_replace_signs() { - inittests(); - let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap(); - assert_eq!(replace_signs(String::from("ls ~ && echo !!")), String::from(format!("ls {} && echo pwd", homedir))); - } - - #[test] - fn test_writehistline() { - inittests(); - let towrite = String::from("pwd"); - write_to_history(&towrite); - - //Now check directly against the file - let filepath = dirs::home_dir().unwrap().join("history.rshell"); - let file = File::open(filepath).expect("Error"); - let mut reader = BufReader::new(file).lines(); - let myline = String::from(reader.last().unwrap().expect("Error")); - let expected = String::from("3 pwd"); - - assert_eq!(myline.trim(), expected.trim()); - } - - -} - - -