Skip to content

Commit 6bb54cf

Browse files
author
Stephan Dilly
committed
support home/end to jump up/down (#43)
1 parent 13979c1 commit 6bb54cf

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

src/components/diff.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
strings,
66
};
77
use asyncgit::{hash, DiffLine, DiffLineType, FileDiff};
8-
use crossterm::event::{Event, KeyCode};
8+
use crossterm::event::{Event, KeyCode, KeyModifiers};
99
use std::{borrow::Cow, cmp, convert::TryFrom};
1010
use strings::commands;
1111
use tui::{
@@ -24,6 +24,13 @@ struct Current {
2424
hash: u64,
2525
}
2626

27+
enum ScrollType {
28+
Up,
29+
Down,
30+
Home,
31+
End,
32+
}
33+
2734
///
2835
pub struct DiffComponent {
2936
diff: FileDiff,
@@ -86,15 +93,25 @@ impl DiffComponent {
8693
}
8794
}
8895

89-
fn scroll(&mut self, inc: bool) {
96+
fn scroll(&mut self, scroll: ScrollType) {
9097
let old = self.scroll;
91-
if inc {
92-
self.scroll = cmp::min(
93-
self.diff.lines.saturating_sub(1),
94-
self.scroll.saturating_add(1),
95-
);
96-
} else {
97-
self.scroll = self.scroll.saturating_sub(1);
98+
99+
let scroll_max = self.diff.lines.saturating_sub(1);
100+
101+
match scroll {
102+
ScrollType::Down => {
103+
self.scroll = cmp::min(
104+
scroll_max,
105+
self.scroll.saturating_add(1),
106+
);
107+
}
108+
109+
ScrollType::Up => {
110+
self.scroll = self.scroll.saturating_sub(1);
111+
}
112+
113+
ScrollType::Home => self.scroll = 0,
114+
ScrollType::End => self.scroll = scroll_max,
98115
}
99116

100117
if old != self.scroll {
@@ -322,6 +339,15 @@ impl Component for DiffComponent {
322339
self.focused,
323340
));
324341

342+
out.push(
343+
CommandInfo::new(
344+
commands::DIFF_HOME_END,
345+
self.can_scroll(),
346+
self.focused,
347+
)
348+
.hidden(),
349+
);
350+
325351
let cmd_text = if self.current.is_stage {
326352
commands::DIFF_HUNK_REMOVE
327353
} else {
@@ -340,13 +366,23 @@ impl Component for DiffComponent {
340366
fn event(&mut self, ev: Event) -> bool {
341367
if self.focused {
342368
if let Event::Key(e) = ev {
369+
let has_shift =
370+
e.modifiers.contains(KeyModifiers::SHIFT);
343371
return match e.code {
344-
KeyCode::Down => {
345-
self.scroll(true);
372+
KeyCode::Down if !has_shift => {
373+
self.scroll(ScrollType::Down);
374+
true
375+
}
376+
KeyCode::End | KeyCode::Down if has_shift => {
377+
self.scroll(ScrollType::End);
378+
true
379+
}
380+
KeyCode::Home | KeyCode::Up if has_shift => {
381+
self.scroll(ScrollType::Home);
346382
true
347383
}
348-
KeyCode::Up => {
349-
self.scroll(false);
384+
KeyCode::Up if !has_shift => {
385+
self.scroll(ScrollType::Up);
350386
true
351387
}
352388
KeyCode::Enter => {

src/strings.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ pub mod commands {
4343
CMD_GROUP_GENERAL,
4444
);
4545
///
46+
pub static DIFF_HOME_END: CommandText = CommandText::new(
47+
"Scroll [Home,End]",
48+
"scroll to top or bottom",
49+
CMD_GROUP_DIFF,
50+
);
51+
///
4652
pub static DIFF_HUNK_ADD: CommandText = CommandText::new(
4753
"Add hunk [enter]",
4854
"adds selected hunk to stage",

0 commit comments

Comments
 (0)