Some code reformat

This commit is contained in:
Justine 2023-03-01 00:37:05 +01:00
parent b5ef54c8f4
commit aec1641024
2 changed files with 79 additions and 59 deletions

View File

@ -15,6 +15,7 @@ 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.
TODO:
* Before anything : keep reformating the code. The main loop could use a struct containing current_pos, max_pos, etc.
* Allow redirecting >> to the end of a file ?
* Allow && in commands ?
* Improve word jumping

View File

@ -33,50 +33,62 @@ pub mod shell {
extern crate shell_words;
extern crate ctrlc;
fn change_dir(args: &Vec<String>, previous_command: &mut Option<Stdio>) {
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 mut new_dir = String::new();
if args.len() > 0 {
*&mut new_dir = args.join(" ");
} else {
*&mut new_dir = String::from(homedir.as_str());
}
fn process_line(input: &str) -> Result<String, shell_words::ParseError> {
let root = Path::new(&new_dir);
if let Err(e) = env::set_current_dir(&root) {
eprintln!(" Err: {}", e);
} else {
println!("");
}
*previous_command = None;
}
fn print_hist(previous_command: &mut Option<Stdio>, command_next: bool) {
//let stdout = Stdio::inherit();
let res = get_history();
match res {
Ok(r) => {
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let file = File::open(filepath).unwrap();
*previous_command = Some(Stdio::from(file));
//if !commands.peek().is_some() {
if !command_next {
print!("{}", r);
}
},
Err(e) => eprintln!(" Err reading history: {}", e),
}
}
fn process_line(input: &str) -> String {
let mut commands = input.trim().split("|").peekable();
let mut previous_command = None;
let mut resultat = String::new();
while let Some(command) = commands.next() {
let parts = shell_words::split(&command.trim())?;
let parts = match shell_words::split(&command.trim()) {
Ok(w) => w,
Err(e) => { return format!("Error parsing the command : {:?}", e); },
};
let command = parts[0].as_str();
let args = Vec::from(&parts[1..]);
match command {
"cd" => {
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 mut new_dir = String::new();
if args.len() > 0 {
*&mut new_dir = args.join(" ");
} else {
*&mut new_dir = String::from(homedir.as_str());
}
let root = Path::new(&new_dir);
if let Err(e) = env::set_current_dir(&root) {
eprintln!(" Err: {}", e);
} else {
println!("");
}
previous_command = None;
change_dir(&args, &mut previous_command);
},
"history" => {
//let stdout = Stdio::inherit();
let res = get_history();
match res {
Ok(r) => {
let filepath = dirs::home_dir().unwrap().join(".history.sqish");
let file = File::open(filepath).unwrap();
*&mut previous_command = Some(Stdio::from(file));
if !commands.peek().is_some() {
print!("{}", r);
}
},
Err(e) => eprintln!(" Err reading history: {}", e),
}
let next = commands.peek().is_some();
print_hist(&mut previous_command, next);
},
command => {
if commands.peek().is_some() {
@ -114,7 +126,7 @@ pub mod shell {
Ok(h) => h,
Err(_) => {
let err_string = format!("Not found : {}", command);
return Ok(String::from(err_string));
return String::from(err_string);
},
};
@ -133,7 +145,7 @@ pub mod shell {
},
};
}
return Ok(resultat);
return resultat;
}
fn replace_signs(line: &String) -> String {
@ -160,7 +172,7 @@ pub mod shell {
match command_found {
Some(c) => {
write_to_history(&c);
let outp = process_line(&c).unwrap();
let outp = process_line(&c);
return outp;
},
None => return String::from(" "),
@ -168,7 +180,7 @@ pub mod shell {
//Regular command
} else if line.len() > 0 {
write_to_history(line);
return process_line(line).unwrap();
return process_line(line);
//Nothing
} else {
//Command was empty, new line
@ -330,30 +342,8 @@ pub mod shell {
return max;
}
pub fn run_raw() {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
//RawTerminal::suspend_raw_mode(&stdout);
let mut mycommand = String::new();
//Used in conjunction with Up arrow to go back in history
//Resetted by pressing Enter
let mut current_number = get_curr_history_number();
//Used to track the location of the cursor inside the command
let mut current_pos: usize = 0;
let mut max_pos: usize = 0;
//Handle Ctrl+C
ctrlc::set_handler(|| {
();
}).unwrap();
let mut conf = match SqishConf::from_sqishrc() {
fn handle_conf(stdout: &mut Stdout) -> SqishConf {
let conf = match SqishConf::from_sqishrc() {
Ok(c) => c,
Err(e) => {
let conf = SqishConf {
@ -369,6 +359,35 @@ pub mod shell {
conf
},
};
return conf;
}
fn set_ctrlc() {
ctrlc::set_handler(|| {
();
}).unwrap();
}
pub fn run_raw() {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
//RawTerminal::suspend_raw_mode(&stdout);
let mut mycommand = String::new();
//Used in conjunction with Up arrow to go back in history
//Resetted by pressing Enter
let mut current_number = get_curr_history_number();
//Used to track the location of the cursor inside the command
let mut current_pos: usize = 0;
let mut max_pos: usize = 0;
//Get conf
let mut conf = handle_conf(&mut stdout);
//Handle Ctrl+C
set_ctrlc();
&conf.update_prompt(get_curr_history_number());
//Initializing