From 7ae540f5664ecdbbc9589b0a68a7377cb73e2bb1 Mon Sep 17 00:00:00 2001 From: amtoine Date: Sat, 13 Apr 2024 19:07:33 +0200 Subject: [PATCH] add the ability to jump to a particular line --- src/handler.rs | 4 ++++ src/navigation.rs | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/handler.rs b/src/handler.rs index cd1163a..c4876cd 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -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 => { diff --git a/src/navigation.rs b/src/navigation.rs index d9859c6..621493b 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -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 @@ -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, @@ -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() @@ -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); @@ -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);