diff --git a/src/main.rs b/src/main.rs index 4598e1a..422fe74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,7 +112,7 @@ impl RoftlLoop { // quick switch if only one result if results.len() == 1 { - self.roftl.do_action(0, roftl::Action::Primary); + self.roftl.exec_default(0); *flow = ControlFlow::Exit; return; } @@ -185,7 +185,7 @@ impl RoftlLoop { // Enter c if c == char::from(0x0d) => { trace!{"Retrieved enter. Trigger primary action"} - self.roftl.do_action(self.selection, roftl::Action::Primary); + self.roftl.exec_action(self.selection, 0); ControlFlow::Exit } @@ -235,22 +235,22 @@ impl RoftlLoop { match character { '1' => { trace!{"Retrieved action selection 1. Trigger primary action"} - self.roftl.do_action(self.selection, roftl::Action::Primary); + self.roftl.exec_action(self.selection, 0); ControlFlow::Exit }, '2' => { trace!{"Retrieved action selection 2. Trigger secondary action"} - self.roftl.do_action(self.selection, roftl::Action::Secondary); + self.roftl.exec_action(self.selection, 1); ControlFlow::Exit }, '3' => { trace!{"Retrieved action selection 3. Trigger tertiary action"} - self.roftl.do_action(self.selection, roftl::Action::Tertiary); + self.roftl.exec_action(self.selection, 2); ControlFlow::Exit }, '4' => { trace!{"Retrieved action selection 4. Trigger quaternary action"} - self.roftl.do_action(self.selection, roftl::Action::Quaternary); + self.roftl.exec_action(self.selection, 3); ControlFlow::Exit }, diff --git a/src/roftl.rs b/src/roftl.rs index 69f6487..45c7f7f 100644 --- a/src/roftl.rs +++ b/src/roftl.rs @@ -14,20 +14,16 @@ pub struct Entry { pub identifier: u64, } -#[derive(Debug)] -pub enum Action { - Primary, - Secondary, - 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, _entry: &Entry) -> Vec { vec![] } + + fn exec_default(&self, entry: &Entry) + { self.exec_action(entry, 0) } + + fn exec_action(&self, entry: &Entry, action: u8); } pub trait Matcher: Send + Sync { @@ -82,7 +78,7 @@ impl Roftl { .flat_map(|s| s.entries()) .collect(); - debug!("Entries {:?}", self.entries); + debug!("Sourced {} entries from {} sources", self.entries.len(), self.sources.len()); } pub fn narrow(&mut self, input: &str) -> Vec<(&Entry, Vec)> { @@ -133,16 +129,21 @@ impl Roftl { .collect() } - pub fn do_action(&self, selection_id: usize, action: Action) { - let (entry, source) = self.find_selection(selection_id); - source.action(entry, action); - } - pub fn actions(&self, selection_id: usize) -> Vec { let (entry, source) = self.find_selection(selection_id); source.actions(entry) } + pub fn exec_default(&self, selection_id: usize) { + let (entry, source) = self.find_selection(selection_id); + source.exec_default(entry); + } + + pub fn exec_action(&self, selection_id: usize, action: u8) { + let (entry, source) = self.find_selection(selection_id); + source.exec_action(entry, action); + } + fn find_selection(&self, selection_id: usize) -> (&Entry, &Box) { let entry_id = self.narrow_map[selection_id]; diff --git a/src/sources/apps.rs b/src/sources/apps.rs index d613836..d2beb52 100644 --- a/src/sources/apps.rs +++ b/src/sources/apps.rs @@ -5,7 +5,7 @@ use log::{debug, error, trace}; use walkdir::WalkDir; use nix::unistd::{getpid, setpgid}; -use crate::roftl::{Action, Entry, Source}; +use crate::roftl::{Entry, Source}; #[derive(Debug)] struct App { @@ -176,7 +176,7 @@ impl Source for Apps { entries } - fn action(&self, entry: &Entry, action: Action) { + fn exec_action(&self, entry: &Entry, action: u8) { debug!{"Got desktop entry {:?} for action", entry} debug!{"Desktop entry has id {}", entry.identifier} trace!{"Apps has entries {:?}", self.entries} diff --git a/src/sources/shellhost.rs b/src/sources/shellhost.rs index 8adcbf3..6281b17 100644 --- a/src/sources/shellhost.rs +++ b/src/sources/shellhost.rs @@ -3,7 +3,7 @@ use log::{debug, error}; use nix::unistd::{getpid, setpgid}; use walkdir::WalkDir; -use crate::roftl::{Entry, Source, Action}; +use crate::roftl::{Entry, Source}; pub struct ShellHost { scripts: Vec, @@ -43,7 +43,7 @@ impl Source for ShellHost { entries } - fn action(&self, entry: &Entry, action: Action) { + fn exec_action(&self, entry: &Entry, action: u8) { let script: &PathBuf = self.scripts.get(entry.identifier as usize).unwrap(); debug!{"Got script {:?}", script}; diff --git a/src/sources/test.rs b/src/sources/test.rs index c8dba4c..a3b7bf1 100644 --- a/src/sources/test.rs +++ b/src/sources/test.rs @@ -1,4 +1,4 @@ -use crate::roftl::{Entry, Source, Action}; +use crate::roftl::{Entry, Source}; pub struct TestSource { entries: Vec, @@ -38,7 +38,7 @@ impl Source for TestSource { self.entries.clone() } - fn action(&self, entry: &Entry, action: Action) { + fn exec_action(&self, entry: &Entry, action: u8) { println!("Doing action {:?} for entry {}", action, entry.name) } diff --git a/src/sources/windows.rs b/src/sources/windows.rs index 5467594..9ca22dc 100644 --- a/src/sources/windows.rs +++ b/src/sources/windows.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::ptr; -use crate::roftl::{Entry, Source, Action}; +use crate::roftl::{Entry, Source}; use log::debug; use xcb::ffi::XCB_CURRENT_TIME; use xcb_util::ewmh; @@ -111,31 +111,31 @@ impl Source for Window { entries } - fn action(&self, entry: &Entry, action: Action) { + fn exec_action(&self, entry: &Entry, action: u8) { debug!("Doing action {:?} for entry {}", action, entry.name); match action { - Action::Primary => { + 0 => { let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); self.switch_window(&ewmh_conn, window, screen).unwrap() }, - Action::Secondary => { + 1 => { let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); self.toggle_maximize_window(&ewmh_conn, window, screen).unwrap(); self.switch_window(&ewmh_conn, window, screen).unwrap() }, - Action::Tertiary => { + 2 => { let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); self.toggle_hide_window(&ewmh_conn, window, screen).unwrap(); }, - Action::Quaternary => { + 3 => { let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap();