Simplify the code

This commit is contained in:
sxyazi 2025-04-12 17:50:18 +08:00
parent d066aae015
commit ad1d7e86d1
No known key found for this signature in database
5 changed files with 44 additions and 51 deletions

View file

@ -1,24 +1,12 @@
use yazi_shared::event::{CmdCow, Data}; use yazi_shared::event::CmdCow;
use crate::{mgr::{Mgr, commands::quit}, tasks::Tasks}; use crate::{mgr::{Mgr, commands::quit}, tasks::Tasks};
#[derive(Default)] #[derive(Default)]
struct Opt { struct Opt(quit::Opt);
no_cwd_file: bool,
exit_code: i32,
}
impl From<CmdCow> for Opt { impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { fn from(c: CmdCow) -> Self { Self(c.into()) }
Self {
no_cwd_file: c.bool("no-cwd-file"),
exit_code: c.get("exit_code").and_then(Data::as_i32).unwrap_or_default(),
}
}
}
impl From<Opt> for quit::Opt {
fn from(value: Opt) -> Self {
Self { no_cwd_file: value.no_cwd_file, exit_code: value.exit_code }
}
} }
impl Mgr { impl Mgr {
@ -27,6 +15,6 @@ impl Mgr {
if self.tabs.len() > 1 { if self.tabs.len() > 1 {
return self.tabs.close(self.tabs.cursor); return self.tabs.close(self.tabs.cursor);
} }
self.quit(opt, tasks); self.quit(opt.0, tasks);
} }
} }

View file

@ -1,20 +1,18 @@
use std::{borrow::Cow, ffi::OsString}; use std::borrow::Cow;
use tracing::error; use tracing::error;
use yazi_boot::ARGS;
use yazi_config::{YAZI, popup::PickCfg}; use yazi_config::{YAZI, popup::PickCfg};
use yazi_fs::File; use yazi_fs::File;
use yazi_macro::emit;
use yazi_plugin::isolate; use yazi_plugin::isolate;
use yazi_proxy::{MgrProxy, PickProxy, TasksProxy, options::OpenDoOpt}; use yazi_proxy::{MgrProxy, PickProxy, TasksProxy, options::OpenDoOpt};
use yazi_shared::{MIME_DIR, event::{CmdCow, EventQuit}, url::Url}; use yazi_shared::{MIME_DIR, event::CmdCow, url::Url};
use crate::{mgr::Mgr, tab::Folder, tasks::Tasks}; use crate::{mgr::Mgr, tab::Folder, tasks::Tasks};
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct Opt { pub(super) struct Opt {
interactive: bool, pub(super) interactive: bool,
hovered: bool, pub(super) hovered: bool,
} }
impl From<CmdCow> for Opt { impl From<CmdCow> for Opt {
@ -120,19 +118,4 @@ impl Mgr {
|| find(self.hovered_folder()) || find(self.hovered_folder())
|| find(self.active().history.get(&p)) || find(self.active().history.get(&p))
} }
fn quit_with_selected(opt: Opt, selected: &[&Url]) -> bool {
if opt.interactive || ARGS.chooser_file.is_none() {
return false;
}
let paths = selected.iter().fold(OsString::new(), |mut s, &u| {
s.push(u.as_os_str());
s.push("\n");
s
});
emit!(Quit(EventQuit { selected: Some(paths), ..Default::default() }));
true
}
} }

View file

@ -1,32 +1,39 @@
use std::time::Duration; use std::{ffi::OsString, time::Duration};
use tokio::{select, time}; use tokio::{select, time};
use yazi_boot::ARGS;
use yazi_config::popup::ConfirmCfg; use yazi_config::popup::ConfirmCfg;
use yazi_macro::emit; use yazi_macro::emit;
use yazi_proxy::ConfirmProxy; use yazi_proxy::ConfirmProxy;
use yazi_shared::event::{CmdCow, Data, EventQuit}; use yazi_shared::{event::{CmdCow, Data, EventQuit}, url::Url};
use crate::{mgr::Mgr, tasks::Tasks}; use crate::{mgr::Mgr, tasks::Tasks};
#[derive(Default)] #[derive(Default)]
pub(super) struct Opt { pub(super) struct Opt {
pub(super) code: i32,
pub(super) no_cwd_file: bool, pub(super) no_cwd_file: bool,
pub(super) exit_code: i32,
} }
impl From<CmdCow> for Opt { impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { fn from(c: CmdCow) -> Self {
Self { Self {
code: c.get("code").and_then(Data::as_i32).unwrap_or_default(),
no_cwd_file: c.bool("no-cwd-file"), no_cwd_file: c.bool("no-cwd-file"),
exit_code: c.get("exit_code").and_then(Data::as_i32).unwrap_or_default(),
} }
} }
} }
impl From<Opt> for EventQuit {
fn from(value: Opt) -> Self {
EventQuit { code: value.code, no_cwd_file: value.no_cwd_file, ..Default::default() }
}
}
impl Mgr { impl Mgr {
#[yazi_codegen::command] #[yazi_codegen::command]
pub fn quit(&self, opt: Opt, tasks: &Tasks) { pub fn quit(&self, opt: Opt, tasks: &Tasks) {
let opt = let event = opt.into();
EventQuit { no_cwd_file: opt.no_cwd_file, exit_code: opt.exit_code, ..Default::default() };
let ongoing = tasks.ongoing().clone(); let ongoing = tasks.ongoing().clone();
let (left, left_names) = { let (left, left_names) = {
@ -35,7 +42,7 @@ impl Mgr {
}; };
if left == 0 { if left == 0 {
emit!(Quit(opt)); emit!(Quit(event));
return; return;
} }
@ -48,13 +55,13 @@ impl Mgr {
i += 1; i += 1;
if i > 40 { break } if i > 40 { break }
else if ongoing.lock().is_empty() { else if ongoing.lock().is_empty() {
emit!(Quit(opt)); emit!(Quit(event));
return; return;
} }
} }
b = &mut rx => { b = &mut rx => {
if b.unwrap_or(false) { if b.unwrap_or(false) {
emit!(Quit(opt)); emit!(Quit(event));
} }
return; return;
} }
@ -62,8 +69,23 @@ impl Mgr {
} }
if rx.await.unwrap_or(false) { if rx.await.unwrap_or(false) {
emit!(Quit(opt)); emit!(Quit(event));
} }
}); });
} }
pub(super) fn quit_with_selected(opt: super::open::Opt, selected: &[&Url]) -> bool {
if opt.interactive || ARGS.chooser_file.is_none() {
return false;
}
let paths = selected.iter().fold(OsString::new(), |mut s, &u| {
s.push(u.as_os_str());
s.push("\n");
s
});
emit!(Quit(EventQuit { selected: Some(paths), ..Default::default() }));
true
}
} }

View file

@ -19,7 +19,7 @@ impl App {
self.selected_to_file(selected); self.selected_to_file(selected);
} }
Term::goodbye(|| opt.exit_code); Term::goodbye(|| opt.code);
} }
fn cwd_to_file(&self) { fn cwd_to_file(&self) {

View file

@ -23,8 +23,8 @@ pub enum Event {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct EventQuit { pub struct EventQuit {
pub code: i32,
pub no_cwd_file: bool, pub no_cwd_file: bool,
pub exit_code: i32,
pub selected: Option<OsString>, pub selected: Option<OsString>,
} }