Skip to content

Commit d332e7e

Browse files
author
Stephan Dilly
committed
refactor key handling in diff
1 parent e8204d5 commit d332e7e

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/components/diff.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::{CommandBlocking, DrawableComponent};
22
use crate::{
33
components::{CommandInfo, Component},
4+
keys,
45
queue::{InternalEvent, Queue},
56
strings,
67
};
78
use asyncgit::{hash, DiffLine, DiffLineType, FileDiff};
8-
use crossterm::event::{Event, KeyCode, KeyModifiers};
9+
use crossterm::event::Event;
910
use std::{borrow::Cow, cmp, convert::TryFrom};
1011
use strings::commands;
1112
use tui::{
@@ -367,34 +368,26 @@ impl Component for DiffComponent {
367368
fn event(&mut self, ev: Event) -> bool {
368369
if self.focused {
369370
if let Event::Key(e) = ev {
370-
let has_shift =
371-
e.modifiers.contains(KeyModifiers::SHIFT);
372-
return match e.code {
373-
KeyCode::Down if !has_shift => {
371+
return match e {
372+
keys::MOVE_DOWN => {
374373
self.scroll(ScrollType::Down);
375374
true
376375
}
377-
KeyCode::Down if has_shift => {
376+
keys::SHIFT_DOWN | keys::END => {
378377
self.scroll(ScrollType::End);
379378
true
380379
}
381-
KeyCode::End => {
382-
self.scroll(ScrollType::End);
383-
true
384-
}
385-
KeyCode::Up if has_shift => {
386-
self.scroll(ScrollType::Home);
387-
true
388-
}
389-
KeyCode::Home => {
380+
381+
keys::HOME | keys::SHIFT_UP => {
390382
self.scroll(ScrollType::Home);
391383
true
392384
}
393-
KeyCode::Up if !has_shift => {
385+
386+
keys::MOVE_UP => {
394387
self.scroll(ScrollType::Up);
395388
true
396389
}
397-
KeyCode::Enter => {
390+
keys::ENTER => {
398391
self.add_hunk();
399392
true
400393
}

src/keys.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ pub const OPEN_HELP: KeyEvent = no_mod(KeyCode::Char('h'));
2828
pub const MOVE_LEFT: KeyEvent = no_mod(KeyCode::Left);
2929
pub const MOVE_RIGHT: KeyEvent = no_mod(KeyCode::Right);
3030
pub const HOME: KeyEvent = no_mod(KeyCode::Home);
31+
pub const END: KeyEvent = no_mod(KeyCode::End);
3132
pub const MOVE_UP: KeyEvent = no_mod(KeyCode::Up);
3233
pub const MOVE_DOWN: KeyEvent = no_mod(KeyCode::Down);
3334
pub const SHIFT_UP: KeyEvent =
3435
with_mod(KeyCode::Up, KeyModifiers::SHIFT);
36+
pub const SHIFT_DOWN: KeyEvent =
37+
with_mod(KeyCode::Down, KeyModifiers::SHIFT);
38+
pub const ENTER: KeyEvent = no_mod(KeyCode::Enter);
3539
pub const STATUS_STAGE_FILE: KeyEvent = no_mod(KeyCode::Enter);
3640
pub const STATUS_RESET_FILE: KeyEvent =
3741
with_mod(KeyCode::Char('D'), KeyModifiers::SHIFT);

0 commit comments

Comments
 (0)