From 64e9b000cf4b090b820bd5022569fbe3e3103731 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Thu, 14 Apr 2022 13:18:58 +0900 Subject: [PATCH] adding keymap '$' and '^' for move line in table --- README.md | 1 + src/components/command.rs | 10 ++++++++ src/components/table.rs | 52 +++++++++++++++++++++++++++++++++++++++ src/config.rs | 4 +++ 4 files changed, 67 insertions(+) diff --git a/README.md b/README.md index 39b6ec9..ef97f55 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ If you want to add connections, you need to edit your config file. For more info | h, j, k, l | Scroll left/down/up/right | | Ctrl + u, Ctrl + d | Scroll up/down multiple lines | | g , G | Scroll to top/bottom | +| ^, $ | Move to head/tail of line | | H, J, K, L | Extend selection by one cell left/down/up/right | | y | Copy a cell value | | , | Move focus to left/right | diff --git a/src/components/command.rs b/src/components/command.rs index 961b8c6..62d4c8f 100644 --- a/src/components/command.rs +++ b/src/components/command.rs @@ -62,6 +62,16 @@ pub fn scroll_to_top_bottom(key: &KeyConfig) -> CommandText { ) } +pub fn move_to_head_tail_of_line(key: &KeyConfig) -> CommandText { + CommandText::new( + format!( + "Move to head/tail of line [{},{}]", + key.move_to_head_of_line, key.move_to_tail_of_line, + ), + CMD_GROUP_TABLE, + ) +} + pub fn expand_collapse(key: &KeyConfig) -> CommandText { CommandText::new( format!("Expand/Collapse [{},{}]", key.scroll_right, key.scroll_left,), diff --git a/src/components/table.rs b/src/components/table.rs index 0c5da62..a7ad79a 100644 --- a/src/components/table.rs +++ b/src/components/table.rs @@ -140,6 +140,15 @@ impl TableComponent { .select(Some(self.rows.len().saturating_sub(1))); } + fn move_to_head_of_line(&mut self) { + self.selected_column = 0; + } + + fn move_to_tail_of_line(&mut self) { + let vertical_length = self.headers.len().saturating_sub(1); + self.selected_column = vertical_length; + } + fn next_column(&mut self) { if self.rows.is_empty() { return; @@ -522,6 +531,9 @@ impl Component for TableComponent { out.push(CommandInfo::new(command::extend_selection_by_one_cell( &self.key_config, ))); + out.push(CommandInfo::new(command::move_to_head_tail_of_line( + &self.key_config, + ))); } fn event(&mut self, key: Key) -> Result { @@ -546,6 +558,10 @@ impl Component for TableComponent { } else if key == self.key_config.scroll_to_bottom { self.scroll_to_bottom(); return Ok(EventState::Consumed); + } else if key == self.key_config.move_to_head_of_line { + self.move_to_head_of_line(); + } else if key == self.key_config.move_to_tail_of_line { + self.move_to_tail_of_line(); } else if key == self.key_config.scroll_right { self.next_column(); return Ok(EventState::Consumed); @@ -779,6 +795,42 @@ mod test { assert!(!component.is_selected_cell(1, 3, 1)); } + #[test] + fn test_move_to_head_of_line() { + let mut component = TableComponent::new(KeyConfig::default()); + + component.headers = vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(); + component.rows = vec![ + vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(), + vec!["g", "h", "i"].iter().map(|h| h.to_string()).collect(), + ]; + + // cursor returns to the top. + component.expand_selected_area_y(true); + component.expand_selected_area_y(true); + component.move_to_head_of_line(); + assert_eq!(component.selected_column, 0); + } + + #[test] + fn test_move_to_tail_of_line() { + let mut component = TableComponent::new(KeyConfig::default()); + + // if component does not have a header, cursor is not moved. + component.move_to_head_of_line(); + assert_eq!(component.selected_column, 0); + + // if component has a header, cursor is moved to tail of line. + component.headers = vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(); + component.rows = vec![ + vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(), + vec!["g", "h", "i"].iter().map(|h| h.to_string()).collect(), + ]; + + component.move_to_tail_of_line(); + assert_eq!(component.selected_column, 2); + } + #[test] fn test_calculate_cell_widths_when_sum_of_cell_widths_is_greater_than_table_width() { let mut component = TableComponent::new(KeyConfig::default()); diff --git a/src/config.rs b/src/config.rs index 3d41831..ff92adc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -101,6 +101,8 @@ pub struct KeyConfig { pub scroll_up_multiple_lines: Key, pub scroll_to_top: Key, pub scroll_to_bottom: Key, + pub move_to_head_of_line: Key, + pub move_to_tail_of_line: Key, pub extend_selection_by_one_cell_left: Key, pub extend_selection_by_one_cell_right: Key, pub extend_selection_by_one_cell_up: Key, @@ -140,6 +142,8 @@ impl Default for KeyConfig { scroll_up_multiple_lines: Key::Ctrl('u'), scroll_to_top: Key::Char('g'), scroll_to_bottom: Key::Char('G'), + move_to_head_of_line: Key::Char('^'), + move_to_tail_of_line: Key::Char('$'), extend_selection_by_one_cell_left: Key::Char('H'), extend_selection_by_one_cell_right: Key::Char('L'), extend_selection_by_one_cell_down: Key::Char('J'),