adding keymap '$' and '^' for move line in table

pull/146/head
kyoto7250 2 years ago
parent b13e4bb255
commit 64e9b000cf

@ -103,6 +103,7 @@ If you want to add connections, you need to edit your config file. For more info
| <kbd>h</kbd>, <kbd>j</kbd>, <kbd>k</kbd>, <kbd>l</kbd> | Scroll left/down/up/right |
| <kbd>Ctrl</kbd> + <kbd>u</kbd>, <kbd>Ctrl</kbd> + <kbd>d</kbd> | Scroll up/down multiple lines |
| <kbd>g</kbd> , <kbd>G</kbd> | Scroll to top/bottom |
| <kbd>^</kbd>, <kbd>$</kbd> | Move to head/tail of line |
| <kbd>H</kbd>, <kbd>J</kbd>, <kbd>K</kbd>, <kbd>L</kbd> | Extend selection by one cell left/down/up/right |
| <kbd>y</kbd> | Copy a cell value |
| <kbd></kbd>, <kbd></kbd> | Move focus to left/right |

@ -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,),

@ -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<EventState> {
@ -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());

@ -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'),

Loading…
Cancel
Save