diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index 21d01c4a..1009cc98 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -1,10 +1,9 @@ use std::ffi::OsString; -use tokio::fs; use tracing::error; use yazi_config::{popup::SelectCfg, ARGS, OPEN}; use yazi_plugin::isolate; -use yazi_shared::{emit, event::Exec, 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}; @@ -96,9 +95,9 @@ impl Manager { } fn quit_with_selected(selected: &[&File]) -> bool { - let Some(p) = ARGS.chooser_file.clone() else { + 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()); @@ -106,10 +105,7 @@ impl Manager { s }); - tokio::spawn(async move { - fs::write(p, paths.as_encoded_bytes()).await.ok(); - emit!(Quit(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 d47b4fca..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}; +use yazi_shared::{emit, event::{EventQuit, Exec}}; use crate::{input::Input, manager::Manager, tasks::Tasks}; @@ -16,11 +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 opt = EventQuit { no_cwd_file: opt.into().no_cwd_file, ..Default::default() }; let tasks = tasks.len(); if tasks == 0 { - emit!(Quit(opt.no_cwd_file)); + emit!(Quit(opt)); return; } @@ -28,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(opt.no_cwd_file)); + emit!(Quit(opt)); } } }); diff --git a/yazi-fm/src/app/app.rs b/yazi-fm/src/app/app.rs index c4356934..78ef15a3 100644 --- a/yazi-fm/src/app/app.rs +++ b/yazi-fm/src/app/app.rs @@ -35,8 +35,8 @@ impl App { Event::Key(key) => app.dispatch_key(key), Event::Resize => app.resize()?, Event::Paste(str) => app.dispatch_paste(str), - Event::Quit(no_cwd_file) => { - app.quit(no_cwd_file)?; + Event::Quit(opt) => { + app.quit(opt)?; return Ok(()); } } diff --git a/yazi-fm/src/app/commands/quit.rs b/yazi-fm/src/app/commands/quit.rs index 758f5036..43a07bcf 100644 --- a/yazi-fm/src/app/commands/quit.rs +++ b/yazi-fm/src/app/commands/quit.rs @@ -1,26 +1,33 @@ +use std::ffi::OsString; + use anyhow::Result; use yazi_config::ARGS; -use yazi_shared::term::Term; +use yazi_shared::{event::EventQuit, term::Term}; use crate::app::App; -pub struct Opt { - no_cwd_file: bool, -} - -impl From for Opt { - fn from(no_cwd_file: bool) -> Self { Self { no_cwd_file } } -} - impl App { - pub(crate) fn quit(&mut self, opt: impl Into) -> Result<()> { - let opt = opt.into() as Opt; - - if let Some(p) = ARGS.cwd_file.as_ref().filter(|_| !opt.no_cwd_file) { - let cwd = self.cx.manager.cwd().as_os_str(); - std::fs::write(p, cwd.as_encoded_bytes()).ok(); + 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 { + let cwd = self.cx.manager.cwd().as_os_str(); + std::fs::write(p, cwd.as_encoded_bytes()).ok(); + } + } + + 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 db3f4f0f..f3e4eaa7 100644 --- a/yazi-fm/src/signals.rs +++ b/yazi-fm/src/signals.rs @@ -61,7 +61,7 @@ impl Signals { while let Some(signal) = signals.next().await { match signal { SIGHUP | SIGTERM | SIGQUIT | SIGINT => { - if tx.send(Event::Quit(false)).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 c6e2d49c..756c9d66 100644 --- a/yazi-shared/src/event/event.rs +++ b/yazi-shared/src/event/event.rs @@ -1,3 +1,5 @@ +use std::ffi::OsString; + use crossterm::event::KeyEvent; use tokio::sync::{mpsc::UnboundedSender, oneshot}; @@ -13,7 +15,13 @@ pub enum Event { Key(KeyEvent), Resize, Paste(String), - Quit(bool), // no-cwd-file + Quit(EventQuit), +} + +#[derive(Debug, Default)] +pub struct EventQuit { + pub no_cwd_file: bool, + pub selected: Option, } impl Event { @@ -31,13 +39,12 @@ impl Event { #[macro_export] macro_rules! emit { - (Quit($no_cwd_file:expr)) => { - $crate::event::Event::Quit($no_cwd_file).emit(); + (Quit($opt:expr)) => { + $crate::event::Event::Quit($opt).emit(); }; (Call($exec:expr, $layer:expr)) => { $crate::event::Event::Call($exec, $layer).emit(); }; - ($event:ident) => { $crate::event::Event::$event.emit(); }; diff --git a/yazi-shared/src/term/term.rs b/yazi-shared/src/term/term.rs index d4ee2437..55124f39 100644 --- a/yazi-shared/src/term/term.rs +++ b/yazi-shared/src/term/term.rs @@ -62,6 +62,7 @@ impl Term { .ok(); disable_raw_mode().ok(); + std::process::exit(f() as i32); }