diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index 0792df55..9313c691 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -3,7 +3,7 @@ use std::ffi::OsString; use tracing::error; use yazi_config::{popup::SelectCfg, ARGS, OPEN}; use yazi_plugin::isolate; -use yazi_shared::{emit, event::{Exec, QuitAction}, fs::{File, Url}, Layer, MIME_DIR}; +use yazi_shared::{emit, event::{EventQuit, Exec}, fs::{File, Url}, Layer, MIME_DIR}; use crate::{manager::Manager, select::Select, tasks::Tasks}; @@ -23,7 +23,7 @@ impl Manager { let selected = self.selected(); if selected.is_empty() { return; - } else if Self::quit_with_chooser(&selected) { + } else if Self::quit_with_selected(&selected) { return; } @@ -94,8 +94,10 @@ impl Manager { }); } - fn quit_with_chooser(selected: &[&File]) -> bool { - let mut quit_actions = vec![QuitAction::CwdToFile]; + fn quit_with_selected(selected: &[&File]) -> bool { + if ARGS.chooser_file.is_none() { + return false; + } let paths = selected.iter().fold(OsString::new(), |mut s, &f| { s.push(f.url.as_os_str()); @@ -103,18 +105,7 @@ impl Manager { s }); - if ARGS.chooser_file.is_some() { - quit_actions.push(QuitAction::SelectToFile(paths.clone())); - }; - - if quit_actions.len() > 1 { - tokio::spawn(async move { - emit!(Quit(quit_actions)); - }); - - true - } else { - false - } + emit!(Quit(EventQuit { selected: Some(paths), ..Default::default() })); + true } } diff --git a/yazi-core/src/manager/commands/quit.rs b/yazi-core/src/manager/commands/quit.rs index 00e284bd..daa94593 100644 --- a/yazi-core/src/manager/commands/quit.rs +++ b/yazi-core/src/manager/commands/quit.rs @@ -1,5 +1,5 @@ use yazi_config::popup::InputCfg; -use yazi_shared::{emit, event::{Exec, QuitAction}}; +use yazi_shared::{emit, event::{EventQuit, Exec}}; use crate::{input::Input, manager::Manager, tasks::Tasks}; @@ -16,12 +16,11 @@ impl From<&Exec> for Opt { impl Manager { pub fn quit(&self, opt: impl Into, tasks: &Tasks) { - let opt = opt.into() as Opt; - let actions = if opt.no_cwd_file { vec![] } else { vec![QuitAction::CwdToFile] }; + let opt = EventQuit { no_cwd_file: opt.into().no_cwd_file, ..Default::default() }; let tasks = tasks.len(); if tasks == 0 { - emit!(Quit(actions)); + emit!(Quit(opt)); return; } @@ -29,7 +28,7 @@ impl Manager { let mut result = Input::_show(InputCfg::quit(tasks)); if let Some(Ok(choice)) = result.recv().await { if choice == "y" || choice == "Y" { - emit!(Quit(actions)); + emit!(Quit(opt)); } } }); diff --git a/yazi-fm/src/app/commands/quit.rs b/yazi-fm/src/app/commands/quit.rs index f4889ee5..43a07bcf 100644 --- a/yazi-fm/src/app/commands/quit.rs +++ b/yazi-fm/src/app/commands/quit.rs @@ -2,31 +2,31 @@ use std::ffi::OsString; use anyhow::Result; use yazi_config::ARGS; -use yazi_shared::{event::QuitAction, term::Term}; +use yazi_shared::{event::EventQuit, term::Term}; use crate::app::App; impl App { - pub(crate) fn quit(&mut self, actions: Vec) -> Result<()> { - for action in actions { - match action { - QuitAction::CwdToFile => self.cwd_to_file(), - QuitAction::SelectToFile(selected) => self.select_to_file(selected), - } + pub(crate) fn quit(&mut self, opt: EventQuit) -> Result<()> { + if !opt.no_cwd_file { + self.cwd_to_file(); + } + if let Some(selected) = opt.selected { + self.selected_to_file(selected); } Term::goodbye(|| false); } fn cwd_to_file(&self) { - if let Some(p) = ARGS.cwd_file.as_ref() { + if let Some(p) = &ARGS.cwd_file { let cwd = self.cx.manager.cwd().as_os_str(); std::fs::write(p, cwd.as_encoded_bytes()).ok(); } } - fn select_to_file(&self, selected: OsString) { - if let Some(p) = ARGS.chooser_file.clone() { + fn selected_to_file(&self, selected: OsString) { + if let Some(p) = &ARGS.chooser_file { std::fs::write(p, selected.as_encoded_bytes()).ok(); } } diff --git a/yazi-fm/src/signals.rs b/yazi-fm/src/signals.rs index 54654568..a7eed5db 100644 --- a/yazi-fm/src/signals.rs +++ b/yazi-fm/src/signals.rs @@ -48,7 +48,6 @@ impl Signals { fn spawn_system_task(&self) -> Result> { use libc::{SIGCONT, SIGHUP, SIGINT, SIGQUIT, SIGTERM}; use yazi_scheduler::Scheduler; - use yazi_shared::event::QuitAction; let tx = self.tx.clone(); let mut signals = signal_hook_tokio::Signals::new([ @@ -62,7 +61,7 @@ impl Signals { while let Some(signal) = signals.next().await { match signal { SIGHUP | SIGTERM | SIGQUIT | SIGINT => { - if tx.send(Event::Quit(vec![QuitAction::CwdToFile])).is_err() { + if tx.send(Event::Quit(Default::default())).is_err() { break; } } diff --git a/yazi-shared/src/event/event.rs b/yazi-shared/src/event/event.rs index 4f6bfd3c..f954e86b 100644 --- a/yazi-shared/src/event/event.rs +++ b/yazi-shared/src/event/event.rs @@ -15,13 +15,13 @@ pub enum Event { Key(KeyEvent), Resize(u16, u16), Paste(String), - Quit(Vec), + Quit(EventQuit), } -#[derive(Debug, PartialEq)] -pub enum QuitAction { - CwdToFile, - SelectToFile(OsString), +#[derive(Debug, Default)] +pub struct EventQuit { + pub no_cwd_file: bool, + pub selected: Option, } impl Event { @@ -39,8 +39,8 @@ impl Event { #[macro_export] macro_rules! emit { - (Quit($actions:expr)) => { - $crate::event::Event::Quit($actions).emit(); + (Quit($opt:expr)) => { + $crate::event::Event::Quit($opt).emit(); }; (Call($exec:expr, $layer:expr)) => { $crate::event::Event::Call($exec, $layer).emit();