summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuvi Panda <yuvipanda@gmail.com>2021-05-10 17:25:29 +0530
committerGitHub <noreply@github.com>2021-05-10 12:55:29 +0100
commitc02934d184cca5d24a628d2e5ec601e12db2a630 (patch)
tree644c6014991b306682fefdf4db196122c3e36b65
parentf0463326fa456c96c78e4d3f78106d0312fec588 (diff)
Implement 'quick access' via Alt-<n> (#79)
* Implement 'quick access' via numbers Puts numbers 0-9 next to commands *above* current selection. Ctrl-<number> should activate them - but since Ctrl-<num> 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+<n> to select last nth command * Don't print Opt+0 Same as <Enter> * Run rustfmt * Simplify code - Use ? operator for getting selected item - Use RangeInclusive to check if character pressed is a number
-rw-r--r--src/command/search.rs37
1 files 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();