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

View file

@ -18,18 +18,18 @@ pub struct Entry {
pub enum Action { pub enum Action {
Primary, Primary,
Secondary, Secondary,
Ternary Tertiary,
Quaternary
} }
pub trait Source: Send + Sync { pub trait Source: Send + Sync {
fn name(&self) -> &'static str; fn name(&self) -> &'static str;
fn entries(&mut self) -> Vec<Entry>; fn entries(&mut self) -> Vec<Entry>;
fn action(&self, entry: &Entry, action: Action); fn action(&self, entry: &Entry, action: Action);
fn actions(&self) -> Vec<String> fn actions(&self, entry: &Entry) -> Vec<String>
{ vec![] } { vec![] }
} }
pub trait Matcher: Send + Sync { pub trait Matcher: Send + Sync {
fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec<usize>)>; 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> { pub fn actions(&self, selection_id: usize) -> Vec<String> {
let (_entry, source) = self.find_selection(selection_id); let (entry, source) = self.find_selection(selection_id);
source.actions() source.actions(entry)
} }
fn find_selection(&self, selection_id: usize) -> (&Entry, &Box<dyn Source>) 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> { pub fn new(s: &str) -> Box<TestSource> {
let mut ts = Box::new(TestSource { entries: vec![] }); let mut ts = Box::new(TestSource { entries: vec![] });
(0..1).for_each(|i| { (0..5).for_each(|i| {
ts.add_entry( ts.add_entry(
format! {"Test {} {}", i, s}, format! {"Test {} {}", i, s},
format! {"Test {} description", i}, format! {"Test {} description", i},
@ -42,7 +42,7 @@ impl Source for TestSource {
println!("Doing action {:?} for entry {}", action, entry.name) println!("Doing action {:?} for entry {}", action, entry.name)
} }
fn actions(&self) -> Vec<String> { fn actions(&self, _entry: &Entry) -> Vec<String> {
["Primary", "Secondary", "Ternary"] ["Primary", "Secondary", "Ternary"]
.iter().map(|s| (*s).into()).collect() .iter().map(|s| (*s).into()).collect()
} }