diff options
| author | Fuwn <[email protected]> | 2021-05-05 15:52:20 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-05 15:52:20 +0000 |
| commit | dbe104face7cdfa7e3c179030e5300b112cd3287 (patch) | |
| tree | 6f745f270622a965668aabbb3ed8f1b91d14fb90 /src | |
| parent | feat(global): add a shell-like prompt for interfacing with the server during ... (diff) | |
| download | whirl-dbe104face7cdfa7e3c179030e5300b112cd3287.tar.xz whirl-dbe104face7cdfa7e3c179030e5300b112cd3287.zip | |
feat(prompt): history builtin
Diffstat (limited to 'src')
| -rw-r--r-- | src/prompt/builtins.rs | 7 | ||||
| -rw-r--r-- | src/prompt/mod.rs | 30 | ||||
| -rw-r--r-- | src/subs.rs | 5 |
3 files changed, 31 insertions, 11 deletions
diff --git a/src/prompt/builtins.rs b/src/prompt/builtins.rs index 0ede1d3..dbbe3de 100644 --- a/src/prompt/builtins.rs +++ b/src/prompt/builtins.rs @@ -25,3 +25,10 @@ pub fn builtin_echo(args: &[String]) -> i32 { println!("{}", args.join(" ")); 0 } + +pub fn builtin_history(history: Vec<String>) -> i32 { + for cmd in history { + println!("{}", cmd.trim()); + } + 0 +} diff --git a/src/prompt/mod.rs b/src/prompt/mod.rs index a1329bf..2dccc17 100644 --- a/src/prompt/mod.rs +++ b/src/prompt/mod.rs @@ -7,15 +7,26 @@ mod structure; use std::{io, io::Write, str::FromStr}; use crate::prompt::{ - builtins::{builtin_echo, BuiltIn}, + builtins::{builtin_echo, builtin_history, BuiltIn}, structure::Command, }; -pub struct Prompt; +pub struct Prompt { + history: Vec<String>, +} impl Prompt { - pub fn handle() { - Prompt::write_prompt(); - Prompt::process_command(Prompt::tokenize_command(Prompt::read_command())); + pub fn handle() -> ! { + let mut prompt = Prompt { + history: vec![] + }; + + loop { + Prompt::write_prompt(); + Prompt::process_command( + Prompt::tokenize_command(prompt.read_command()), + prompt.history.clone(), + ); + } } fn write_prompt() { @@ -23,12 +34,14 @@ impl Prompt { io::stdout().flush().unwrap(); } - fn read_command() -> String { + fn read_command(&mut self) -> String { let mut input = String::new(); io::stdin() .read_line(&mut input) .expect("failed to read command from stdin"); + self.history.push(input.clone()); + input } @@ -41,10 +54,13 @@ impl Prompt { } } - fn process_command(c: Command) -> i32 { + // TODO: Find a way to make this access itself `history` doesn't have to be + // passed everytime. + fn process_command(c: Command, history: Vec<String>) -> i32 { 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(history), _ => { println!("{}: command not found", &c.keyword); 1 diff --git a/src/subs.rs b/src/subs.rs index 5e0d769..80c11d4 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -37,8 +37,5 @@ pub async fn run() -> ! { ]; std::thread::sleep(std::time::Duration::from_secs(2)); - loop { - // TODO: Find a way to keep this persistent on the bottom row. - Prompt::handle(); - } + Prompt::handle(); } |