From 6bb54cfa8cbebecc59fecef05539d579b7cdfc4c Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sat, 9 May 2020 22:55:02 +0200 Subject: [PATCH] support home/end to jump up/down (#43) --- src/components/diff.rs | 62 +++++++++++++++++++++++++++++++++--------- src/strings.rs | 6 ++++ 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/components/diff.rs b/src/components/diff.rs index ff716420e2..584baf0b06 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -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::{ @@ -24,6 +24,13 @@ struct Current { hash: u64, } +enum ScrollType { + Up, + Down, + Home, + End, +} + /// pub struct DiffComponent { diff: FileDiff, @@ -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 { @@ -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 { @@ -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 => { diff --git a/src/strings.rs b/src/strings.rs index 691583c7ff..1ccbea823d 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -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",