diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-05-13 09:31:56 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-05-13 09:31:56 -0400 |
commit | 95bc120687723a8c9e525b7448c5fd71235a329e (patch) | |
tree | 84a1e9fe847b366fa1beff8afaa7cba8865bac1c /src/commands/shell.rs | |
parent | ddb0669bbf0b5bb9e039094e58e69a5a81f13494 (diff) |
rework command parsing and add shell command
- This change will break compatibility with previous keymap.toml configs
- 'shell' command allows users to run a shell command inside joshuto
Diffstat (limited to 'src/commands/shell.rs')
-rw-r--r-- | src/commands/shell.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/commands/shell.rs b/src/commands/shell.rs new file mode 100644 index 0000000..19e901d --- /dev/null +++ b/src/commands/shell.rs @@ -0,0 +1,44 @@ +use std::process; + +use crate::commands::{self, JoshutoCommand, JoshutoRunnable}; +use crate::context::JoshutoContext; +use crate::error::JoshutoResult; +use crate::ui::widgets::TuiTextField; +use crate::ui::TuiBackend; + +#[derive(Clone, Debug)] +pub struct ShellCommand { + pub command: String, +} + +impl ShellCommand { + pub fn new(command: String) -> Self { + Self { command } + } + pub const fn command() -> &'static str { + "console" + } + + pub fn shell_command(command: &str) -> std::io::Result<()> { + let mut command = process::Command::new("sh").arg("-c").arg(command).spawn()?; + Ok(()) + } +} + +impl JoshutoCommand for ShellCommand {} + +impl std::fmt::Display for ShellCommand { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{}: sh -c '{}'", Self::command(), self.command) + } +} + +impl JoshutoRunnable for ShellCommand { + fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { + backend.terminal_drop(); + let res = Self::shell_command(self.command.as_str()); + backend.terminal_restore()?; + res?; + Ok(()) + } +} |