From 1956e111dd1391c197631de433796a67c1f98e6d Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Fri, 20 Mar 2020 02:08:10 +0100 Subject: [PATCH] better key handling --- src/app.rs | 57 ++++++++++++++++++++++++++--------------------------- src/keys.rs | 12 +++++++---- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/app.rs b/src/app.rs index 832a6faa..00855ce3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,7 +6,7 @@ use crate::{ git_utils::{self, Diff}, keys, strings, }; -use crossterm::event::{Event, KeyCode}; +use crossterm::event::Event; use git2::StatusShow; use itertools::Itertools; use std::{borrow::Cow, path::Path}; @@ -144,33 +144,32 @@ impl App { return; } - if ev == Event::Key(KeyCode::Esc.into()) - || ev == Event::Key(KeyCode::Char('q').into()) - { - self.do_quit = true; - } - - if ev == Event::Key(keys::FOCUS_STATUS) { - self.switch_focus(Focus::Status); - } else if ev == Event::Key(keys::FOCUS_STAGE) { - self.switch_focus(Focus::Stage); - } else if ev == Event::Key(keys::FOCUS_RIGHT) { - self.switch_focus(Focus::Diff); - } else if ev == Event::Key(keys::FOCUS_LEFT) { - self.switch_focus(match self.diff_target { - DiffTarget::Stage => Focus::Stage, - DiffTarget::WorkingDir => Focus::Status, - }); - } - - if let Event::Key(e) = ev { - if e.code == KeyCode::Enter { - self.index_add_remove(); - } - } - - if ev == Event::Key(KeyCode::Char('D').into()) { - self.index_reset(); + if let Event::Key(k) = ev { + match k { + keys::EXIT_1 | keys::EXIT_2 => { + self.do_quit = true + } + keys::FOCUS_STATUS => { + self.switch_focus(Focus::Status) + } + keys::FOCUS_STAGE => { + self.switch_focus(Focus::Stage) + } + keys::FOCUS_RIGHT => { + self.switch_focus(Focus::Diff) + } + keys::FOCUS_LEFT => { + self.switch_focus(match self.diff_target { + DiffTarget::Stage => Focus::Stage, + DiffTarget::WorkingDir => Focus::Status, + }) + } + keys::STATUS_STAGE_FILE => { + self.index_add_remove() + } + keys::STATUS_RESET_FILE => self.index_reset(), + _ => (), + }; } } } @@ -311,7 +310,7 @@ impl App { } fn index_add_remove(&mut self) { - if self.index_wd.focused() { + if self.diff_target == DiffTarget::WorkingDir { if let Some(i) = self.index_wd.selection() { let path = Path::new(i.path.as_str()); diff --git a/src/keys.rs b/src/keys.rs index 17868f66..b2b17603 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -7,7 +7,11 @@ const fn no_mod(code: KeyCode) -> KeyEvent { } } -pub static FOCUS_STATUS: KeyEvent = no_mod(KeyCode::Char('1')); -pub static FOCUS_STAGE: KeyEvent = no_mod(KeyCode::Char('2')); -pub static FOCUS_RIGHT: KeyEvent = no_mod(KeyCode::Right); -pub static FOCUS_LEFT: KeyEvent = no_mod(KeyCode::Left); +pub const FOCUS_STATUS: KeyEvent = no_mod(KeyCode::Char('1')); +pub const FOCUS_STAGE: KeyEvent = no_mod(KeyCode::Char('2')); +pub const FOCUS_RIGHT: KeyEvent = no_mod(KeyCode::Right); +pub const FOCUS_LEFT: KeyEvent = no_mod(KeyCode::Left); +pub const STATUS_RESET_FILE: KeyEvent = no_mod(KeyCode::Char('D')); +pub const STATUS_STAGE_FILE: KeyEvent = no_mod(KeyCode::Enter); +pub const EXIT_1: KeyEvent = no_mod(KeyCode::Esc); +pub const EXIT_2: KeyEvent = no_mod(KeyCode::Char('q'));