diff --git a/README.md b/README.md index 8101784..20d7815 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,9 @@ Also keep in mind I'm a beginner in Rust, so the code is pretty dirty; I will pr TODO (Reformat): * Creating a struct "shell" that contains builder method taking an input and an output as parameters as well as a conf could be cool -* Maybe subdivide lib.src to have more modularity 'cause it getting kinda long... 500 lines it a bit much ? -TODO(Features): -* Allow redirecting >> to the end of a file ? -* Allow && in commands ? -* Improve word jumping -* Add arguments : run a file (sqish myexec) which I believe would make it compatible with gdb ? -* Add export command => Reuse the function set_envvars +TODO (features): +* Add a && ## sqishrc (Config) See the included sqishrc file included for comments, and copy it as ~/.sqishrc for use (optionnal). diff --git a/src/lib.rs b/src/lib.rs index 57150ba..c2a8891 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,6 +73,21 @@ pub mod shell { } } + fn export_var(command: String) -> Result { + let cmd_split = command.split('=').collect::>(); + if cmd_split.len() < 2 { + return Err(CmdOutput::from_values( + String::from("Please a format such as : VAR=value"), + 255)); + } + let key = String::from(cmd_split[0].to_uppercase()); + let val = String::from(cmd_split[1]); + let mut map = HashMap::new(); + map.insert(key, val); + set_envvars(&map); + return Ok(CmdOutput::new_empty()); + } + fn change_dir(args: &Vec, previous_command: &mut Option) -> Result { 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); @@ -127,17 +142,25 @@ pub mod shell { return Err(CmdOutput::from_values(format!("Could not parse command {:?}", e), -1)); }, }; + if parts.len() < 1 { + return Err(CmdOutput::from_values(String::from("Could not parse command"), -1)); + } let command = parts[0].as_str(); let args = Vec::from(&parts[1..]); match command { "cd" => { change_dir(&args, &mut previous_command)?; + print!("\r\n"); }, "history" => { let next = commands.peek().is_some(); print_hist(&mut previous_command, next)?; }, + "export" => { + export_var(args.join(" "))?; + print!("\r\n"); + }, command => { if commands.peek().is_some() { let stdin = match previous_command { @@ -639,8 +662,8 @@ pub mod shell { } //I smell horrible code in here - fn set_envvars(conf: &SqishConf) { - let vars = conf.env.clone(); + fn set_envvars(vars: &HashMap) { + let vars = vars.clone(); let re = Regex::new(r"\$[A-Za-z_]+").unwrap(); for (key, value) in vars { @@ -692,7 +715,7 @@ pub mod shell { //Initializing write!(elems.stdout, "\r\n ---Sqish initializing--- \r\n{}", elems.conf.promptline); elems.stdout.flush(); - set_envvars(&elems.conf); + set_envvars(&elems.conf.env); if elems.conf.init != String::from("") { run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout);