Skip to content

Commit

Permalink
support home/end to jump up/down (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed May 9, 2020
1 parent 13979c1 commit 6bb54cf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
62 changes: 49 additions & 13 deletions src/components/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
strings,
};
use asyncgit::{hash, DiffLine, DiffLineType, FileDiff};
use crossterm::event::{Event, KeyCode};
use crossterm::event::{Event, KeyCode, KeyModifiers};
use std::{borrow::Cow, cmp, convert::TryFrom};
use strings::commands;
use tui::{
Expand All @@ -24,6 +24,13 @@ struct Current {
hash: u64,
}

enum ScrollType {
Up,
Down,
Home,
End,
}

///
pub struct DiffComponent {
diff: FileDiff,
Expand Down Expand Up @@ -86,15 +93,25 @@ impl DiffComponent {
}
}

fn scroll(&mut self, inc: bool) {
fn scroll(&mut self, scroll: ScrollType) {
let old = self.scroll;
if inc {
self.scroll = cmp::min(
self.diff.lines.saturating_sub(1),
self.scroll.saturating_add(1),
);
} else {
self.scroll = self.scroll.saturating_sub(1);

let scroll_max = self.diff.lines.saturating_sub(1);

match scroll {
ScrollType::Down => {
self.scroll = cmp::min(
scroll_max,
self.scroll.saturating_add(1),
);
}

ScrollType::Up => {
self.scroll = self.scroll.saturating_sub(1);
}

ScrollType::Home => self.scroll = 0,
ScrollType::End => self.scroll = scroll_max,
}

if old != self.scroll {
Expand Down Expand Up @@ -322,6 +339,15 @@ impl Component for DiffComponent {
self.focused,
));

out.push(
CommandInfo::new(
commands::DIFF_HOME_END,
self.can_scroll(),
self.focused,
)
.hidden(),
);

let cmd_text = if self.current.is_stage {
commands::DIFF_HUNK_REMOVE
} else {
Expand All @@ -340,13 +366,23 @@ impl Component for DiffComponent {
fn event(&mut self, ev: Event) -> bool {
if self.focused {
if let Event::Key(e) = ev {
let has_shift =
e.modifiers.contains(KeyModifiers::SHIFT);
return match e.code {
KeyCode::Down => {
self.scroll(true);
KeyCode::Down if !has_shift => {
self.scroll(ScrollType::Down);
true
}
KeyCode::End | KeyCode::Down if has_shift => {
self.scroll(ScrollType::End);
true
}
KeyCode::Home | KeyCode::Up if has_shift => {
self.scroll(ScrollType::Home);
true
}
KeyCode::Up => {
self.scroll(false);
KeyCode::Up if !has_shift => {
self.scroll(ScrollType::Up);
true
}
KeyCode::Enter => {
Expand Down
6 changes: 6 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ pub mod commands {
CMD_GROUP_GENERAL,
);
///
pub static DIFF_HOME_END: CommandText = CommandText::new(
"Scroll [Home,End]",
"scroll to top or bottom",
CMD_GROUP_DIFF,
);
///
pub static DIFF_HUNK_ADD: CommandText = CommandText::new(
"Add hunk [enter]",
"adds selected hunk to stage",
Expand Down

0 comments on commit 6bb54cf

Please sign in to comment.