diff --git a/KEY_CONFIG.md b/KEY_CONFIG.md index 658aad11b6..345d5fec0e 100644 --- a/KEY_CONFIG.md +++ b/KEY_CONFIG.md @@ -19,6 +19,14 @@ Create a `key_bindings.ron` file like this: ) ``` +Horizontal diff paging defaults to `Ctrl+Left` and `Ctrl+Right`. Override +`page_left` and `page_right` to use different bindings: +``` +( + page_left: Some(( code: Left, modifiers: "ALT")), + page_right: Some(( code: Right, modifiers: "ALT")), +) + The config file format based on the [Ron file format](https://github.com/ron-rs/ron). The location of the file depends on your OS: * `$HOME/.config/gitui/key_bindings.ron` (mac) diff --git a/src/components/diff.rs b/src/components/diff.rs index 04779caada..b48695fb13 100644 --- a/src/components/diff.rs +++ b/src/components/diff.rs @@ -750,8 +750,8 @@ impl Component for DiffComponent { _force_all: bool, ) -> CommandBlocking { out.push(CommandInfo::new( - strings::commands::scroll(&self.key_config), - self.can_scroll(), + strings::commands::diff_scroll(&self.key_config), + self.can_scroll() || self.max_scroll_right() > 0, self.focused(), )); out.push(CommandInfo::new( @@ -861,6 +861,22 @@ impl Component for DiffComponent { { self.move_selection(ScrollType::PageDown); Ok(EventState::Consumed) + } else if key_match( + e, + self.key_config.keys.page_right, + ) { + self.horizontal_scroll.move_page( + HorizontalScrollType::Right, + self.current_size.get().0.into(), + ); + Ok(EventState::Consumed) + } else if key_match(e, self.key_config.keys.page_left) + { + self.horizontal_scroll.move_page( + HorizontalScrollType::Left, + self.current_size.get().0.into(), + ); + Ok(EventState::Consumed) } else if key_match( e, self.key_config.keys.move_right, diff --git a/src/components/utils/scroll_horizontal.rs b/src/components/utils/scroll_horizontal.rs index 9ee7e5a397..bb9847697f 100644 --- a/src/components/utils/scroll_horizontal.rs +++ b/src/components/utils/scroll_horizontal.rs @@ -49,6 +49,34 @@ impl HorizontalScroll { true } + pub fn move_page( + &self, + move_type: HorizontalScrollType, + page_width: usize, + ) -> bool { + let old = self.right.get(); + let max = self.max_right.get(); + let page_width = page_width.saturating_sub(1).max(1); + + let new_scroll_right = match move_type { + HorizontalScrollType::Left => { + old.saturating_sub(page_width) + } + HorizontalScrollType::Right => { + old.saturating_add(page_width) + } + }; + + let new_scroll_right = new_scroll_right.clamp(0, max); + + if new_scroll_right == old { + return false; + } + + self.right.set(new_scroll_right); + + true + } pub fn update( &self, selection: usize, @@ -130,4 +158,15 @@ mod tests { fn test_scroll_zero_width() { assert_eq!(calc_scroll_right(4, 0, 4, 3), 0); } + + #[test] + fn test_scroll_page_left_right() { + let scroll = HorizontalScroll::new(); + scroll.update_no_selection(100, 20); + + assert!(scroll.move_page(HorizontalScrollType::Right, 20)); + assert_eq!(scroll.get_right(), 19); + assert!(scroll.move_page(HorizontalScrollType::Left, 20)); + assert_eq!(scroll.get_right(), 0); + } } diff --git a/src/keys/key_list.rs b/src/keys/key_list.rs index 24a9507a49..496d0797bb 100644 --- a/src/keys/key_list.rs +++ b/src/keys/key_list.rs @@ -64,6 +64,8 @@ pub struct KeysList { pub popup_down: GituiKeyEvent, pub page_down: GituiKeyEvent, pub page_up: GituiKeyEvent, + pub page_left: GituiKeyEvent, + pub page_right: GituiKeyEvent, pub shift_up: GituiKeyEvent, pub shift_down: GituiKeyEvent, pub enter: GituiKeyEvent, @@ -162,6 +164,8 @@ impl Default for KeysList { popup_down: GituiKeyEvent::new(KeyCode::Down, KeyModifiers::empty()), page_down: GituiKeyEvent::new(KeyCode::PageDown, KeyModifiers::empty()), page_up: GituiKeyEvent::new(KeyCode::PageUp, KeyModifiers::empty()), + page_left: GituiKeyEvent::new(KeyCode::Left, KeyModifiers::CONTROL), + page_right: GituiKeyEvent::new(KeyCode::Right, KeyModifiers::CONTROL), shift_up: GituiKeyEvent::new(KeyCode::Up, KeyModifiers::SHIFT), shift_down: GituiKeyEvent::new(KeyCode::Down, KeyModifiers::SHIFT), enter: GituiKeyEvent::new(KeyCode::Enter, KeyModifiers::empty()), diff --git a/src/strings.rs b/src/strings.rs index 93496cd2ca..cc34faec26 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -649,6 +649,21 @@ pub mod commands { CMD_GROUP_GENERAL, ) } + pub fn diff_scroll(key_config: &SharedKeyConfig) -> CommandText { + CommandText::new( + format!( + "Scroll [{}{}{}{}{}{}]", + key_config.get_hint(key_config.keys.move_up), + key_config.get_hint(key_config.keys.move_down), + key_config.get_hint(key_config.keys.page_up), + key_config.get_hint(key_config.keys.page_down), + key_config.get_hint(key_config.keys.page_left), + key_config.get_hint(key_config.keys.page_right) + ), + "scroll vertically or horizontally in diff", + CMD_GROUP_DIFF, + ) + } pub fn commit_list_mark( key_config: &SharedKeyConfig, marked: bool,