diff options
| author | Fuwn <[email protected]> | 2021-05-05 16:34:45 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-05 16:34:45 +0000 |
| commit | 9100a7f61e641b9adb71dd3ed3d4aba7e583e182 (patch) | |
| tree | 10b2f4fe20ba0bbd4ed54b174502f359c2cc0ddf /src | |
| parent | feat(prompt): ability to specify prompt ps1 (diff) | |
| download | whirl-9100a7f61e641b9adb71dd3ed3d4aba7e583e182.tar.xz whirl-9100a7f61e641b9adb71dd3ed3d4aba7e583e182.zip | |
feat(prompt): null handling and help command
Diffstat (limited to 'src')
| -rw-r--r-- | src/prompt/builtins.rs | 18 | ||||
| -rw-r--r-- | src/prompt/mod.rs | 12 |
2 files changed, 25 insertions, 5 deletions
diff --git a/src/prompt/builtins.rs b/src/prompt/builtins.rs index dbbe3de..1c8389f 100644 --- a/src/prompt/builtins.rs +++ b/src/prompt/builtins.rs @@ -7,6 +7,8 @@ pub enum BuiltIn { Echo, History, Exit, + Null, + Help, } impl FromStr for BuiltIn { type Err = (); @@ -16,6 +18,8 @@ impl FromStr for BuiltIn { "echo" => Ok(BuiltIn::Echo), "history" => Ok(BuiltIn::History), "exit" => Ok(BuiltIn::Exit), + "null" => Ok(BuiltIn::Null), + "help" => Ok(BuiltIn::Help), _ => Err(()), } } @@ -27,8 +31,18 @@ pub fn builtin_echo(args: &[String]) -> i32 { } pub fn builtin_history(history: Vec<String>) -> i32 { - for cmd in history { - println!("{}", cmd.trim()); + let mut index = 0; + for cmd in &history { + index += 1; + println!("{} {}", index, cmd.trim()); } 0 } + +pub fn builtin_help() -> i32 { + println!( + "echo - display a line of text\nhistory - manipulate the history list\nexit - end the \ + application" + ); + 0 +} diff --git a/src/prompt/mod.rs b/src/prompt/mod.rs index d62db10..47a65e5 100644 --- a/src/prompt/mod.rs +++ b/src/prompt/mod.rs @@ -9,7 +9,7 @@ use std::{io, io::Write, str::FromStr}; use crate::{ config::Config, prompt::{ - builtins::{builtin_echo, builtin_history, BuiltIn}, + builtins::{builtin_echo, builtin_help, builtin_history, BuiltIn}, structure::Command, }, }; @@ -43,7 +43,11 @@ impl Prompt { .read_line(&mut input) .expect("failed to read command from stdin"); - self.history.push(input.clone()); + if input.len() > 2 { + self.history.push(input.clone()); + } else { + input = "null".to_string(); + } input } @@ -64,8 +68,10 @@ impl Prompt { Ok(BuiltIn::Echo) => builtin_echo(&c.args), Ok(BuiltIn::Exit) => std::process::exit(0), Ok(BuiltIn::History) => builtin_history(history), + Ok(BuiltIn::Null) => 0, + Ok(BuiltIn::Help) => builtin_help(), _ => { - println!("{}: command not found", &c.keyword); + println!("wsh: command not found: {}", &c.keyword); 1 } } |