Now handling double quotes properly

This commit is contained in:
Justine 2023-02-23 11:53:49 +01:00
parent 6f63294857
commit d22e34c8bd
2 changed files with 15 additions and 14 deletions

View File

@ -17,3 +17,4 @@ which = { version = "4.4.0", features = ["regex"] }
regex = "1.7.1" regex = "1.7.1"
yaml-rust = "0.4.5" yaml-rust = "0.4.5"
ctrlc = "3.2.5" ctrlc = "3.2.5"
shell-words = "1.1.0"

View File

@ -30,24 +30,22 @@ pub mod shell {
//used for home directory //used for home directory
extern crate dirs; extern crate dirs;
extern crate shell_words;
extern crate ctrlc; extern crate ctrlc;
fn process_line(input: &str) -> Result<String, shell_words::ParseError> {
fn process_line(input: &str) -> String { let mut commands = input.trim().split("|").peekable();
let mut commands = input.trim().split(" | ").peekable();
let mut previous_command = None; let mut previous_command = None;
let mut resultat = String::new(); let mut resultat = String::new();
while let Some(command) = commands.next() { while let Some(command) = commands.next() {
let mut parts = command.trim().split_whitespace(); let parts = shell_words::split(&command.trim())?;
let command = parts.next().unwrap(); let command = parts[0].as_str();
let args = parts; let args = Vec::from(&parts[1..]);
match command { match command {
"cd" => { "cd" => {
let args = args.collect::<Vec<&str>>();
let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap(); 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 new_dir = args.peekable().peek().map_or(homedir.as_str(), |x| *x);
let mut new_dir = String::new(); let mut new_dir = String::new();
@ -116,7 +114,7 @@ pub mod shell {
Ok(h) => h, Ok(h) => h,
Err(_) => { Err(_) => {
let err_string = format!("Not found : {}", command); 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 { fn replace_signs(line: &String) -> String {
@ -162,7 +160,7 @@ pub mod shell {
match command_found { match command_found {
Some(c) => { Some(c) => {
write_to_history(&c); write_to_history(&c);
let outp = process_line(&c); let outp = process_line(&c).unwrap();
return outp; return outp;
}, },
None => return String::from(" "), None => return String::from(" "),
@ -170,7 +168,7 @@ pub mod shell {
//Regular command //Regular command
} else if line.len() > 0 { } else if line.len() > 0 {
write_to_history(line); write_to_history(line);
return process_line(line); return process_line(line).unwrap();
//Nothing //Nothing
} else { } else {
//Command was empty, new line //Command was empty, new line
@ -374,7 +372,7 @@ pub mod shell {
&conf.update_prompt(get_curr_history_number()); &conf.update_prompt(get_curr_history_number());
//Initializing //Initializing
write!(stdout, "\r\n ---Sqish initializing...--- \r\n{}", conf.promptline); write!(stdout, "\r\n ---Sqish initializing--- \r\n{}", conf.promptline);
stdout.flush(); stdout.flush();
if conf.init != String::from("") { if conf.init != String::from("") {
@ -593,8 +591,10 @@ pub mod shell {
append_prev_arg(&mut mycommand, &mut current_pos, &mut max_pos); 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(",", "\r\n");
cmd = cmd.replace('"', "");
write!(stdout, "{}", cmd); write!(stdout, "{}", cmd);
}, },