From 0da93562c9b227ae814366a55a45763127926ddd Mon Sep 17 00:00:00 2001 From: fzdwx Date: Sat, 12 Aug 2023 23:57:35 +0800 Subject: [PATCH] feat: add command template support for `shell` (#48) --- app/src/executor.rs | 6 +++++- config/docs/keymap.md | 2 ++ core/src/manager/manager.rs | 21 +++++++++++++-------- core/src/tasks/scheduler.rs | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/src/executor.rs b/app/src/executor.rs index 038c67a1..4adac290 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -106,7 +106,11 @@ impl Executor { } "create" => cx.manager.create(), "rename" => cx.manager.rename(), - "shell" => cx.manager.shell(exec.named.contains_key("block")), + "shell" => cx.manager.shell( + exec.args.get(0).map(|e| e.as_str()).unwrap_or(""), + exec.named.contains_key("block"), + exec.named.contains_key("confirm"), + ), "hidden" => cx.manager.current_mut().hidden(match exec.args.get(0).map(|s| s.as_str()) { Some("show") => Some(true), Some("hide") => Some(false), diff --git a/config/docs/keymap.md b/config/docs/keymap.md index 4a12bc9c..b3dad095 100644 --- a/config/docs/keymap.md +++ b/config/docs/keymap.md @@ -62,7 +62,9 @@ - rename: Rename a file or directory. - shell: Run a shell command. + - `exec`: Optional, command template to be run. - `--block`: Block the UI until the command finishes. + - `--confirm`: When the template is provided, run it directly, no input UI was shown. - hidden: Set the visibility of hidden files. diff --git a/core/src/manager/manager.rs b/core/src/manager/manager.rs index 6660f0d4..9a55ef1a 100644 --- a/core/src/manager/manager.rs +++ b/core/src/manager/manager.rs @@ -225,16 +225,21 @@ impl Manager { pub fn bulk_rename(&self) -> bool { false } - pub fn shell(&self, block: bool) -> bool { + pub fn shell(&self, exec: &str, block: bool, confirm: bool) -> bool { + let mut exec = exec.to_owned(); tokio::spawn(async move { - let result = emit!(Input(InputOpt::top("Shell:").with_highlight())); - - if let Ok(exec) = result.await { - emit!(Open( - Default::default(), - Some(Opener { exec, block, display_name: Default::default(), spread: true }) - )); + if !confirm || exec.is_empty() { + let result = emit!(Input(InputOpt::top("Shell:").with_value(&exec).with_highlight())); + match result.await { + Ok(e) => exec = e, + Err(_) => return, + } } + + emit!(Open( + Default::default(), + Some(Opener { exec, block, display_name: Default::default(), spread: true }) + )); }); false diff --git a/core/src/tasks/scheduler.rs b/core/src/tasks/scheduler.rs index ae12a259..e00dd537 100644 --- a/core/src/tasks/scheduler.rs +++ b/core/src/tasks/scheduler.rs @@ -302,7 +302,7 @@ impl Scheduler { }) }); - let args = args.into_iter().map(|a| a.as_ref().to_os_string()).collect::>(); + let args = args.iter().map(|a| a.as_ref().to_os_string()).collect::>(); tokio::spawn({ let process = self.process.clone(); let opener = opener.clone();