diff --git a/src/lib.rs b/src/lib.rs index 82c579e..57150ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #[allow(unused_must_use)] pub mod shell { + use regex::Regex; use std::io::Stdout; use std::process::Command; use std::io::stdin; @@ -412,6 +413,7 @@ pub mod shell { aliases: HashMap::new(), hotkeys: HashMap::new(), init: String::new(), + env: HashMap::new(), }; let ret_line = format!("Could not build conf, got {}\ \r\nUsing default promptline", e); @@ -636,6 +638,38 @@ pub mod shell { }; } + //I smell horrible code in here + fn set_envvars(conf: &SqishConf) { + let vars = conf.env.clone(); + let re = Regex::new(r"\$[A-Za-z_]+").unwrap(); + + for (key, value) in vars { + let mut after = value.clone(); + let mut nbr_of_vars = *&value.matches('$').count(); + while nbr_of_vars >= 1 { + let temp = after.clone(); + let caps = re.captures(&temp).unwrap(); + for cap in caps.iter() { + match cap { + Some(c) => { + let myvar = String::from(c.as_str()).replace('$', ""); + match env::var(myvar) { + Ok(r) => { + *&mut after = after.replace(c.as_str(), &r); + }, + Err(_) => continue, + }; + }, + None => continue, + }; + } + nbr_of_vars -= 1; + } + env::set_var(&key, &after); + } + } + + //THE ENTRYPOINT FUNCTION pub fn run_raw() { @@ -658,6 +692,7 @@ pub mod shell { //Initializing write!(elems.stdout, "\r\n ---Sqish initializing--- \r\n{}", elems.conf.promptline); elems.stdout.flush(); + set_envvars(&elems.conf); if elems.conf.init != String::from("") { run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout);