Add key bindings to go to top or bottom of the list

This commit is contained in:
FichteFoll 2021-12-06 18:53:49 +01:00
parent 82df932a99
commit a7696f34cc
2 changed files with 16 additions and 1 deletions

View File

@ -12,6 +12,10 @@ pub trait ListView<T: fmt::Display + Clone> {
fn normal_mode(&mut self);
// selection manipulation
fn selection_first(&mut self) {
*self.selection_pointer() = 0;
}
fn selection_up(&mut self) {
*self.selection_pointer() = self.selection_pointer().saturating_sub(1);
}
@ -21,6 +25,10 @@ pub trait ListView<T: fmt::Display + Clone> {
(*self.selection_pointer() + 1).min(self.list().len().saturating_sub(1));
}
fn selection_last(&mut self) {
*self.selection_pointer() = self.list().len().saturating_sub(1);
}
// adding/removing elements
fn insert(&mut self, item: T, position: Option<usize>) {
let pos = position.unwrap_or(*self.selection_pointer());

View File

@ -51,7 +51,7 @@ impl Tracc {
Focus::Bottom => $action(&mut self.times, $($arg,)*),
}
};
};
}
let mut inputs = io::stdin().keys();
loop {
@ -63,6 +63,13 @@ impl Tracc {
Key::Char('q') => break,
Key::Char('j') => with_focused!(ListView::selection_down),
Key::Char('k') => with_focused!(ListView::selection_up),
Key::Char('G') => with_focused!(ListView::selection_first),
// gg
Key::Char('g') => {
if let Some(Ok(Key::Char('g'))) = inputs.next() {
with_focused!(ListView::selection_last);
}
}
Key::Char('o') => {
with_focused!(ListView::insert, Default::default(), None);
self.set_mode(Mode::Insert)?;