diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-19 21:21:33 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-19 21:21:33 -0400 |
commit | 7b4c611ed4d804bd52aeda0a619a54bea9b1e13d (patch) | |
tree | 7ba31fff7d5de171aadfc32c67f81dd64428b3ba /src/commands/command_line.rs | |
parent | 7741d4d2c8798ad0bb609e97fb3bda86c5318a36 (diff) |
Change command to use an enum instead of polymorphism
Diffstat (limited to 'src/commands/command_line.rs')
-rw-r--r-- | src/commands/command_line.rs | 64 |
1 files changed, 19 insertions, 45 deletions
diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs index 52a5925..0e7c3b2 100644 --- a/src/commands/command_line.rs +++ b/src/commands/command_line.rs @@ -1,54 +1,28 @@ -use crate::commands::{self, JoshutoCommand, JoshutoRunnable}; +use crate::commands::KeyCommand; use crate::context::JoshutoContext; use crate::error::JoshutoResult; use crate::ui::widgets::TuiTextField; use crate::ui::TuiBackend; -#[derive(Clone, Debug)] -pub struct CommandLine { - pub prefix: String, - pub suffix: String, -} - -impl CommandLine { - pub fn new(prefix: String, suffix: String) -> Self { - CommandLine { prefix, suffix } - } - pub const fn command() -> &'static str { - "console" - } - - pub fn readline( - &self, - context: &mut JoshutoContext, - backend: &mut TuiBackend, - ) -> JoshutoResult<()> { - let user_input: Option<String> = TuiTextField::default() - .prompt(":") - .prefix(self.prefix.as_str()) - .suffix(self.suffix.as_str()) - .get_input(backend, context); +use super::JoshutoRunnable; - if let Some(s) = user_input { - let trimmed = s.trim_start(); - let command = commands::parse_command(trimmed)?; - command.execute(context, backend) - } else { - Ok(()) - } - } -} - -impl JoshutoCommand for CommandLine {} - -impl std::fmt::Display for CommandLine { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}: {} {}", Self::command(), self.prefix, self.suffix) - } -} +pub fn readline( + context: &mut JoshutoContext, + backend: &mut TuiBackend, + prefix: &str, + suffix: &str, +) -> JoshutoResult<()> { + let user_input: Option<String> = TuiTextField::default() + .prompt(":") + .prefix(prefix) + .suffix(suffix) + .get_input(backend, context); -impl JoshutoRunnable for CommandLine { - fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { - self.readline(context, backend) + if let Some(s) = user_input { + let trimmed = s.trim_start(); + let command = KeyCommand::parse_command(trimmed)?; + command.execute(context, backend) + } else { + Ok(()) } } |