From 450e13d65545a43723f8f93c1b06f36760b048aa Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Wed, 3 Nov 2021 21:54:00 +0100 Subject: [PATCH] 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. --- src/main.rs | 21 +++++++++++++-------- src/roftl.rs | 10 +++++----- src/sources/test.rs | 4 ++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2f6ebc7..4fdf794 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) }, } } diff --git a/src/roftl.rs b/src/roftl.rs index 2309e41..221642c 100644 --- a/src/roftl.rs +++ b/src/roftl.rs @@ -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; fn action(&self, entry: &Entry, action: Action); - fn actions(&self) -> Vec + fn actions(&self, entry: &Entry) -> Vec { vec![] } } - pub trait Matcher: Send + Sync { fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec)>; } @@ -106,8 +106,8 @@ impl Roftl { } pub fn actions(&self, selection_id: usize) -> Vec { - 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) diff --git a/src/sources/test.rs b/src/sources/test.rs index 20722f6..e6df8cd 100644 --- a/src/sources/test.rs +++ b/src/sources/test.rs @@ -8,7 +8,7 @@ impl TestSource { pub fn new(s: &str) -> Box { 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 { + fn actions(&self, _entry: &Entry) -> Vec { ["Primary", "Secondary", "Ternary"] .iter().map(|s| (*s).into()).collect() }