Skip to content

Commit

Permalink
feat: add new --no-cwd-file option to quit command for flexible c…
Browse files Browse the repository at this point in the history
…wd-file setting (#245)
  • Loading branch information
XOR-op authored Oct 5, 2023
1 parent 6da04c7 commit 4527044
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
10 changes: 5 additions & 5 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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")]
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion app/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
2 changes: 1 addition & 1 deletion app/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
9 changes: 5 additions & 4 deletions config/preset/keymap.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[manager]

keymap = [
{ on = [ "<Esc>" ], exec = "escape", desc = "Exit visual mode, clear selected, or cancel search" },
{ on = [ "q" ], exec = "quit", desc = "Exit the process" },
{ on = [ "<C-q>" ], exec = "close", desc = "Close the current tab, or quit if it is last tab" },
{ on = [ "<C-z>" ], exec = "suspend", desc = "Suspend the process" },
{ on = [ "<Esc>" ], 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 = [ "<C-q>" ], exec = "close", desc = "Close the current tab, or quit if it is last tab" },
{ on = [ "<C-z>" ], exec = "suspend", desc = "Suspend the process" },

# Navigation
{ on = [ "k" ], exec = "arrow -1", desc = "Move cursor up" },
Expand Down
5 changes: 4 additions & 1 deletion core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::manager::PreviewLock;
static TX: RoCell<UnboundedSender<Event>> = RoCell::new();

pub enum Event {
Quit,
Quit(bool), // no-cwd-file
Key(KeyEvent),
Paste(String),
Render(String),
Expand Down Expand Up @@ -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();
};
Expand Down
8 changes: 4 additions & 4 deletions core/src/manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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));
}
}
});
Expand All @@ -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 {
Expand Down

0 comments on commit 4527044

Please sign in to comment.