Skip to content

Commit

Permalink
add the ability to jump to a particular line
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Apr 15, 2024
1 parent 0170923 commit 7ae540f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ pub fn handle_key_events(
app.mode = Mode::Normal;
navigation::go_up_or_down_in_data(app, Direction::Up(n));
return Ok(TransitionResult::Continue);
} else if key_event.code == KeyCode::Char('g') {
app.mode = Mode::Normal;
navigation::go_up_or_down_in_data(app, Direction::At(n));
return Ok(TransitionResult::Continue);
}
}
Mode::Insert => {
Expand Down
10 changes: 10 additions & 0 deletions src/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum Direction {
Top,
/// go to the bottom of the data, i.e. the last element or the last key
Bottom,
/// go at a particular line in the data
At(usize),
}

/// go up or down in the data
Expand Down Expand Up @@ -67,6 +69,7 @@ pub(super) fn go_up_or_down_in_data(app: &mut App, direction: Direction) {
Direction::Down(step) => val.saturating_add(step).min(vals.len() - 1),
Direction::Top => 0,
Direction::Bottom => vals.len() - 1,
Direction::At(id) => id.min(vals.len() - 1),
}
},
span,
Expand Down Expand Up @@ -97,6 +100,7 @@ pub(super) fn go_up_or_down_in_data(app: &mut App, direction: Direction) {
}
Direction::Top => 0,
Direction::Bottom => cols.len() - 1,
Direction::At(id) => id.min(cols.len() - 1),
};

cols[new_index].clone()
Expand Down Expand Up @@ -207,6 +211,9 @@ mod tests {
(Direction::Bottom, 2),
(Direction::Bottom, 2),
(Direction::Top, 0),
(Direction::At(0), 0),
(Direction::At(1), 1),
(Direction::At(2), 2),
];
for (direction, id) in sequence {
go_up_or_down_in_data(&mut app, direction);
Expand Down Expand Up @@ -235,6 +242,9 @@ mod tests {
(Direction::Bottom, "c"),
(Direction::Bottom, "c"),
(Direction::Top, "a"),
(Direction::At(0), "a"),
(Direction::At(1), "b"),
(Direction::At(2), "c"),
];
for (direction, id) in sequence {
go_up_or_down_in_data(&mut app, direction);
Expand Down

0 comments on commit 7ae540f

Please sign in to comment.