Reformatted the command execution part

This commit is contained in:
Justine Pelletreau 2023-03-02 14:49:34 +01:00
parent 488c9c6e0a
commit 1eb1b239c2
2 changed files with 89 additions and 54 deletions

View File

@ -14,7 +14,13 @@ Sqish is now my default shell on my home computer, and I'll keep adding features
Also keep in mind I'm a beginner in Rust, so the code is pretty dirty; I will probably come around to reformating it once I have all the features I want and ironed out all the bugs. Also keep in mind I'm a beginner in Rust, so the code is pretty dirty; I will probably come around to reformating it once I have all the features I want and ironed out all the bugs.
TODO: # TODO
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 redirecting >> to the end of a file ?
* Allow && in commands ? * Allow && in commands ?
* Improve word jumping * Improve word jumping

View File

@ -14,7 +14,7 @@ pub mod shell {
use termion::input::TermRead; use termion::input::TermRead;
use termion::raw::IntoRawMode; use termion::raw::IntoRawMode;
use termion::raw::RawTerminal; use termion::raw::RawTerminal;
use termion::cursor; use termion::{color, cursor};
use termion; use termion;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use std::collections::HashMap; use std::collections::HashMap;
@ -33,7 +33,46 @@ pub mod shell {
extern crate shell_words; extern crate shell_words;
extern crate ctrlc; extern crate ctrlc;
fn change_dir(args: &Vec<String>, previous_command: &mut Option<Stdio>) { ///Stores essential terminal variables
struct TermElements {
//Stores currently typed command
command: String,
//Current cursor position in the line
cur_pos: usize,
//Max position of cursor in the line
max_pos: usize,
stdout: RawTerminal<Stdout>,
//Current history number
current_number: i32,
//Configuration
conf: SqishConf,
}
///Used to unify command output and error handling
///across functions
struct CmdOutput {
outp: String,
rc: i16,
}
impl CmdOutput {
fn from_values(output: String, rc: i16) -> CmdOutput {
let myoutp = CmdOutput {
outp: output,
rc: rc,
};
return myoutp;
}
fn new_empty() -> CmdOutput {
let outp = CmdOutput {
outp: String::new(),
rc: 0,
};
return outp;
}
}
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 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();
@ -45,14 +84,15 @@ pub mod shell {
let root = Path::new(&new_dir); let root = Path::new(&new_dir);
if let Err(e) = env::set_current_dir(&root) { if let Err(e) = env::set_current_dir(&root) {
eprintln!(" Err: {}", e); return Err(CmdOutput::from_values(
} else { format!("Error setting directory to {}, got {:?}",
println!(""); new_dir, e), -1));
} };
*previous_command = None; *previous_command = None;
return Ok(CmdOutput::new_empty());
} }
fn print_hist(previous_command: &mut Option<Stdio>, command_next: bool) { fn print_hist(previous_command: &mut Option<Stdio>, command_next: bool) -> Result<CmdOutput, CmdOutput> {
//let stdout = Stdio::inherit(); //let stdout = Stdio::inherit();
let res = get_history(); let res = get_history();
match res { match res {
@ -65,31 +105,19 @@ pub mod shell {
print!("{}", r); print!("{}", r);
} }
}, },
Err(e) => eprintln!(" Err reading history: {}", e), Err(e) => {
return Err(CmdOutput::from_values(format!("Could not print history, got {:?}", e), 255));
},
} }
return Ok(CmdOutput::new_empty());
} }
struct CmdOutput {
outp: String,
rc: i16,
}
impl CmdOutput {
fn from_values(output: String, rc: i16) -> CmdOutput {
let myoutp = CmdOutput {
outp: output,
rc: rc,
}
return myoutp;
}
}
fn process_line(input: &str) -> Result<CmdOutput, CmdOutput> { fn process_line(input: &str) -> Result<CmdOutput, CmdOutput> {
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();
let mut rc: i16 = 0;
let mut outp = CmdOutput::new_empty();
while let Some(command) = commands.next() { while let Some(command) = commands.next() {
let parts = match shell_words::split(&command.trim()) { let parts = match shell_words::split(&command.trim()) {
@ -103,11 +131,11 @@ pub mod shell {
match command { match command {
"cd" => { "cd" => {
change_dir(&args, &mut previous_command); change_dir(&args, &mut previous_command)?;
}, },
"history" => { "history" => {
let next = commands.peek().is_some(); let next = commands.peek().is_some();
print_hist(&mut previous_command, next); print_hist(&mut previous_command, next)?;
}, },
command => { command => {
if commands.peek().is_some() { if commands.peek().is_some() {
@ -145,7 +173,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 Err(CmdOutput::from_values(err_string, -1));
}, },
}; };
@ -159,12 +187,15 @@ pub mod shell {
previous_command = None; previous_command = None;
let format_res = format!("{}", let format_res = format!("{}",
status); status);
let _ = &mut resultat.push_str(&format_res); *&mut rc = format_res.parse::<i16>().unwrap();
*&mut resultat = String::from_utf8(output.stdout).unwrap();
//let _ = &mut resultat.push_str(&format_res);
} }
}, },
}; };
} }
return resultat; //return resultat;
return Ok(CmdOutput::from_values(resultat, rc));
} }
fn replace_signs(line: &String) -> String { fn replace_signs(line: &String) -> String {
@ -184,17 +215,18 @@ pub mod shell {
return ayo; return ayo;
} }
fn handle_input(line: &str) -> String { fn handle_input(line: &str) -> Result<CmdOutput, CmdOutput> {
//history callback //history callback
if (line.len() > 0) && (line.starts_with('!')) { if (line.len() > 0) && (line.starts_with('!')) {
let command_found = treat_history_callback(line); let command_found = treat_history_callback(line);
match command_found { match command_found {
Some(c) => { Some(c) => {
write_to_history(&c); write_to_history(&c);
let outp = process_line(&c); return process_line(&c);
return outp;
}, },
None => return String::from(" "), None => return Err(CmdOutput::from_values(
String::from("No such value"),
255)),
}; };
//Regular command //Regular command
} else if line.len() > 0 { } else if line.len() > 0 {
@ -203,7 +235,7 @@ pub mod shell {
//Nothing //Nothing
} else { } else {
//Command was empty, new line //Command was empty, new line
return String::from(""); return Ok(CmdOutput::new_empty());
} }
} }
@ -281,25 +313,36 @@ pub mod shell {
current_number: &mut i32, current_number: &mut i32,
conf: &mut SqishConf, conf: &mut SqishConf,
stdout: &mut RawTerminal<Stdout>) { stdout: &mut RawTerminal<Stdout>) {
//NORMAL CMD
if (mycommand != &String::from("")) && (mycommand != &String::from("exit")) { if (mycommand != &String::from("")) && (mycommand != &String::from("exit")) {
//Run the command
let comm = replace_signs(&mycommand); let comm = replace_signs(&mycommand);
RawTerminal::suspend_raw_mode(&stdout); RawTerminal::suspend_raw_mode(&stdout);
let res = handle_input(&comm); let res = handle_input(&comm);
//Display the results
match res {
Ok(_) => (),
Err(e) => {
println!("\r\n{}Got error {}, RC : {:?}{}",
color::Fg(color::Red),
e.outp,
e.rc,
color::Fg(color::Reset));
},
};
//Prepare for a new command
RawTerminal::activate_raw_mode(&stdout); RawTerminal::activate_raw_mode(&stdout);
mycommand.clear(); mycommand.clear();
for line in res.split("\r\n") {
if line != "" && line.starts_with('N'){
write!(stdout, "{}\r\n", line);
}
}
conf.update_prompt(get_curr_history_number()); conf.update_prompt(get_curr_history_number());
write!(stdout, "{}", conf.promptline).unwrap(); write!(stdout, "{}", conf.promptline).unwrap();
stdout.flush(); stdout.flush();
*current_number += 1; *current_number += 1;
//EXITTING
} else if mycommand == &String::from("exit") { } else if mycommand == &String::from("exit") {
write!(stdout, "\r\nThanks for using Sqish!\r\nSayonara \r\n"); write!(stdout, "\r\nThanks for using Sqish!\r\nSayonara \r\n");
RawTerminal::suspend_raw_mode(&stdout); RawTerminal::suspend_raw_mode(&stdout);
std::process::exit(0); std::process::exit(0);
//EMPTY LINE
} else { } else {
conf.update_prompt(get_curr_history_number()); conf.update_prompt(get_curr_history_number());
write!(stdout, "\r\n{}", conf.promptline).unwrap(); write!(stdout, "\r\n{}", conf.promptline).unwrap();
@ -535,20 +578,6 @@ pub mod shell {
write!(elems.stdout, "{}{}", elems.conf.promptline, elems.command); write!(elems.stdout, "{}{}", elems.conf.promptline, elems.command);
} }
struct TermElements {
//Stores currently typed command
command: String,
//Current cursor position in the line
cur_pos: usize,
//Max position of cursor in the line
max_pos: usize,
stdout: RawTerminal<Stdout>,
//Current history number
current_number: i32,
//Configuration
conf: SqishConf,
}
fn jmp_nxt_word(elems: &mut TermElements) { fn jmp_nxt_word(elems: &mut TermElements) {
let steps = find_next_space(&elems.cur_pos, &elems.max_pos, &elems.command); let steps = find_next_space(&elems.cur_pos, &elems.max_pos, &elems.command);
//println!("steps: {:?} cur {:?} max {:?}", steps, elems.cur_pos, elems.max_pos); //println!("steps: {:?} cur {:?} max {:?}", steps, elems.cur_pos, elems.max_pos);