From 4527044547af74802450efc4dad85dc0be2b40b7 Mon Sep 17 00:00:00 2001 From: XOR-op <17672363+XOR-op@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:36:08 -0400 Subject: [PATCH] feat: add new `--no-cwd-file` option to `quit` command for flexible cwd-file setting (#245) --- app/src/app.rs | 10 +++++----- app/src/executor.rs | 2 +- app/src/signals.rs | 2 +- config/preset/keymap.toml | 9 +++++---- core/src/event.rs | 5 ++++- core/src/manager/manager.rs | 8 ++++---- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/app.rs b/app/src/app.rs index 761d0917..525f5a6f 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(no_cwd_file) => { + app.dispatch_quit(no_cwd_file); break; } Event::Key(key) => app.dispatch_key(key), @@ -41,8 +41,8 @@ impl App { Ok(()) } - fn dispatch_quit(&mut self) { - if let Some(p) = &BOOT.cwd_file { + fn dispatch_quit(&mut self, no_cwd_file: bool) { + if let Some(p) = BOOT.cwd_file.as_ref().filter(|_| !no_cwd_file) { let cwd = self.cx.manager.cwd().as_os_str(); #[cfg(target_os = "windows")] @@ -200,7 +200,7 @@ impl App { use std::os::unix::ffi::OsStrExt; std::fs::write(p, paths.as_bytes()).ok(); } - return emit!(Quit); + return emit!(Quit(false)); } if let Some(opener) = opener { diff --git a/app/src/executor.rs b/app/src/executor.rs index 788cf3f4..8799e84f 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..a01caa39 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(false)).is_err() { break; } } diff --git a/config/preset/keymap.toml b/config/preset/keymap.toml index 8c3acd79..7109c75f 100644 --- a/config/preset/keymap.toml +++ b/config/preset/keymap.toml @@ -1,10 +1,11 @@ [manager] keymap = [ - { on = [ "" ], exec = "escape", desc = "Exit visual mode, clear selected, or cancel search" }, - { on = [ "q" ], exec = "quit", desc = "Exit the process" }, - { on = [ "" ], exec = "close", desc = "Close the current tab, or quit if it is last tab" }, - { on = [ "" ], exec = "suspend", desc = "Suspend the process" }, + { 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 cwd-file" }, + { on = [ "" ], exec = "close", desc = "Close the current tab, or quit if it is last tab" }, + { on = [ "" ], exec = "suspend", desc = "Suspend the process" }, # Navigation { on = [ "k" ], exec = "arrow -1", desc = "Move cursor up" }, diff --git a/core/src/event.rs b/core/src/event.rs index 5537c50a..31b7d825 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), // no-cwd-file Key(KeyEvent), Paste(String), Render(String), @@ -54,6 +54,9 @@ impl Event { #[macro_export] macro_rules! emit { + (Quit($no_cwd_file:expr)) => { + $crate::Event::Quit($no_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..8839501f 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, no_cwd_file: bool) -> bool { let tasks = tasks.len(); if tasks == 0 { - emit!(Quit); + emit!(Quit(no_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(no_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, false) } pub fn suspend(&mut self) -> bool {