Per-entry actions and use simple actions for now

Preliminary code for action blocks and flags (similar to magit dispatch) was
removed for now. Reasoning is that it is too costly to remember specific
keybindings for each source/entry. We only allow 4 (for now)
actions which get pre-defined keybindings.

What those actions trigger exactly and whether less than 4 are used is left to
the source.
This commit is contained in:
Armin Friedl 2021-11-03 21:54:00 +01:00
parent 721d7dc326
commit 450e13d655
3 changed files with 20 additions and 15 deletions

View file

@ -189,8 +189,8 @@ impl RoftlLoop {
ControlFlow::Exit
}
// Ctrl+n or tab
c if c == char::from(0x0e) || c == char::from(0x09) => {
// Ctrl+n
c if c == char::from(0x0e) => {
debug!{"Received next"}
self.selection += 1;
self.window.request_redraw();
@ -205,8 +205,8 @@ impl RoftlLoop {
ControlFlow::Wait
}
// Ctrl+a
c if c == char::from(0x01) => {
// tab
c if c == char::from(0x09) => {
debug!{"Received actions"}
match self.mode {
Mode::Selection => { self.mode = Mode::Actions; self.input_changed = true },
@ -244,13 +244,18 @@ impl RoftlLoop {
ControlFlow::Exit
},
'2' => {
trace!{"Retrieved action selection 2. Trigger ternary action"}
self.roftl.action(self.selection, roftl::Action::Ternary);
trace!{"Retrieved action selection 2. Trigger tertiary action"}
self.roftl.action(self.selection, roftl::Action::Tertiary);
ControlFlow::Exit
},
'3' => {
trace!{"Retrieved action selection 2. Trigger quaternary action"}
self.roftl.action(self.selection, roftl::Action::Quaternary);
ControlFlow::Exit
},
_ => {
trace!{"Retrieved unknown action selection"}
ControlFlow::Wait
trace!{"Retrieved unknown action selection. Processing character {}", character}
self.process_character(character)
},
}
}

View file

@ -18,18 +18,18 @@ pub struct Entry {
pub enum Action {
Primary,
Secondary,
Ternary
Tertiary,
Quaternary
}
pub trait Source: Send + Sync {
fn name(&self) -> &'static str;
fn entries(&mut self) -> Vec<Entry>;
fn action(&self, entry: &Entry, action: Action);
fn actions(&self) -> Vec<String>
fn actions(&self, entry: &Entry) -> Vec<String>
{ vec![] }
}
pub trait Matcher: Send + Sync {
fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec<usize>)>;
}
@ -106,8 +106,8 @@ impl Roftl {
}
pub fn actions(&self, selection_id: usize) -> Vec<String> {
let (_entry, source) = self.find_selection(selection_id);
source.actions()
let (entry, source) = self.find_selection(selection_id);
source.actions(entry)
}
fn find_selection(&self, selection_id: usize) -> (&Entry, &Box<dyn Source>)

View file

@ -8,7 +8,7 @@ impl TestSource {
pub fn new(s: &str) -> Box<TestSource> {
let mut ts = Box::new(TestSource { entries: vec![] });
(0..1).for_each(|i| {
(0..5).for_each(|i| {
ts.add_entry(
format! {"Test {} {}", i, s},
format! {"Test {} description", i},
@ -42,7 +42,7 @@ impl Source for TestSource {
println!("Doing action {:?} for entry {}", action, entry.name)
}
fn actions(&self) -> Vec<String> {
fn actions(&self, _entry: &Entry) -> Vec<String> {
["Primary", "Secondary", "Ternary"]
.iter().map(|s| (*s).into()).collect()
}