From af0e59c9d4822031c52264721c63022e6dc59ac9 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:54:39 +0900 Subject: [PATCH 1/4] add key for selecting horizontal line --- README.md | 1 + src/components/command.rs | 10 ++++++++++ src/components/table.rs | 18 ++++++++++++++++++ src/config.rs | 2 ++ 4 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 39b6ec9..cc3f3e1 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ If you want to add connections, you need to edit your config file. For more info | Ctrl + u, Ctrl + d | Scroll up/down multiple lines | | g , G | Scroll to top/bottom | | H, J, K, L | Extend selection by one cell left/down/up/right | +| V | Extend selection by horizontal line | | y | Copy a cell value | | , | Move focus to left/right | | c | Move focus to connections | diff --git a/src/components/command.rs b/src/components/command.rs index 961b8c6..477aa1e 100644 --- a/src/components/command.rs +++ b/src/components/command.rs @@ -96,6 +96,16 @@ pub fn extend_selection_by_one_cell(key: &KeyConfig) -> CommandText { ) } +pub fn extend_selection_by_line(key: &KeyConfig) -> CommandText { + CommandText::new( + format!( + "Extend selection by horizontal line [{}]", + key.extend_selection_by_horizontal_line, + ), + CMD_GROUP_TABLE, + ) +} + pub fn extend_or_shorten_widget_width(key: &KeyConfig) -> CommandText { CommandText::new( format!( diff --git a/src/components/table.rs b/src/components/table.rs index 0c5da62..c1c9b50 100644 --- a/src/components/table.rs +++ b/src/components/table.rs @@ -200,6 +200,18 @@ impl TableComponent { } } + fn expand_selection_by_horizontal_line(&mut self, _positive: bool) { + if self.selection_area_corner.is_none() { + self.selected_column = 0; + self.selection_area_corner = Some(( + self.headers.len().saturating_sub(1), + self.selected_row.selected().unwrap_or(0), + )); + } else { + self.selection_area_corner = None; + } + } + pub fn selected_cells(&self) -> Option { if let Some((x, y)) = self.selection_area_corner { let selected_row_index = self.selected_row.selected()?; @@ -522,6 +534,9 @@ impl Component for TableComponent { out.push(CommandInfo::new(command::extend_selection_by_one_cell( &self.key_config, ))); + out.push(CommandInfo::new(command::extend_selection_by_line( + &self.key_config, + ))); } fn event(&mut self, key: Key) -> Result { @@ -561,6 +576,9 @@ impl Component for TableComponent { } else if key == self.key_config.extend_selection_by_one_cell_right { self.expand_selected_area_x(true); return Ok(EventState::Consumed); + } else if key == self.key_config.extend_selection_by_horizontal_line { + self.expand_selection_by_horizontal_line(true); + return Ok(EventState::Consumed); } Ok(EventState::NotConsumed) } diff --git a/src/config.rs b/src/config.rs index 3d41831..5e98bf3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -105,6 +105,7 @@ pub struct KeyConfig { pub extend_selection_by_one_cell_right: Key, pub extend_selection_by_one_cell_up: Key, pub extend_selection_by_one_cell_down: Key, + pub extend_selection_by_horizontal_line: Key, pub tab_records: Key, pub tab_columns: Key, pub tab_constraints: Key, @@ -144,6 +145,7 @@ impl Default for KeyConfig { extend_selection_by_one_cell_right: Key::Char('L'), extend_selection_by_one_cell_down: Key::Char('J'), extend_selection_by_one_cell_up: Key::Char('K'), + extend_selection_by_horizontal_line: Key::Char('V'), tab_records: Key::Char('1'), tab_properties: Key::Char('2'), tab_sql_editor: Key::Char('3'), From b75821f1d765373bfb0cb351f82dca1992cd066b Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Fri, 8 Apr 2022 10:07:30 +0900 Subject: [PATCH 2/4] Change function name and remove unused argument --- src/components/table.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/table.rs b/src/components/table.rs index c1c9b50..5855f72 100644 --- a/src/components/table.rs +++ b/src/components/table.rs @@ -200,7 +200,7 @@ impl TableComponent { } } - fn expand_selection_by_horizontal_line(&mut self, _positive: bool) { + fn expand_selected_by_horizontal_line(&mut self) { if self.selection_area_corner.is_none() { self.selected_column = 0; self.selection_area_corner = Some(( @@ -577,7 +577,7 @@ impl Component for TableComponent { self.expand_selected_area_x(true); return Ok(EventState::Consumed); } else if key == self.key_config.extend_selection_by_horizontal_line { - self.expand_selection_by_horizontal_line(true); + self.expand_selected_by_horizontal_line(); return Ok(EventState::Consumed); } Ok(EventState::NotConsumed) From 047286c1827b8274f87fd0e8857a7a5ffafc8146 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Fri, 8 Apr 2022 11:04:27 +0900 Subject: [PATCH 3/4] If already selected, V key extend the range and selected_column is not change because this key match movements with other keys --- src/components/table.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/components/table.rs b/src/components/table.rs index 5855f72..80a8e37 100644 --- a/src/components/table.rs +++ b/src/components/table.rs @@ -201,14 +201,17 @@ impl TableComponent { } fn expand_selected_by_horizontal_line(&mut self) { - if self.selection_area_corner.is_none() { - self.selected_column = 0; - self.selection_area_corner = Some(( - self.headers.len().saturating_sub(1), - self.selected_row.selected().unwrap_or(0), - )); + let horizontal_length = self.headers.len().saturating_sub(1); + let vertical_length = self.selected_row.selected().unwrap_or(0); + + if let Some((x, y)) = self.selection_area_corner { + if x == horizontal_length { + self.selection_area_corner = None; + } else { + self.selection_area_corner = Some((horizontal_length, y)); + } } else { - self.selection_area_corner = None; + self.selection_area_corner = Some((horizontal_length, vertical_length)); } } From 781a1822aaf606a4182defa3dcc123350abeac22 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Fri, 8 Apr 2022 11:04:49 +0900 Subject: [PATCH 4/4] adding test for expand_selected_by_horizontal_line --- src/components/table.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/components/table.rs b/src/components/table.rs index 80a8e37..dbea5dc 100644 --- a/src/components/table.rs +++ b/src/components/table.rs @@ -707,6 +707,32 @@ mod test { assert_eq!(component.selected_cells(), Some("b\ne".to_string())); } + #[test] + fn test_expand_selected_by_horizontal_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(), + ]; + + // select one line + component.selected_row.select(Some(0)); + component.expand_selected_by_horizontal_line(); + assert_eq!(component.selection_area_corner, Some((2, 0))); + assert_eq!(component.selected_cells(), Some("d,e,f".to_string())); + + // undo select horizontal line + component.expand_selected_by_horizontal_line(); + assert_eq!(component.selection_area_corner, None); + + // select two line + component.expand_selected_area_y(true); + component.expand_selected_by_horizontal_line(); + assert_eq!(component.selection_area_corner, Some((2, 1))); + assert_eq!(component.selected_cells(), Some("d,e,f\ng,h,i".to_string())); + } + #[test] fn test_is_number_column() { let mut component = TableComponent::new(KeyConfig::default());