From d7fdbfdb972668d9574127d136378191f302d716 Mon Sep 17 00:00:00 2001 From: extrawurst Date: 2023年12月22日 16:43:15 +0100 Subject: [PATCH] use signal handlers to quit instead of key-combo --- Cargo.lock | 1 + Cargo.toml | 1 + src/app.rs | 12 +----------- src/keys/key_list.rs | 2 -- src/main.rs | 25 +++++++++++++++++++++++++ src/strings.rs | 2 +- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 30cdfc0f8a..22a34cad25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -769,6 +769,7 @@ dependencies = [ "scopetime", "serde", "shellexpand", + "signal-hook", "simplelog", "struct-patch", "syntect", diff --git a/Cargo.toml b/Cargo.toml index ce834bdf0b..5c8d4a3c05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ categories = ["command-line-utilities"] keywords = ["git", "gui", "cli", "terminal", "ui"] [dependencies] +signal-hook = "0.3" anyhow = "1.0" asyncgit = { path = "./asyncgit", version = "0.24", default-features = false } backtrace = "0.3" diff --git a/src/app.rs b/src/app.rs index 2bdb2754ef..905fdd2346 100644 --- a/src/app.rs +++ b/src/app.rs @@ -405,7 +405,7 @@ impl App { log::trace!("event: {:?}", ev); if let InputEvent::Input(ev) = ev { - if self.check_hard_exit(&ev) || self.check_quit(&ev) { + if self.check_quit(&ev) { return Ok(()); } @@ -670,16 +670,6 @@ impl App { false } - fn check_hard_exit(&mut self, ev: &Event) -> bool { - if let Event::Key(e) = ev { - if key_match(e, self.key_config.keys.exit) { - self.do_quit = QuitState::Close; - return true; - } - } - false - } - fn get_tabs(&mut self) -> Vec<&mut dyn Component> { vec![ &mut self.status_tab, diff --git a/src/keys/key_list.rs b/src/keys/key_list.rs index 5530719330..329499589b 100644 --- a/src/keys/key_list.rs +++ b/src/keys/key_list.rs @@ -45,7 +45,6 @@ pub struct KeysList { pub tab_toggle: GituiKeyEvent, pub tab_toggle_reverse: GituiKeyEvent, pub toggle_workarea: GituiKeyEvent, - pub exit: GituiKeyEvent, pub quit: GituiKeyEvent, pub exit_popup: GituiKeyEvent, pub open_commit: GituiKeyEvent, @@ -134,7 +133,6 @@ impl Default for KeysList { tab_toggle: GituiKeyEvent::new(KeyCode::Tab, KeyModifiers::empty()), tab_toggle_reverse: GituiKeyEvent::new(KeyCode::BackTab, KeyModifiers::SHIFT), toggle_workarea: GituiKeyEvent::new(KeyCode::Char('w'), KeyModifiers::empty()), - exit: GituiKeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL), quit: GituiKeyEvent::new(KeyCode::Char('q'), KeyModifiers::empty()), exit_popup: GituiKeyEvent::new(KeyCode::Esc, KeyModifiers::empty()), open_commit: GituiKeyEvent::new(KeyCode::Char('c'), KeyModifiers::empty()), diff --git a/src/main.rs b/src/main.rs index c0f41a2770..d0f8780fa9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,6 +76,10 @@ use std::{ cell::RefCell, io::{self, Write}, panic, process, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, time::{Duration, Instant}, }; use ui::style::Theme; @@ -147,6 +151,20 @@ fn main() -> Result<()> { let mut repo_path = cliargs.repo_path; let input = Input::new(); + let term = Arc::new(AtomicBool::new(false)); + signal_hook::flag::register( + signal_hook::consts::SIGTERM, + Arc::clone(&term), + )?; + signal_hook::flag::register( + signal_hook::consts::SIGINT, + Arc::clone(&term), + )?; + signal_hook::flag::register( + signal_hook::consts::SIGQUIT, + Arc::clone(&term), + )?; + let updater = if cliargs.notify_watcher { Updater::NotifyWatcher } else { @@ -162,6 +180,7 @@ fn main() -> Result<()> { &input, updater, &mut terminal, + term.clone(), )?; match quit_state { @@ -183,6 +202,7 @@ fn run_app( input: &Input, updater: Updater, terminal: &mut Terminal>, + term: Arc, ) -> Result { let (tx_git, rx_git) = unbounded(); let (tx_app, rx_app) = unbounded(); @@ -274,6 +294,11 @@ fn run_app( if app.is_quit() { break; } + + if term.load(Ordering::Relaxed) { + log::info!("signal received. exiting"); + break; + } } } diff --git a/src/strings.rs b/src/strings.rs index 6c3b061a65..6164413117 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -1117,7 +1117,7 @@ pub mod commands { CommandText::new( format!( "Quit [{}]", - key_config.get_hint(key_config.keys.exit), + key_config.get_hint(key_config.keys.quit), ), "quit gitui application", CMD_GROUP_GENERAL,

AltStyle によって変換されたページ (->オリジナル) /