diff --git a/src/core/mod.rs b/src/core/mod.rs new file mode 100644 index 0000000..f6ac8fc --- /dev/null +++ b/src/core/mod.rs @@ -0,0 +1 @@ +pub mod traits; diff --git a/src/core/traits.rs b/src/core/traits.rs new file mode 100644 index 0000000..55b1610 --- /dev/null +++ b/src/core/traits.rs @@ -0,0 +1,23 @@ +#[derive(Clone, Debug)] +pub struct Entry { + pub source: &'static str, + pub name: String, + pub description: String, + pub identifier: u64, +} + +pub trait Source: Send + Sync { + fn name(&self) -> &'static str; + fn entries(&mut self) -> Vec; + 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 { + fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec)>; +} diff --git a/src/main.rs b/src/main.rs index 18ffede..968724e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,13 @@ use winit::event_loop::{ControlFlow, EventLoop}; use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent::{CloseRequested, ReceivedCharacter}}; -use crate::matcher::{FuseMatcher, PrefixMatcher, SkimMatcher, FlxMatcher}; +use crate::matcher::{FuseMatcher, PrefixMatcher}; mod roftl; mod ui; mod sources; mod matcher; +mod core; use roftl::Roftl; @@ -27,11 +28,8 @@ fn main() -> Result<(), Box> { .with_primary_source(sources::Window::new()) .with_primary_matcher(PrefixMatcher::new()); - debug!{"Source primary roftl sources"} - roftl.source_primary(); - - debug!{"Source additional roftl sources"} - roftl.source_additional(); + debug!{"Source roftl sources"} + roftl.source(); debug!{"Build window"} let event_loop = EventLoop::new(); diff --git a/src/matcher/flx.rs b/src/matcher/flx.rs index 9c75ace..8b363d0 100644 --- a/src/matcher/flx.rs +++ b/src/matcher/flx.rs @@ -1,4 +1,4 @@ -use crate::roftl::Matcher; +use super::core::traits::Matcher; use flx_rs::Score; use log::debug; diff --git a/src/matcher/fuse.rs b/src/matcher/fuse.rs index 6dda7d2..cd90e8a 100644 --- a/src/matcher/fuse.rs +++ b/src/matcher/fuse.rs @@ -2,7 +2,7 @@ use std::ops::Range; use fuse_rust::Fuse; -use crate::roftl::Matcher; +use super::core::traits::Matcher; fn flatten_ranges(ranges: Vec>) -> Vec { ranges.into_iter().flat_map(|range| {range.collect::>().into_iter()}) diff --git a/src/matcher/mod.rs b/src/matcher/mod.rs index 0969d3a..d9b4bd5 100644 --- a/src/matcher/mod.rs +++ b/src/matcher/mod.rs @@ -1,3 +1,5 @@ +use super::core; + mod prefix; pub use prefix::PrefixMatcher; diff --git a/src/matcher/prefix.rs b/src/matcher/prefix.rs index 32e8cab..b00c47e 100644 --- a/src/matcher/prefix.rs +++ b/src/matcher/prefix.rs @@ -1,4 +1,4 @@ -use crate::roftl::Matcher; +use super::core::traits::Matcher; pub struct PrefixMatcher; diff --git a/src/matcher/skim.rs b/src/matcher/skim.rs index 409a2cf..37399da 100644 --- a/src/matcher/skim.rs +++ b/src/matcher/skim.rs @@ -1,6 +1,6 @@ use fuzzy_matcher::{FuzzyMatcher, skim::SkimMatcherV2}; -use crate::roftl::Matcher; +use super::core::traits::Matcher; pub struct SkimMatcher { matcher: SkimMatcherV2 diff --git a/src/roftl.rs b/src/roftl.rs index 785ded8..fa91788 100644 --- a/src/roftl.rs +++ b/src/roftl.rs @@ -1,4 +1,3 @@ -use std::rc::Rc; use std::sync::RwLock; use std::sync::Arc; use log::debug; @@ -6,33 +5,14 @@ use rayon::prelude::*; use super::matcher::PrefixMatcher; +use super::core::traits::Matcher; +use super::core::traits::Source; +use super::core::traits::Entry; + + pub type SourceRef = Box; pub type MatcherRef = Box; -#[derive(Clone, Debug)] -pub struct Entry { - pub source: &'static str, - pub name: String, - pub description: String, - pub identifier: u64, -} - -pub trait Source: Send + Sync { - fn name(&self) -> &'static str; - fn entries(&mut self) -> Vec; - 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 { - fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec)>; -} - pub struct Roftl { sources: Arc>>, source_chan: Option>>, @@ -66,7 +46,7 @@ impl Roftl { self } - pub fn add_source(mut self, source: SourceRef) -> Self { + pub fn add_source(self, source: SourceRef) -> Self { if self.sources.read().unwrap().par_iter().any(|s| s.name().eq(source.name())) { panic! {"Source with name '{}' already exists", source.name()} } @@ -85,7 +65,12 @@ impl Roftl { self } - pub fn source_primary(&mut self) { + pub fn source(&mut self) { + self.source_primary(); + self.source_additional(); + } + + fn source_primary(&mut self) { self.entries = self.primary_source .par_iter_mut() .flat_map(|s| s.entries()) @@ -94,7 +79,7 @@ impl Roftl { debug!("Sourced {} primary entries", self.entries.len()); } - pub fn source_additional(&mut self) { + fn source_additional(&mut self) { let (tx, rx) = std::sync::mpsc::channel(); self.source_chan = Some(rx); @@ -153,7 +138,6 @@ impl Roftl { .par_iter() .enumerate() .filter_map(|(idx, entry)| { - if entry.source == "window" { return None } if input.is_empty() { return Some((0.0, idx, entry, vec![])) } let match_result = matcher.try_match(&entry.name, input); diff --git a/src/sources/apps.rs b/src/sources/apps.rs index d2beb52..1634fe2 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::{Entry, Source}; +use super::core::traits::{Entry, Source}; #[derive(Debug)] struct App { diff --git a/src/sources/mod.rs b/src/sources/mod.rs index e9aba03..d73c72b 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -1,3 +1,5 @@ +use super::core; + mod test; pub use test::TestSource; diff --git a/src/sources/shellhost.rs b/src/sources/shellhost.rs index 6281b17..18655a7 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}; +use super::core::traits::{Entry, Source}; pub struct ShellHost { scripts: Vec, diff --git a/src/sources/test.rs b/src/sources/test.rs index a3b7bf1..1998d1c 100644 --- a/src/sources/test.rs +++ b/src/sources/test.rs @@ -1,4 +1,4 @@ -use crate::roftl::{Entry, Source}; +use super::core::traits::{Entry, Source}; pub struct TestSource { entries: Vec, diff --git a/src/sources/windows.rs b/src/sources/windows.rs index 610b1bc..a5e4984 100644 --- a/src/sources/windows.rs +++ b/src/sources/windows.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; -use std::ptr; -use crate::roftl::{Entry, Source}; +use super::core::traits::{Entry, Source}; use log::debug; use xcb::ffi::XCB_CURRENT_TIME; use xcb_util::ewmh; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index d2f8c91..ea7b154 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,3 +1,5 @@ +use super::core; + mod ui; pub use ui::draw; pub use ui::redraw_quick; diff --git a/src/ui/painter.rs b/src/ui/painter.rs index 4cf983e..7711011 100644 --- a/src/ui/painter.rs +++ b/src/ui/painter.rs @@ -1,5 +1,4 @@ use log::debug; -use rayon::result; use super::theme::{Theme, ThemedContextExt}; diff --git a/src/ui/ui.rs b/src/ui/ui.rs index 0427188..80026dc 100644 --- a/src/ui/ui.rs +++ b/src/ui/ui.rs @@ -4,7 +4,7 @@ use std::ffi::c_void; use log::debug; use winit::{platform::unix::WindowExtUnix, window::Window}; -use crate::roftl::Entry; +use super::core::traits::Entry; use super::painter;