From 9e4c70138c42ffd2e651afd85c0e6d30d318d52c Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 12 Mar 2024 18:43:21 +0800 Subject: [PATCH] .. --- yazi-core/src/manager/commands/open.rs | 2 +- yazi-core/src/tasks/commands/mod.rs | 1 + yazi-core/src/tasks/commands/open_with.rs | 2 +- yazi-core/src/tasks/commands/process_exec.rs | 12 ++++++++ yazi-core/src/tasks/mod.rs | 1 + yazi-core/src/tasks/process.rs | 30 ++++++++++++++++++++ yazi-core/src/tasks/tasks.rs | 22 -------------- yazi-proxy/src/options/mod.rs | 2 ++ yazi-proxy/src/options/process.rs | 18 ++++++++++++ 9 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 yazi-core/src/tasks/commands/process_exec.rs create mode 100644 yazi-core/src/tasks/process.rs create mode 100644 yazi-proxy/src/options/process.rs diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index e4d1862a..8c1eaca5 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -84,7 +84,7 @@ impl Manager { if targets.is_empty() { return; } else if !opt.interactive { - return tasks.file_open(&opt.hovered, &targets); + return tasks.process_from_files(&opt.hovered, &targets); } let openers: Vec<_> = OPEN.common_openers(&targets).into_iter().cloned().collect(); diff --git a/yazi-core/src/tasks/commands/mod.rs b/yazi-core/src/tasks/commands/mod.rs index 0fce8d2b..9456f9da 100644 --- a/yazi-core/src/tasks/commands/mod.rs +++ b/yazi-core/src/tasks/commands/mod.rs @@ -2,4 +2,5 @@ mod arrow; mod cancel; mod inspect; mod open_with; +mod process_exec; mod toggle; diff --git a/yazi-core/src/tasks/commands/open_with.rs b/yazi-core/src/tasks/commands/open_with.rs index ff874532..aaff5b3f 100644 --- a/yazi-core/src/tasks/commands/open_with.rs +++ b/yazi-core/src/tasks/commands/open_with.rs @@ -5,7 +5,7 @@ use crate::tasks::Tasks; impl Tasks { pub fn open_with(&mut self, opt: impl TryInto) { if let Ok(opt) = opt.try_into() { - self.file_open_with(&opt.opener, &opt.targets); + self.process_from_opener(&opt.opener, &opt.targets); } } } diff --git a/yazi-core/src/tasks/commands/process_exec.rs b/yazi-core/src/tasks/commands/process_exec.rs new file mode 100644 index 00000000..94676e6a --- /dev/null +++ b/yazi-core/src/tasks/commands/process_exec.rs @@ -0,0 +1,12 @@ +use yazi_proxy::options::ProcessExecOpt; + +use crate::tasks::Tasks; + +impl Tasks { + pub fn process_exec(&mut self, opt: impl TryInto) { + if let Ok(opt) = opt.try_into() { + // FIXME + // self.process_from_opener(&opt.opener, &opt.targets); + } + } +} diff --git a/yazi-core/src/tasks/mod.rs b/yazi-core/src/tasks/mod.rs index d8475836..df6e5caa 100644 --- a/yazi-core/src/tasks/mod.rs +++ b/yazi-core/src/tasks/mod.rs @@ -1,4 +1,5 @@ mod commands; +mod process; mod progress; mod tasks; diff --git a/yazi-core/src/tasks/process.rs b/yazi-core/src/tasks/process.rs new file mode 100644 index 00000000..f3b65eac --- /dev/null +++ b/yazi-core/src/tasks/process.rs @@ -0,0 +1,30 @@ +use std::{collections::HashMap, ffi::OsStr}; + +use yazi_config::{open::Opener, OPEN}; +use yazi_shared::fs::Url; + +use super::Tasks; + +impl Tasks { + pub fn process_from_files(&self, hovered: &Url, targets: &[(Url, String)]) { + let mut openers = HashMap::new(); + for (url, mime) in targets { + if let Some(opener) = OPEN.openers(url, mime).and_then(|o| o.first().copied()) { + openers.entry(opener).or_insert_with(|| vec![hovered]).push(url); + } + } + for (opener, args) in openers { + self.process_from_opener(opener, &args); + } + } + + pub fn process_from_opener(&self, opener: &Opener, args: &[impl AsRef]) { + if opener.spread { + self.scheduler.process_open(opener, args); + return; + } + for target in args.iter().skip(1) { + self.scheduler.process_open(opener, &[&args[0], target]); + } + } +} diff --git a/yazi-core/src/tasks/tasks.rs b/yazi-core/src/tasks/tasks.rs index f2684c1b..f555a877 100644 --- a/yazi-core/src/tasks/tasks.rs +++ b/yazi-core/src/tasks/tasks.rs @@ -56,28 +56,6 @@ impl Tasks { ongoing.values().take(Self::limit()).map(Into::into).collect() } - pub fn file_open(&self, hovered: &Url, targets: &[(Url, String)]) { - let mut openers = HashMap::new(); - for (url, mime) in targets { - if let Some(opener) = OPEN.openers(url, mime).and_then(|o| o.first().copied()) { - openers.entry(opener).or_insert_with(|| vec![hovered]).push(url); - } - } - for (opener, args) in openers { - self.file_open_with(opener, &args); - } - } - - pub fn file_open_with(&self, opener: &Opener, args: &[impl AsRef]) { - if opener.spread { - self.scheduler.process_open(opener, args); - return; - } - for target in args.iter().skip(1) { - self.scheduler.process_open(opener, &[&args[0], target]); - } - } - pub fn file_cut(&self, src: &[&Url], dest: &Url, force: bool) { for &u in src { let to = dest.join(u.file_name().unwrap()); diff --git a/yazi-proxy/src/options/mod.rs b/yazi-proxy/src/options/mod.rs index e787fc53..d8b2feb1 100644 --- a/yazi-proxy/src/options/mod.rs +++ b/yazi-proxy/src/options/mod.rs @@ -1,9 +1,11 @@ mod input; mod notify; mod open; +mod process; mod select; pub use input::*; pub use notify::*; pub use open::*; +pub use process::*; pub use select::*; diff --git a/yazi-proxy/src/options/process.rs b/yazi-proxy/src/options/process.rs new file mode 100644 index 00000000..9dbf8e30 --- /dev/null +++ b/yazi-proxy/src/options/process.rs @@ -0,0 +1,18 @@ +use std::ffi::OsString; + +use yazi_shared::event::Cmd; + +// --- Exec +#[derive(Default)] +pub struct ProcessExecOpt { + pub cmd: OsString, + pub args: Vec, + pub block: bool, + pub orphan: bool, +} + +impl TryFrom for ProcessExecOpt { + type Error = (); + + fn try_from(mut c: Cmd) -> Result { c.take_data().ok_or(()) } +}