From dfdebed390d99019d5f0d9938dc858cdd69ea122 Mon Sep 17 00:00:00 2001 From: XOR-op <17672363+XOR-op@users.noreply.github.com> Date: Wed, 4 Oct 2023 22:11:11 -0400 Subject: [PATCH] feat: add new options to `quit` command for flexible cwd-file setting --- app/src/app.rs | 14 +++++++++----- app/src/executor.rs | 2 +- app/src/signals.rs | 2 +- config/preset/keymap.toml | 1 + core/src/event.rs | 5 ++++- core/src/manager/manager.rs | 8 ++++---- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/src/app.rs b/app/src/app.rs index 761d0917..d4b28b30 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -25,8 +25,8 @@ impl App { while let Some(event) = app.signals.recv().await { match event { - Event::Quit => { - app.dispatch_quit(); + Event::Quit(write_cwd_file) => { + app.dispatch_quit(write_cwd_file); break; } Event::Key(key) => app.dispatch_key(key), @@ -41,9 +41,13 @@ impl App { Ok(()) } - fn dispatch_quit(&mut self) { + fn dispatch_quit(&mut self, write_cwd_file: bool) { if let Some(p) = &BOOT.cwd_file { - let cwd = self.cx.manager.cwd().as_os_str(); + let cwd = if write_cwd_file { + self.cx.manager.cwd().as_os_str() + } else { + &BOOT.cwd.as_os_str() + }; #[cfg(target_os = "windows")] { @@ -200,7 +204,7 @@ impl App { use std::os::unix::ffi::OsStrExt; std::fs::write(p, paths.as_bytes()).ok(); } - return emit!(Quit); + return emit!(Quit(true)); } if let Some(opener) = opener { diff --git a/app/src/executor.rs b/app/src/executor.rs index 788cf3f4..60770c7b 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -55,7 +55,7 @@ impl Executor { fn manager(cx: &mut Ctx, exec: &Exec) -> bool { match exec.cmd.as_str() { "escape" => cx.manager.active_mut().escape(), - "quit" => cx.manager.quit(&cx.tasks), + "quit" => cx.manager.quit(&cx.tasks,!exec.named.contains_key("no-cwd-file")), "close" => cx.manager.close(&cx.tasks), "suspend" => cx.manager.suspend(), diff --git a/app/src/signals.rs b/app/src/signals.rs index 9a337130..da183f52 100644 --- a/app/src/signals.rs +++ b/app/src/signals.rs @@ -64,7 +64,7 @@ impl Signals { while let Some(signal) = signals.next().await { match signal { SIGHUP | SIGTERM | SIGQUIT | SIGINT => { - if tx.send(Event::Quit).is_err() { + if tx.send(Event::Quit(true)).is_err() { break; } } diff --git a/config/preset/keymap.toml b/config/preset/keymap.toml index 8c3acd79..ed9df9fb 100644 --- a/config/preset/keymap.toml +++ b/config/preset/keymap.toml @@ -3,6 +3,7 @@ keymap = [ { on = [ "" ], exec = "escape", desc = "Exit visual mode, clear selected, or cancel search" }, { on = [ "q" ], exec = "quit", desc = "Exit the process" }, + { on = [ "Q" ], exec = "quit --no-cwd-file", desc = "Exit the process without writing the current directory to cwd-file" }, { on = [ "" ], exec = "close", desc = "Close the current tab, or quit if it is last tab" }, { on = [ "" ], exec = "suspend", desc = "Suspend the process" }, diff --git a/core/src/event.rs b/core/src/event.rs index 5537c50a..b8eecb89 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -12,7 +12,7 @@ use crate::manager::PreviewLock; static TX: RoCell> = RoCell::new(); pub enum Event { - Quit, + Quit(bool), // write-cwd-file Key(KeyEvent), Paste(String), Render(String), @@ -54,6 +54,9 @@ impl Event { #[macro_export] macro_rules! emit { + (Quit($write_cwd_file:expr)) => { + $crate::Event::Quit($write_cwd_file).emit(); + }; (Key($key:expr)) => { $crate::Event::Key($key).emit(); }; diff --git a/core/src/manager/manager.rs b/core/src/manager/manager.rs index 4de2bf6c..967440e0 100644 --- a/core/src/manager/manager.rs +++ b/core/src/manager/manager.rs @@ -88,10 +88,10 @@ impl Manager { false } - pub fn quit(&self, tasks: &Tasks) -> bool { + pub fn quit(&self, tasks: &Tasks, write_cwd_file: bool) -> bool { let tasks = tasks.len(); if tasks == 0 { - emit!(Quit); + emit!(Quit(write_cwd_file)); return false; } @@ -102,7 +102,7 @@ impl Manager { if let Some(Ok(choice)) = result.recv().await { if choice == "y" || choice == "Y" { - emit!(Quit); + emit!(Quit(write_cwd_file)); } } }); @@ -113,7 +113,7 @@ impl Manager { if self.tabs.len() > 1 { return self.tabs.close(self.tabs.idx()); } - self.quit(tasks) + self.quit(tasks,true) } pub fn suspend(&mut self) -> bool {