From d22e34c8bdfac0a10ce025a52a0726869b211314 Mon Sep 17 00:00:00 2001 From: Justine Date: Thu, 23 Feb 2023 11:53:49 +0100 Subject: [PATCH] Now handling double quotes properly --- Cargo.toml | 1 + src/lib.rs | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 219c32b..206657f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ which = { version = "4.4.0", features = ["regex"] } regex = "1.7.1" yaml-rust = "0.4.5" ctrlc = "3.2.5" +shell-words = "1.1.0" diff --git a/src/lib.rs b/src/lib.rs index e91526d..044aee4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,24 +30,22 @@ pub mod shell { //used for home directory extern crate dirs; - + extern crate shell_words; extern crate ctrlc; - - fn process_line(input: &str) -> String { - let mut commands = input.trim().split(" | ").peekable(); + fn process_line(input: &str) -> Result { + let mut commands = input.trim().split("|").peekable(); let mut previous_command = None; let mut resultat = String::new(); while let Some(command) = commands.next() { - let mut parts = command.trim().split_whitespace(); - let command = parts.next().unwrap(); - let args = parts; + let parts = shell_words::split(&command.trim())?; + let command = parts[0].as_str(); + let args = Vec::from(&parts[1..]); match command { "cd" => { - let args = args.collect::>(); let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap(); //let new_dir = args.peekable().peek().map_or(homedir.as_str(), |x| *x); let mut new_dir = String::new(); @@ -116,7 +114,7 @@ pub mod shell { Ok(h) => h, Err(_) => { let err_string = format!("Not found : {}", command); - return String::from(err_string); + return Ok(String::from(err_string)); }, }; @@ -135,7 +133,7 @@ pub mod shell { }, }; } - return resultat; + return Ok(resultat); } fn replace_signs(line: &String) -> String { @@ -162,7 +160,7 @@ pub mod shell { match command_found { Some(c) => { write_to_history(&c); - let outp = process_line(&c); + let outp = process_line(&c).unwrap(); return outp; }, None => return String::from(" "), @@ -170,7 +168,7 @@ pub mod shell { //Regular command } else if line.len() > 0 { write_to_history(line); - return process_line(line); + return process_line(line).unwrap(); //Nothing } else { //Command was empty, new line @@ -374,7 +372,7 @@ pub mod shell { &conf.update_prompt(get_curr_history_number()); //Initializing - write!(stdout, "\r\n ---Sqish initializing...--- \r\n{}", conf.promptline); + write!(stdout, "\r\n ---Sqish initializing--- \r\n{}", conf.promptline); stdout.flush(); if conf.init != String::from("") { @@ -593,8 +591,10 @@ pub mod shell { append_prev_arg(&mut mycommand, &mut current_pos, &mut max_pos); }, '!' => { - let mut cmd = format!("\r\n-- ALIASES --> \r\n{:?}\r\n", conf.hotkeys); + let mut cmd = format!("\r\n-- HOTKEYS --> \r\n{:?}\r\n", conf.hotkeys); + cmd = format!("{}\r\n-- ALIASES -->\r\n{:?}\r\n", cmd, conf.aliases); cmd = cmd.replace(",", "\r\n"); + cmd = cmd.replace('"', ""); write!(stdout, "{}", cmd); },