From 8fe080d802d8bd7b436584cdf1306e03a960bde5 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 12 Mar 2024 23:06:25 +0800 Subject: [PATCH] .. --- yazi-core/src/manager/commands/bulk_rename.rs | 22 ++++++---------- yazi-core/src/manager/commands/open.rs | 6 ++--- yazi-core/src/tab/commands/shell.rs | 21 +++++++++------ yazi-core/src/tasks/process.rs | 6 ++--- yazi-proxy/src/options/open.rs | 4 ++- yazi-proxy/src/options/process.rs | 4 +-- yazi-proxy/src/tasks.rs | 17 ++++++++++-- yazi-scheduler/src/process/op.rs | 26 +++++++++---------- yazi-scheduler/src/process/process.rs | 26 +++++++++---------- yazi-scheduler/src/scheduler.rs | 24 ++++++++--------- 10 files changed, 85 insertions(+), 71 deletions(-) diff --git a/yazi-core/src/manager/commands/bulk_rename.rs b/yazi-core/src/manager/commands/bulk_rename.rs index eb7778af..23f6be96 100644 --- a/yazi-core/src/manager/commands/bulk_rename.rs +++ b/yazi-core/src/manager/commands/bulk_rename.rs @@ -1,9 +1,9 @@ -use std::{collections::HashMap, ffi::OsStr, io::{stdout, BufWriter, Write}, path::PathBuf}; +use std::{borrow::Cow, collections::HashMap, ffi::{OsStr, OsString}, io::{stdout, BufWriter, Write}, path::PathBuf}; use anyhow::{anyhow, Result}; use tokio::{fs::{self, OpenOptions}, io::{stdin, AsyncReadExt, AsyncWriteExt}}; use yazi_config::{OPEN, PREVIEW}; -use yazi_proxy::{AppProxy, HIDER, WATCHER}; +use yazi_proxy::{AppProxy, TasksProxy, HIDER, WATCHER}; use yazi_shared::{fs::{accessible, max_common_root, File, FilesOp, Url}, term::Term, Defer}; use crate::manager::Manager; @@ -31,19 +31,13 @@ impl Manager { .write_all(s.as_encoded_bytes()) .await?; - let _permit = HIDER.acquire().await.unwrap(); - let _defer1 = Defer::new(AppProxy::resume); - let _defer2 = Defer::new(|| tokio::spawn(fs::remove_file(tmp.clone()))); - AppProxy::stop().await; + let _defer1 = Defer::new(|| tokio::spawn(fs::remove_file(tmp.clone()))); + TasksProxy::process_exec(vec![OsString::new(), tmp.to_owned().into()], Cow::Borrowed(opener)) + .await; - // FIXME - // let mut child = super::shell(ShellOpt { - // cmd: (*opener.run).into(), - // args: vec![OsString::new(), tmp.to_owned().into()], - // piped: false, - // orphan: false, - // })?; - // child.wait().await?; + let _permit = HIDER.acquire().await.unwrap(); + let _defer2 = Defer::new(AppProxy::resume); + AppProxy::stop().await; let new: Vec<_> = fs::read_to_string(&tmp).await?.lines().map(PathBuf::from).collect(); Self::bulk_rename_do(cwd, root, old, new).await diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index 24852522..926ca773 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -1,4 +1,4 @@ -use std::ffi::OsString; +use std::{borrow::Cow, ffi::OsString}; use tracing::error; use yazi_boot::ARGS; @@ -87,7 +87,7 @@ impl Manager { return tasks.process_from_files(opt.hovered, targets); } - let openers: Vec<_> = OPEN.common_openers(&targets).into_iter().cloned().collect(); + let openers: Vec<_> = OPEN.common_openers(&targets); if openers.is_empty() { return; } @@ -98,7 +98,7 @@ impl Manager { openers.iter().map(|o| o.desc.clone()).collect(), )); if let Ok(choice) = result.await { - TasksProxy::open_with(urls, openers[choice].clone()); + TasksProxy::open_with(urls, Cow::Borrowed(openers[choice])); } }); } diff --git a/yazi-core/src/tab/commands/shell.rs b/yazi-core/src/tab/commands/shell.rs index 2547a694..3be29cd8 100644 --- a/yazi-core/src/tab/commands/shell.rs +++ b/yazi-core/src/tab/commands/shell.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use yazi_config::{open::Opener, popup::InputCfg}; use yazi_proxy::{InputProxy, TasksProxy}; use yazi_shared::event::Cmd; @@ -38,14 +40,17 @@ impl Tab { } } - TasksProxy::open_with(selected, Opener { - run: opt.run, - block: opt.block, - orphan: false, - desc: Default::default(), - for_: None, - spread: true, - }); + TasksProxy::open_with( + selected, + Cow::Owned(Opener { + run: opt.run, + block: opt.block, + orphan: false, + desc: Default::default(), + for_: None, + spread: true, + }), + ); }); } } diff --git a/yazi-core/src/tasks/process.rs b/yazi-core/src/tasks/process.rs index b8b98cf3..17beaa1a 100644 --- a/yazi-core/src/tasks/process.rs +++ b/yazi-core/src/tasks/process.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, ffi::OsString, mem}; +use std::{borrow::Cow, collections::HashMap, ffi::OsString, mem}; use yazi_config::{open::Opener, OPEN}; use yazi_shared::fs::Url; @@ -15,13 +15,13 @@ impl Tasks { } for (opener, args) in openers { self.process_from_opener( - opener.clone(), + Cow::Borrowed(opener), args.into_iter().map(|u| u.into_os_string()).collect(), ); } } - pub fn process_from_opener(&self, opener: Opener, mut args: Vec) { + pub fn process_from_opener(&self, opener: Cow<'static, Opener>, mut args: Vec) { if opener.spread { self.scheduler.process_open(opener, args, None); return; diff --git a/yazi-proxy/src/options/open.rs b/yazi-proxy/src/options/open.rs index 65a6cd5d..77ff707d 100644 --- a/yazi-proxy/src/options/open.rs +++ b/yazi-proxy/src/options/open.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use yazi_config::open::Opener; use yazi_shared::{event::Cmd, fs::Url}; @@ -16,7 +18,7 @@ impl From for OpenDoOpt { // --- Open with pub struct OpenWithOpt { pub targets: Vec, - pub opener: Opener, + pub opener: Cow<'static, Opener>, } impl TryFrom for OpenWithOpt { diff --git a/yazi-proxy/src/options/process.rs b/yazi-proxy/src/options/process.rs index 5f77763f..00daf667 100644 --- a/yazi-proxy/src/options/process.rs +++ b/yazi-proxy/src/options/process.rs @@ -1,4 +1,4 @@ -use std::ffi::OsString; +use std::{borrow::Cow, ffi::OsString}; use tokio::sync::oneshot; use yazi_config::open::Opener; @@ -6,8 +6,8 @@ use yazi_shared::event::Cmd; // --- Exec pub struct ProcessExecOpt { - pub opener: Opener, pub args: Vec, + pub opener: Cow<'static, Opener>, pub done: oneshot::Sender<()>, } diff --git a/yazi-proxy/src/tasks.rs b/yazi-proxy/src/tasks.rs index 24ce61f6..b07bdf2c 100644 --- a/yazi-proxy/src/tasks.rs +++ b/yazi-proxy/src/tasks.rs @@ -1,13 +1,26 @@ +use std::{borrow::Cow, ffi::OsString}; + +use tokio::sync::oneshot; use yazi_config::open::Opener; use yazi_shared::{emit, event::Cmd, fs::Url, Layer}; -use crate::options::OpenWithOpt; +use crate::options::{OpenWithOpt, ProcessExecOpt}; pub struct TasksProxy; impl TasksProxy { #[inline] - pub fn open_with(targets: Vec, opener: Opener) { + pub fn open_with(targets: Vec, opener: Cow<'static, Opener>) { emit!(Call(Cmd::new("open_with").with_data(OpenWithOpt { targets, opener }), Layer::Tasks)); } + + #[inline] + pub async fn process_exec(args: Vec, opener: Cow<'static, Opener>) { + let (tx, rx) = oneshot::channel(); + emit!(Call( + Cmd::new("process_exec").with_data(ProcessExecOpt { args, opener, done: tx }), + Layer::Tasks + )); + rx.await.ok(); + } } diff --git a/yazi-scheduler/src/process/op.rs b/yazi-scheduler/src/process/op.rs index 1981522c..b740e071 100644 --- a/yazi-scheduler/src/process/op.rs +++ b/yazi-scheduler/src/process/op.rs @@ -4,19 +4,6 @@ use tokio_util::sync::CancellationToken; use super::ShellOpt; -#[derive(Debug)] -pub struct ProcessOpOrphan { - pub id: usize, - pub cmd: OsString, - pub args: Vec, -} - -impl From for ShellOpt { - fn from(op: ProcessOpOrphan) -> Self { - Self { cmd: op.cmd, args: op.args, piped: false, orphan: true } - } -} - #[derive(Debug)] pub struct ProcessOpBlock { pub id: usize, @@ -30,6 +17,19 @@ impl From for ShellOpt { } } +#[derive(Debug)] +pub struct ProcessOpOrphan { + pub id: usize, + pub cmd: OsString, + pub args: Vec, +} + +impl From for ShellOpt { + fn from(op: ProcessOpOrphan) -> Self { + Self { cmd: op.cmd, args: op.args, piped: false, orphan: true } + } +} + #[derive(Debug)] pub struct ProcessOpBg { pub id: usize, diff --git a/yazi-scheduler/src/process/process.rs b/yazi-scheduler/src/process/process.rs index 10da2bad..e868f3ae 100644 --- a/yazi-scheduler/src/process/process.rs +++ b/yazi-scheduler/src/process/process.rs @@ -13,19 +13,6 @@ pub struct Process { impl Process { pub fn new(prog: mpsc::UnboundedSender) -> Self { Self { prog } } - pub async fn orphan(&self, task: ProcessOpOrphan) -> Result<()> { - let id = task.id; - match super::shell(task.into()) { - Ok(_) => self.succ(id)?, - Err(e) => { - self.prog.send(TaskProg::New(id, 0))?; - self.fail(id, format!("Failed to spawn process: {e}"))?; - } - } - - Ok(()) - } - pub async fn block(&self, task: ProcessOpBlock) -> Result<()> { let _permit = HIDER.acquire().await.unwrap(); let _defer = Defer::new(AppProxy::resume); @@ -50,6 +37,19 @@ impl Process { self.succ(id) } + pub async fn orphan(&self, task: ProcessOpOrphan) -> Result<()> { + let id = task.id; + match super::shell(task.into()) { + Ok(_) => self.succ(id)?, + Err(e) => { + self.prog.send(TaskProg::New(id, 0))?; + self.fail(id, format!("Failed to spawn process: {e}"))?; + } + } + + Ok(()) + } + pub async fn bg(&self, task: ProcessOpBg) -> Result<()> { self.prog.send(TaskProg::New(task.id, 0))?; let mut child = diff --git a/yazi-scheduler/src/scheduler.rs b/yazi-scheduler/src/scheduler.rs index 392f3cba..8cff33eb 100644 --- a/yazi-scheduler/src/scheduler.rs +++ b/yazi-scheduler/src/scheduler.rs @@ -1,4 +1,4 @@ -use std::{ffi::OsString, sync::Arc, time::Duration}; +use std::{borrow::Cow, ffi::OsString, sync::Arc, time::Duration}; use futures::{future::BoxFuture, FutureExt}; use parking_lot::Mutex; @@ -335,7 +335,7 @@ impl Scheduler { pub fn process_open( &self, - opener: Opener, + opener: Cow<'static, Opener>, args: Vec, done: Option>, ) { @@ -369,19 +369,19 @@ impl Scheduler { }) }); - // FIXME: use micro instead - tokio::spawn({ - let process = self.process.clone(); - let opener = opener.clone(); + let process = self.process.clone(); + _ = self.micro.try_send( async move { - if opener.orphan { - process.orphan(ProcessOpOrphan { id, cmd: opener.run.into(), args }).await.ok(); - } else if opener.block { - process.block(ProcessOpBlock { id, cmd: opener.run.into(), args }).await.ok(); + if opener.block { + process.block(ProcessOpBlock { id, cmd: OsString::from(&opener.run), args }).await.ok(); + } else if opener.orphan { + process.orphan(ProcessOpOrphan { id, cmd: OsString::from(&opener.run), args }).await.ok(); } else { - process.bg(ProcessOpBg { id, cmd: opener.run.into(), args, ct }).await.ok(); + process.bg(ProcessOpBg { id, cmd: OsString::from(&opener.run), args, ct }).await.ok(); } } - }); + .boxed(), + HIGH, + ); } }