Added history callback

This commit is contained in:
Justine 2022-12-07 17:37:39 +01:00
parent 8a2b1f7cce
commit 2cbe00a98b
2 changed files with 26 additions and 5 deletions

View File

@ -16,11 +16,12 @@ Later, I can even make something useful out of it. For example, I could make it
* A basic history system. Writes to ~/history.rshell
* Tests for this history system
* Added the !<number> function
# Todo
* Add a function that gets the path of the history file, rather than copying it every time
* Other tests for the rest of the features
* Add a builtin history command
* Add a builtin !<number> like in bash
* Add a test for the treat_history_callback function
* Get some sleep
* Order the file (modules...)

View File

@ -149,18 +149,38 @@ fn get_hist_from_number(number: &i32) -> Option<String> {
}
}
fn treat_history_callback(line: &str) -> Option<String> {
let mut mystring = String::from(line);
mystring = mystring.trim().to_string();
let mut chars = mystring.chars();
//Skip the !
chars.next();
let mynbr: i32 = chars.as_str().parse().unwrap();
get_hist_from_number(&mynbr)
}
fn main(){
loop {
print!("> ");
stdout().flush();
//stdout().flush();
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
write_to_history(&input);
if process_line(input) {
return
if input.starts_with("!") {
let command_found = treat_history_callback(&input);
match command_found {
Some(c) => {
write_to_history(&c);
process_line(c);
},
None => ()
};
} else {
write_to_history(&input);
if process_line(input) {
return
}
}
}
}