refactor: quit command (#484)

This commit is contained in:
Alexander Serowy 2024-01-09 20:10:26 +01:00 committed by GitHub
parent c3a8bdc3b6
commit d7d000c213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 34 deletions

View File

@ -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
}
}

View File

@ -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<Opt>, 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));
}
}
});

View File

@ -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(());
}
}

View File

@ -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<bool> for Opt {
fn from(no_cwd_file: bool) -> Self { Self { no_cwd_file } }
}
impl App {
pub(crate) fn quit(&mut self, opt: impl Into<Opt>) -> 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();
}
}
}

View File

@ -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;
}
}

View File

@ -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<OsString>,
}
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();
};

View File

@ -62,6 +62,7 @@ impl Term {
.ok();
disable_raw_mode().ok();
std::process::exit(f() as i32);
}