From 2cbe00a98bb74907e583bc8d65c21cfde69a29af Mon Sep 17 00:00:00 2001 From: Justine Date: Wed, 7 Dec 2022 17:37:39 +0100 Subject: [PATCH] Added history callback --- README.md | 3 ++- src/main.rs | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 85b4e71..b03bba0 100644 --- a/README.md +++ b/README.md @@ -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 ! 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 ! like in bash +* Add a test for the treat_history_callback function * Get some sleep * Order the file (modules...) diff --git a/src/main.rs b/src/main.rs index 74633cd..2fa7a2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,18 +149,38 @@ fn get_hist_from_number(number: &i32) -> Option { } } +fn treat_history_callback(line: &str) -> Option { + 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 + } } } }