Added history callback
This commit is contained in:
parent
8a2b1f7cce
commit
2cbe00a98b
@ -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...)
|
||||
|
28
src/main.rs
28
src/main.rs
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user