feat: Allow custom exit code from quit

This is useful to integrate yazi into a workflow with other tools. An
example for this is the pager for https://github.com/aurutils/aurutils
This commit is contained in:
Nicholas Schwab 2025-04-12 09:49:48 +02:00
parent bef481057a
commit d066aae015
7 changed files with 27 additions and 10 deletions

View file

@ -1,16 +1,24 @@
use yazi_shared::event::CmdCow;
use yazi_shared::event::{CmdCow, Data};
use crate::{mgr::{Mgr, commands::quit}, tasks::Tasks};
#[derive(Default)]
struct Opt {
no_cwd_file: bool,
exit_code: i32,
}
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { no_cwd_file: c.bool("no-cwd-file") } }
fn from(c: CmdCow) -> Self {
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 } }
fn from(value: Opt) -> Self {
Self { no_cwd_file: value.no_cwd_file, exit_code: value.exit_code }
}
}
impl Mgr {

View file

@ -4,22 +4,29 @@ use tokio::{select, time};
use yazi_config::popup::ConfirmCfg;
use yazi_macro::emit;
use yazi_proxy::ConfirmProxy;
use yazi_shared::event::{CmdCow, EventQuit};
use yazi_shared::event::{CmdCow, Data, EventQuit};
use crate::{mgr::Mgr, tasks::Tasks};
#[derive(Default)]
pub(super) struct Opt {
pub(super) no_cwd_file: bool,
pub(super) exit_code: i32,
}
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { no_cwd_file: c.bool("no-cwd-file") } }
fn from(c: CmdCow) -> Self {
Self {
no_cwd_file: c.bool("no-cwd-file"),
exit_code: c.get("exit_code").and_then(Data::as_i32).unwrap_or_default(),
}
}
}
impl Mgr {
#[yazi_codegen::command]
pub fn quit(&self, opt: Opt, tasks: &Tasks) {
let opt = EventQuit { no_cwd_file: opt.no_cwd_file, ..Default::default() };
let opt =
EventQuit { no_cwd_file: opt.no_cwd_file, exit_code: opt.exit_code, ..Default::default() };
let ongoing = tasks.ongoing().clone();
let (left, left_names) = {

View file

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

View file

@ -10,7 +10,7 @@ impl Panic {
std::panic::set_hook(Box::new(move |info| {
Term::goodbye(|| {
hook(info);
true
true as i32
});
}));
}

View file

@ -89,7 +89,7 @@ impl Term {
Ok(disable_raw_mode()?)
}
pub(super) fn goodbye(f: impl FnOnce() -> bool) -> ! {
pub(super) fn goodbye(f: impl FnOnce() -> i32) -> ! {
if CSI_U.swap(false, Ordering::Relaxed) {
PopKeyboardEnhancementFlags.write_ansi(&mut TTY.writer()).ok();
}
@ -111,7 +111,7 @@ impl Term {
disable_raw_mode().ok();
std::process::exit(f() as i32);
std::process::exit(f());
}
pub(super) fn draw(&mut self, f: impl FnOnce(&mut Frame)) -> io::Result<CompletedFrame> {

View file

@ -182,6 +182,7 @@ macro_rules! impl_number_as {
impl_integer_as!(usize, as_usize);
impl_integer_as!(isize, as_isize);
impl_integer_as!(i16, as_i16);
impl_integer_as!(i32, as_i32);
impl_number_as!(f64, as_f64);

View file

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