Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding key '$' and '^' for moving line in table #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
10 changes: 10 additions & 0 deletions src/components/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,),
Expand Down
52 changes: 52 additions & 0 deletions src/components/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
Expand Down