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
This commit is contained in:
Yuvi Panda 2021-05-10 17:25:29 +05:30 committed by GitHub
parent f0463326fa
commit c02934d184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,6 +97,23 @@ impl State {
ago = format!(" {}", ago); 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( let duration = Span::styled(
duration, duration,
Style::default().fg(if m.exit == 0 || m.duration == -1 { Style::default().fg(if m.exit == 0 || m.duration == -1 {
@ -115,8 +132,14 @@ impl State {
} }
} }
let spans = let spans = Spans::from(vec![
Spans::from(vec![duration, Span::raw(" "), ago, Span::raw(" "), command]); selected_index,
duration,
Span::raw(" "),
ago,
Span::raw(" "),
command,
]);
ListItem::new(spans) ListItem::new(spans)
}) })
@ -171,6 +194,16 @@ async fn key_handler(
.map_or(app.input.clone(), |h| h.command.clone()), .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) => { Key::Char(c) => {
app.input.push(c); app.input.push(c);
query_results(app, search_mode, db).await.unwrap(); query_results(app, search_mode, db).await.unwrap();