Merge pull request 'dev' (#3) from dev into main

Reviewed-on: #3
This commit is contained in:
justine 2023-03-04 20:26:00 +01:00
commit 1eefd44a89
2 changed files with 28 additions and 10 deletions

View File

@ -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).

View File

@ -73,6 +73,21 @@ pub mod shell {
}
}
fn export_var(command: String) -> Result<CmdOutput, CmdOutput> {
let cmd_split = command.split('=').collect::<Vec<&str>>();
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<String>, previous_command: &mut Option<Stdio>) -> Result<CmdOutput, CmdOutput> {
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<String, String>) {
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);