diff options
| author | Fuwn <[email protected]> | 2021-05-20 12:18:09 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-20 12:18:09 +0000 |
| commit | e1fc776b8fabee233f63314bbf6f23e5ed33e8c4 (patch) | |
| tree | 95bddee0e884d9c1255a5b4bc616169985721c84 | |
| parent | perf(whirl_prompt): cut down on clones (diff) | |
| download | whirl-e1fc776b8fabee233f63314bbf6f23e5ed33e8c4.tar.xz whirl-e1fc776b8fabee233f63314bbf6f23e5ed33e8c4.zip | |
feat(whirl_prompt): make history builtin more unix like
Nothing that special, when the history builtin is run, the current command iteration is not appended
to the history list, but after the history builtin is run.
| -rw-r--r-- | whirl_prompt/src/builtins/mod.rs | 2 | ||||
| -rw-r--r-- | whirl_prompt/src/lib.rs | 16 |
2 files changed, 12 insertions, 6 deletions
diff --git a/whirl_prompt/src/builtins/mod.rs b/whirl_prompt/src/builtins/mod.rs index 1b94e90..0359443 100644 --- a/whirl_prompt/src/builtins/mod.rs +++ b/whirl_prompt/src/builtins/mod.rs @@ -15,7 +15,7 @@ pub fn builtin_echo(args: &[String]) -> i32 { 0 } -pub fn builtin_history(history: Vec<String>) -> i32 { +pub fn builtin_history(history: &[String]) -> i32 { for (index, cmd) in history.iter().enumerate() { println!("{} {}", index, cmd.trim()); } diff --git a/whirl_prompt/src/lib.rs b/whirl_prompt/src/lib.rs index 7ae0fda..900a1a7 100644 --- a/whirl_prompt/src/lib.rs +++ b/whirl_prompt/src/lib.rs @@ -62,9 +62,7 @@ impl Prompt { .read_line(&mut input) .expect("failed to read command from stdin"); - if input.len() > 2 { - self.history.push(input.clone()); - } else { + if input.len() <= 2 { input = "null".to_string(); } @@ -83,10 +81,10 @@ impl Prompt { // TODO: Find a way to make this access itself `history` doesn't have to be // passed everytime. async fn process_command(&mut self, c: Command) -> i32 { - match BuiltIn::from_str(&c.keyword) { + let exit_code = match BuiltIn::from_str(&c.keyword) { Ok(BuiltIn::Echo) => builtin_echo(&c.args), Ok(BuiltIn::Exit) => std::process::exit(0), - Ok(BuiltIn::History) => builtin_history(self.history.clone()), + Ok(BuiltIn::History) => builtin_history(&self.history), Ok(BuiltIn::Null) => 0, Ok(BuiltIn::Help) => builtin_help(), Ok(BuiltIn::Ls) => builtin_ls(), @@ -97,7 +95,15 @@ impl Prompt { println!("wsh: command not found: {}", &c.keyword); 1 } + }; + + if c.keyword != "null" { + self + .history + .push(format!("{} {}", &c.keyword, &c.args.join(" "))); } + + exit_code } } |