From c02934d184cca5d24a628d2e5ec601e12db2a630 Mon Sep 17 00:00:00 2001 From: Yuvi Panda Date: Mon, 10 May 2021 17:25:29 +0530 Subject: Implement 'quick access' via Alt- (#79) * Implement 'quick access' via numbers Puts numbers 0-9 next to commands *above* current selection. Ctrl- should activate them - but since Ctrl- are reserved by terminal, this does not currently work. Need to find different sets of keyboard shortcuts. Numbers are *above* current selection, since the user must use the arrow keys to go over the commands below current selection before reaching selection. * Use Alt+ to select last nth command * Don't print Opt+0 Same as * Run rustfmt * Simplify code - Use ? operator for getting selected item - Use RangeInclusive to check if character pressed is a number --- src/command/search.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/command/search.rs b/src/command/search.rs index f8a4a1ba..2dcdecc8 100644 --- a/src/command/search.rs +++ b/src/command/search.rs @@ -97,6 +97,23 @@ impl State { ago = format!(" {}", ago); } + let selected_index = match self.results_state.selected() { + None => Span::raw(" "), + Some(selected) => match i.checked_sub(selected) { + None => Span::raw(" "), + Some(diff) => { + if 0 < diff && diff < 10 { + Span::styled( + format!(" {} ", diff), + Style::default().fg(Color::DarkGray), + ) + } else { + Span::raw(" ") + } + } + }, + }; + let duration = Span::styled( duration, Style::default().fg(if m.exit == 0 || m.duration == -1 { @@ -115,8 +132,14 @@ impl State { } } - let spans = - Spans::from(vec![duration, Span::raw(" "), ago, Span::raw(" "), command]); + let spans = Spans::from(vec![ + selected_index, + duration, + Span::raw(" "), + ago, + Span::raw(" "), + command, + ]); ListItem::new(spans) }) @@ -171,6 +194,16 @@ async fn key_handler( .map_or(app.input.clone(), |h| h.command.clone()), ); } + Key::Alt(c) if ('1'..='9').contains(&c) => { + let c = c.to_digit(10)? as usize; + let i = app.results_state.selected()? + c; + + return Some( + app.results + .get(i) + .map_or(app.input.clone(), |h| h.command.clone()), + ); + } Key::Char(c) => { app.input.push(c); query_results(app, search_mode, db).await.unwrap(); -- cgit v1.2.3