Skip to content

Commit af92b92

Browse files
Nicholas42Nicholas Schwabsxyazi
authored
feat: allow custom exit code with quit --code (#2609)
Co-authored-by: Nicholas Schwab <[email protected]> Co-authored-by: sxyazi <[email protected]>
1 parent bef4810 commit af92b92

File tree

8 files changed

+52
-42
lines changed

8 files changed

+52
-42
lines changed

yazi-core/src/mgr/commands/close.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ use yazi_shared::event::CmdCow;
33
use crate::{mgr::{Mgr, commands::quit}, tasks::Tasks};
44

55
#[derive(Default)]
6-
struct Opt {
7-
no_cwd_file: bool,
8-
}
6+
struct Opt(quit::Opt);
7+
98
impl From<CmdCow> for Opt {
10-
fn from(c: CmdCow) -> Self { Self { no_cwd_file: c.bool("no-cwd-file") } }
11-
}
12-
impl From<Opt> for quit::Opt {
13-
fn from(value: Opt) -> Self { Self { no_cwd_file: value.no_cwd_file } }
9+
fn from(c: CmdCow) -> Self { Self(c.into()) }
1410
}
1511

1612
impl Mgr {
@@ -19,6 +15,6 @@ impl Mgr {
1915
if self.tabs.len() > 1 {
2016
return self.tabs.close(self.tabs.cursor);
2117
}
22-
self.quit(opt, tasks);
18+
self.quit(opt.0, tasks);
2319
}
2420
}

yazi-core/src/mgr/commands/open.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
use std::{borrow::Cow, ffi::OsString};
1+
use std::borrow::Cow;
22

33
use tracing::error;
4-
use yazi_boot::ARGS;
54
use yazi_config::{YAZI, popup::PickCfg};
65
use yazi_fs::File;
7-
use yazi_macro::emit;
86
use yazi_plugin::isolate;
97
use yazi_proxy::{MgrProxy, PickProxy, TasksProxy, options::OpenDoOpt};
10-
use yazi_shared::{MIME_DIR, event::{CmdCow, EventQuit}, url::Url};
8+
use yazi_shared::{MIME_DIR, event::CmdCow, url::Url};
119

1210
use crate::{mgr::Mgr, tab::Folder, tasks::Tasks};
1311

1412
#[derive(Clone, Copy)]
15-
struct Opt {
16-
interactive: bool,
17-
hovered: bool,
13+
pub(super) struct Opt {
14+
pub(super) interactive: bool,
15+
pub(super) hovered: bool,
1816
}
1917

2018
impl From<CmdCow> for Opt {
@@ -120,19 +118,4 @@ impl Mgr {
120118
|| find(self.hovered_folder())
121119
|| find(self.active().history.get(&p))
122120
}
123-
124-
fn quit_with_selected(opt: Opt, selected: &[&Url]) -> bool {
125-
if opt.interactive || ARGS.chooser_file.is_none() {
126-
return false;
127-
}
128-
129-
let paths = selected.iter().fold(OsString::new(), |mut s, &u| {
130-
s.push(u.as_os_str());
131-
s.push("\n");
132-
s
133-
});
134-
135-
emit!(Quit(EventQuit { selected: Some(paths), ..Default::default() }));
136-
true
137-
}
138121
}

yazi-core/src/mgr/commands/quit.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
use std::time::Duration;
1+
use std::{ffi::OsString, time::Duration};
22

33
use tokio::{select, time};
4+
use yazi_boot::ARGS;
45
use yazi_config::popup::ConfirmCfg;
56
use yazi_macro::emit;
67
use yazi_proxy::ConfirmProxy;
7-
use yazi_shared::event::{CmdCow, EventQuit};
8+
use yazi_shared::{event::{CmdCow, Data, EventQuit}, url::Url};
89

910
use crate::{mgr::Mgr, tasks::Tasks};
1011

1112
#[derive(Default)]
1213
pub(super) struct Opt {
14+
pub(super) code: i32,
1315
pub(super) no_cwd_file: bool,
1416
}
17+
1518
impl From<CmdCow> for Opt {
16-
fn from(c: CmdCow) -> Self { Self { no_cwd_file: c.bool("no-cwd-file") } }
19+
fn from(c: CmdCow) -> Self {
20+
Self {
21+
code: c.get("code").and_then(Data::as_i32).unwrap_or_default(),
22+
no_cwd_file: c.bool("no-cwd-file"),
23+
}
24+
}
25+
}
26+
27+
impl From<Opt> for EventQuit {
28+
fn from(value: Opt) -> Self {
29+
EventQuit { code: value.code, no_cwd_file: value.no_cwd_file, ..Default::default() }
30+
}
1731
}
1832

1933
impl Mgr {
2034
#[yazi_codegen::command]
2135
pub fn quit(&self, opt: Opt, tasks: &Tasks) {
22-
let opt = EventQuit { no_cwd_file: opt.no_cwd_file, ..Default::default() };
36+
let event = opt.into();
2337

2438
let ongoing = tasks.ongoing().clone();
2539
let (left, left_names) = {
@@ -28,7 +42,7 @@ impl Mgr {
2842
};
2943

3044
if left == 0 {
31-
emit!(Quit(opt));
45+
emit!(Quit(event));
3246
return;
3347
}
3448

@@ -41,22 +55,37 @@ impl Mgr {
4155
i += 1;
4256
if i > 40 { break }
4357
else if ongoing.lock().is_empty() {
44-
emit!(Quit(opt));
58+
emit!(Quit(event));
4559
return;
4660
}
4761
}
4862
b = &mut rx => {
4963
if b.unwrap_or(false) {
50-
emit!(Quit(opt));
64+
emit!(Quit(event));
5165
}
5266
return;
5367
}
5468
}
5569
}
5670

5771
if rx.await.unwrap_or(false) {
58-
emit!(Quit(opt));
72+
emit!(Quit(event));
5973
}
6074
});
6175
}
76+
77+
pub(super) fn quit_with_selected(opt: super::open::Opt, selected: &[&Url]) -> bool {
78+
if opt.interactive || ARGS.chooser_file.is_none() {
79+
return false;
80+
}
81+
82+
let paths = selected.iter().fold(OsString::new(), |mut s, &u| {
83+
s.push(u.as_os_str());
84+
s.push("\n");
85+
s
86+
});
87+
88+
emit!(Quit(EventQuit { selected: Some(paths), ..Default::default() }));
89+
true
90+
}
6291
}

yazi-fm/src/app/commands/quit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl App {
1919
self.selected_to_file(selected);
2020
}
2121

22-
Term::goodbye(|| false);
22+
Term::goodbye(|| opt.code);
2323
}
2424

2525
fn cwd_to_file(&self) {

yazi-fm/src/panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Panic {
1010
std::panic::set_hook(Box::new(move |info| {
1111
Term::goodbye(|| {
1212
hook(info);
13-
true
13+
1
1414
});
1515
}));
1616
}

yazi-fm/src/term.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Term {
8989
Ok(disable_raw_mode()?)
9090
}
9191

92-
pub(super) fn goodbye(f: impl FnOnce() -> bool) -> ! {
92+
pub(super) fn goodbye(f: impl FnOnce() -> i32) -> ! {
9393
if CSI_U.swap(false, Ordering::Relaxed) {
9494
PopKeyboardEnhancementFlags.write_ansi(&mut TTY.writer()).ok();
9595
}
@@ -111,7 +111,7 @@ impl Term {
111111

112112
disable_raw_mode().ok();
113113

114-
std::process::exit(f() as i32);
114+
std::process::exit(f());
115115
}
116116

117117
pub(super) fn draw(&mut self, f: impl FnOnce(&mut Frame)) -> io::Result<CompletedFrame> {

yazi-shared/src/event/data.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ macro_rules! impl_number_as {
182182
impl_integer_as!(usize, as_usize);
183183
impl_integer_as!(isize, as_isize);
184184
impl_integer_as!(i16, as_i16);
185+
impl_integer_as!(i32, as_i32);
185186

186187
impl_number_as!(f64, as_f64);
187188

yazi-shared/src/event/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub enum Event {
2323

2424
#[derive(Debug, Default)]
2525
pub struct EventQuit {
26+
pub code: i32,
2627
pub no_cwd_file: bool,
2728
pub selected: Option<OsString>,
2829
}

0 commit comments

Comments
 (0)