From ce9836e9f96c9f91e0e2dc1a0f75e4bfd49aeac3 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 6 Aug 2023 01:25:40 +0800 Subject: [PATCH 1/2] feat: running shell --- app/src/executor.rs | 1 + config/docs/keymap.md | 4 ++++ config/preset/keymap.toml | 2 ++ core/src/event.rs | 4 ++-- core/src/manager/manager.rs | 37 ++++++++++++++++++++++++++----- core/src/tasks/workers/process.rs | 5 +++-- 6 files changed, 43 insertions(+), 10 deletions(-) diff --git a/app/src/executor.rs b/app/src/executor.rs index 40da2e698..f61293d58 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -102,6 +102,7 @@ impl Executor { } "create" => cx.manager.create(), "rename" => cx.manager.rename(), + "shell" => cx.manager.shell(exec.named.contains_key("block")), "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 747c9bce7..f695d822d 100644 --- a/config/docs/keymap.md +++ b/config/docs/keymap.md @@ -59,6 +59,10 @@ - create: Create a file or directory (ends with `/` for directory). - rename: Rename a file or directory. +- shell: Run a shell command. + + - `--block`: Block the ui until the command finishes. + - hidden: Set the visibility of hidden files. - `show`: Show hidden files. diff --git a/config/preset/keymap.toml b/config/preset/keymap.toml index 34ced965b..5ae1e1abb 100644 --- a/config/preset/keymap.toml +++ b/config/preset/keymap.toml @@ -45,6 +45,8 @@ keymap = [ { on = [ "D" ], exec = "remove --permanently" }, { on = [ "a" ], exec = "create" }, { on = [ "r" ], exec = "rename" }, + { on = [ ";" ], exec = "shell" }, + { on = [ ":" ], exec = "shell --block" }, { on = [ "." ], exec = "hidden toggle" }, { on = [ "s" ], exec = "search fd" }, { on = [ "S" ], exec = "search rg" }, diff --git a/core/src/event.rs b/core/src/event.rs index 698db6238..ef1246326 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, path::PathBuf}; +use std::{collections::BTreeMap, ffi::OsString, path::PathBuf}; use anyhow::Result; use config::{keymap::{Control, KeymapLayer}, open::Opener}; @@ -32,7 +32,7 @@ pub enum Event { Input(InputOpt, oneshot::Sender>), // Tasks - Open(Vec<(PathBuf, String)>, Option), + Open(Vec<(OsString, String)>, Option), Progress(u8, u32), } diff --git a/core/src/manager/manager.rs b/core/src/manager/manager.rs index d3aec58cb..ad191672d 100644 --- a/core/src/manager/manager.rs +++ b/core/src/manager/manager.rs @@ -1,7 +1,7 @@ use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, mem, path::PathBuf}; use anyhow::Error; -use config::OPEN; +use config::{open::Opener, OPEN}; use shared::MIME_DIR; use tokio::fs; @@ -126,7 +126,7 @@ impl Manager { .into_iter() .map(|f| { ( - f.path(), + f.path().into_os_string(), if f.meta.is_dir() { Some(MIME_DIR.to_owned()) } else { @@ -146,7 +146,7 @@ impl Manager { files = files .into_iter() .map(|(p, m)| { - let mime = m.or_else(|| mimes.remove(&p)); + let mime = m.or_else(|| mimes.remove(&PathBuf::from(&p))); (p, mime) }) .collect::>(); @@ -231,10 +231,30 @@ impl Manager { false } - fn bulk_rename(&self) -> bool { false } + pub fn bulk_rename(&self) -> bool { false } - pub fn selected(&self) -> Vec<&File> { - self.current().selected().or_else(|| self.hovered().map(|h| vec![h])).unwrap_or_default() + pub fn shell(&self, block: bool) -> bool { + tokio::spawn(async move { + let result = emit!(Input(InputOpt { + title: "Shell:".to_string(), + value: "".to_string(), + position: Position::Top, + })); + + if let Ok(cmd) = result.await { + emit!(Open( + vec![(cmd.into(), "".to_string())], + Some(Opener { + cmd: "sh".to_string(), + args: vec!["-c".to_string(), "$0".to_string()], + block, + spread: false, + }) + )); + } + }); + + false } pub fn update_read(&mut self, op: FilesOp) -> bool { @@ -352,4 +372,9 @@ impl Manager { #[inline] pub fn hovered(&self) -> Option<&File> { self.tabs.active().current.hovered.as_ref() } + + #[inline] + pub fn selected(&self) -> Vec<&File> { + self.current().selected().or_else(|| self.hovered().map(|h| vec![h])).unwrap_or_default() + } } diff --git a/core/src/tasks/workers/process.rs b/core/src/tasks/workers/process.rs index 12753ced9..19d4b1e7e 100644 --- a/core/src/tasks/workers/process.rs +++ b/core/src/tasks/workers/process.rs @@ -41,9 +41,10 @@ impl Process { trace!("Failed to spawn {}: {e}", task.cmd); } } - emit!(Stop(false)).await; - return Ok(()); + + self.sch.send(TaskOp::Adv(task.id, 1, 0))?; + return self.done(task.id); } self.sch.send(TaskOp::New(task.id, 0))?; From e408788c1a545fda83a3944a639e386a3226f440 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 6 Aug 2023 01:26:36 +0800 Subject: [PATCH 2/2] .. --- config/docs/keymap.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/docs/keymap.md b/config/docs/keymap.md index f695d822d..042ac98e3 100644 --- a/config/docs/keymap.md +++ b/config/docs/keymap.md @@ -42,7 +42,7 @@ - open: Open the selected files. - - `--interactive`: Open the selected files with an interactive ui to choose the opening method. + - `--interactive`: Open the selected files with an interactive UI to choose the opening method. - yank: Copy the selected files. @@ -61,7 +61,7 @@ - rename: Rename a file or directory. - shell: Run a shell command. - - `--block`: Block the ui until the command finishes. + - `--block`: Block the UI until the command finishes. - hidden: Set the visibility of hidden files.