Fleche du haut marche (plus ou moins)

This commit is contained in:
Justine 2022-12-20 19:02:26 +01:00
parent 09806c7463
commit addf71edd2

View File

@ -97,7 +97,7 @@ pub mod shell {
fn write_to_history(command: &str) -> Result<(), std::io::Error> {
//Write a command to history
let filepath = dirs::home_dir().unwrap().join("history.rshell");
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let mut file = OpenOptions::new()
.write(true)
.append(true)
@ -116,7 +116,7 @@ pub mod shell {
fn get_history_number() -> Result<i32, std::io::Error> {
//Get the latest number found in history file
let filepath = dirs::home_dir().unwrap().join("history.rshell");
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let file = File::open(filepath)?;
let mut reader = BufReader::new(file).lines();
let myline = String::from(reader.last().unwrap_or(Ok(String::from("1")))?);
@ -125,7 +125,7 @@ pub mod shell {
}
fn get_history() -> Result<String, std::io::Error> {
let filepath = dirs::home_dir().unwrap().join("history.rshell");
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let file = File::open(filepath)?;
let contents = BufReader::new(file).lines();
let mut res = String::new();
@ -138,7 +138,7 @@ pub mod shell {
fn get_hist_from_number(number: &i32) -> Option<String> {
//Returns a command from its number in hist file
let filepath = dirs::home_dir().unwrap().join("history.rshell");
let filepath = dirs::home_dir().unwrap().join("history.sqish");
if filepath.exists() == false {
return None;
@ -261,7 +261,7 @@ pub mod shell {
pub fn run_raw() {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
let prompt = build_prompt();
let mut prompt = build_prompt();
let mut mycommand = String::new();
//Initialize
@ -288,9 +288,11 @@ pub mod shell {
for line in res.split('\n') {
write!(stdout, "\r\n{}", line);
}
write!(stdout, "\r\n{}", prompt).unwrap();
prompt = build_prompt();
write!(stdout, "{}", prompt).unwrap();
stdout.flush();
} else if mycommand == String::from("exit") {
write!(stdout, "\r\n Sayonara \r\n");
break;
} else {
write!(stdout, "\r\n{}", prompt).unwrap();
@ -298,7 +300,10 @@ pub mod shell {
}
},
Key::Ctrl('q') => break,
Key::Ctrl('q') => {
writeln!(stdout, "\r\n Sayonara \r\n");
break;
},
Key::Char(C) => {
mycommand.push(C);
@ -317,6 +322,20 @@ pub mod shell {
stdout.flush();
},
Key::Up => {
let lastnumber = match get_history_number() {
Ok(e) => e,
Err(_) => continue,
};
let lastcmd = match get_hist_from_number(&lastnumber) {
Some(c) => c,
None => continue
};
let prompt = build_prompt();
write!(stdout, "\r\n{}{}", prompt, lastcmd.trim());
mycommand = lastcmd;
},
_ => (),
}
@ -325,56 +344,13 @@ pub mod shell {
}
// pub fn run(){
// loop {
// print!("{}", build_prompt());
// stdout().flush();
// let mut input = String::new();
// let bytes = stdin().read_line(&mut input).unwrap();
// input = replace_signs(input);
//
// //history callback
// if (bytes > 0) && (input != String::from("\n")) && (input.starts_with('!')) {
// let command_found = treat_history_callback(&input);
// match command_found {
// Some(c) => {
// write_to_history(&c);
// process_line(&c);
// },
// None => ()
// };
// //Regular command
// } else if (bytes > 0) && (input != String::from("\n")) {
// write_to_history(&input);
// if process_line(input) {
// return
// }
// //Nothing
// } else {
// //Command was empty, new line
// continue;
// }
// }
// }
#[cfg(test)]
mod tests {
use super::*;
fn inittests() {
let filepath = dirs::home_dir().unwrap().join("history.rshell");
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let file = File::create(&filepath).expect("Could not create test history");
writeln!(&file, "1 ls");
@ -416,7 +392,7 @@ pub mod shell {
write_to_history(&towrite);
//Now check directly against the file
let filepath = dirs::home_dir().unwrap().join("history.rshell");
let filepath = dirs::home_dir().unwrap().join("history.sqish");
let file = File::open(filepath).expect("Error");
let mut reader = BufReader::new(file).lines();
let myline = String::from(reader.last().unwrap().expect("Error"));