use signal handlers to quit instead of key-combo

This commit is contained in:
extrawurst 2023-12-22 16:43:15 +01:00
parent e7c61ffc89
commit d7fdbfdb97
6 changed files with 29 additions and 14 deletions

1
Cargo.lock generated
View file

@ -769,6 +769,7 @@ dependencies = [
"scopetime",
"serde",
"shellexpand",
"signal-hook",
"simplelog",
"struct-patch",
"syntect",

View file

@ -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"

View file

@ -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,

View file

@ -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()),

View file

@ -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<CrosstermBackend<io::Stdout>>,
term: Arc<AtomicBool>,
) -> Result<QuitState, anyhow::Error> {
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;
}
}
}

View file

@ -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,