diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-25 16:54:27 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-25 16:54:27 -0400 |
commit | 8c1077a04bcafc541c5685f1b9b4ea61707325b1 (patch) | |
tree | 13d3ac8359f27bc58f1cf026b8c5b3ce6e83819e /src/commands/command_line.rs | |
parent | 144f4c941a268d2e092acda3d14c93dfc643851f (diff) |
changed the way keymap commands are parsed
- add a command line command
Diffstat (limited to 'src/commands/command_line.rs')
-rw-r--r-- | src/commands/command_line.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs new file mode 100644 index 0000000..e6f490f --- /dev/null +++ b/src/commands/command_line.rs @@ -0,0 +1,84 @@ +use crate::commands::{self, JoshutoCommand, JoshutoRunnable}; +use crate::context::JoshutoContext; +use crate::error::JoshutoError; +use crate::textfield::JoshutoTextField; +use crate::ui; +use crate::window::JoshutoView; + +#[derive(Clone, Debug)] +pub struct CommandLine { + prefix: Option<String>, +} + +impl CommandLine { + pub fn new(prefix: Option<String>) -> Self { + CommandLine { prefix } + } + pub const fn command() -> &'static str { + "console" + } + + pub fn readline( + context: &mut JoshutoContext, + view: &JoshutoView, + prefix: Option<&String>, + ) -> Result<(), JoshutoError> { + const PROMPT: &str = ":"; + let (term_rows, term_cols) = ui::getmaxyx(); + let user_input: Option<String> = { + let textfield = JoshutoTextField::new( + 1, + term_cols, + (term_rows as usize - 1, 0), + PROMPT.to_string(), + ); + match prefix { + Some(s) => textfield.readline_with_initial((s, "")), + None => textfield.readline(), + } + }; + + if let Some(s) = user_input { + let trimmed = s.trim_start(); + match trimmed.find(' ') { + Some(ind) => { + let (command, xs) = trimmed.split_at(ind); + let xs = xs.trim_start(); + match commands::from_args(command, xs) { + Ok(s) => s.execute(context, view), + Err(e) => Err(JoshutoError::Keymap(e)), + } + } + None => match commands::from_args(trimmed, "") { + Ok(s) => s.execute(context, view), + Err(e) => Err(JoshutoError::Keymap(e)), + }, + } + } else { + Ok(()) + } + } +} + +impl JoshutoCommand for CommandLine {} + +impl std::fmt::Display for CommandLine { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self.prefix.as_ref() { + Some(s) => write!(f, "{}: {}", Self::command(), s), + None => write!(f, "{}", Self::command()), + } + } +} + +impl JoshutoRunnable for CommandLine { + fn execute( + &self, + context: &mut JoshutoContext, + view: &JoshutoView, + ) -> Result<(), JoshutoError> { + let res = Self::readline(context, view, self.prefix.as_ref()); + ncurses::doupdate(); + res + } +} |