Move standard traits and structs to core
This commit is contained in:
parent
838b329553
commit
198e28460d
17 changed files with 56 additions and 46 deletions
1
src/core/mod.rs
Normal file
1
src/core/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod traits;
|
23
src/core/traits.rs
Normal file
23
src/core/traits.rs
Normal file
|
@ -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<Entry>;
|
||||||
|
fn actions(&self, _entry: &Entry) -> Vec<String>
|
||||||
|
{ 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<usize>)>;
|
||||||
|
}
|
10
src/main.rs
10
src/main.rs
|
@ -6,12 +6,13 @@ use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode,
|
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode,
|
||||||
WindowEvent::{CloseRequested, ReceivedCharacter}};
|
WindowEvent::{CloseRequested, ReceivedCharacter}};
|
||||||
|
|
||||||
use crate::matcher::{FuseMatcher, PrefixMatcher, SkimMatcher, FlxMatcher};
|
use crate::matcher::{FuseMatcher, PrefixMatcher};
|
||||||
|
|
||||||
mod roftl;
|
mod roftl;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod sources;
|
mod sources;
|
||||||
mod matcher;
|
mod matcher;
|
||||||
|
mod core;
|
||||||
|
|
||||||
use roftl::Roftl;
|
use roftl::Roftl;
|
||||||
|
|
||||||
|
@ -27,11 +28,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.with_primary_source(sources::Window::new())
|
.with_primary_source(sources::Window::new())
|
||||||
.with_primary_matcher(PrefixMatcher::new());
|
.with_primary_matcher(PrefixMatcher::new());
|
||||||
|
|
||||||
debug!{"Source primary roftl sources"}
|
debug!{"Source roftl sources"}
|
||||||
roftl.source_primary();
|
roftl.source();
|
||||||
|
|
||||||
debug!{"Source additional roftl sources"}
|
|
||||||
roftl.source_additional();
|
|
||||||
|
|
||||||
debug!{"Build window"}
|
debug!{"Build window"}
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::roftl::Matcher;
|
use super::core::traits::Matcher;
|
||||||
|
|
||||||
use flx_rs::Score;
|
use flx_rs::Score;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::ops::Range;
|
||||||
|
|
||||||
use fuse_rust::Fuse;
|
use fuse_rust::Fuse;
|
||||||
|
|
||||||
use crate::roftl::Matcher;
|
use super::core::traits::Matcher;
|
||||||
|
|
||||||
fn flatten_ranges(ranges: Vec<Range<usize>>) -> Vec<usize> {
|
fn flatten_ranges(ranges: Vec<Range<usize>>) -> Vec<usize> {
|
||||||
ranges.into_iter().flat_map(|range| {range.collect::<Vec<usize>>().into_iter()})
|
ranges.into_iter().flat_map(|range| {range.collect::<Vec<usize>>().into_iter()})
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use super::core;
|
||||||
|
|
||||||
mod prefix;
|
mod prefix;
|
||||||
pub use prefix::PrefixMatcher;
|
pub use prefix::PrefixMatcher;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::roftl::Matcher;
|
use super::core::traits::Matcher;
|
||||||
|
|
||||||
pub struct PrefixMatcher;
|
pub struct PrefixMatcher;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use fuzzy_matcher::{FuzzyMatcher, skim::SkimMatcherV2};
|
use fuzzy_matcher::{FuzzyMatcher, skim::SkimMatcherV2};
|
||||||
|
|
||||||
use crate::roftl::Matcher;
|
use super::core::traits::Matcher;
|
||||||
|
|
||||||
pub struct SkimMatcher {
|
pub struct SkimMatcher {
|
||||||
matcher: SkimMatcherV2
|
matcher: SkimMatcherV2
|
||||||
|
|
42
src/roftl.rs
42
src/roftl.rs
|
@ -1,4 +1,3 @@
|
||||||
use std::rc::Rc;
|
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
@ -6,33 +5,14 @@ use rayon::prelude::*;
|
||||||
|
|
||||||
use super::matcher::PrefixMatcher;
|
use super::matcher::PrefixMatcher;
|
||||||
|
|
||||||
|
use super::core::traits::Matcher;
|
||||||
|
use super::core::traits::Source;
|
||||||
|
use super::core::traits::Entry;
|
||||||
|
|
||||||
|
|
||||||
pub type SourceRef = Box<dyn Source>;
|
pub type SourceRef = Box<dyn Source>;
|
||||||
pub type MatcherRef = Box<dyn Matcher>;
|
pub type MatcherRef = Box<dyn Matcher>;
|
||||||
|
|
||||||
#[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<Entry>;
|
|
||||||
fn actions(&self, _entry: &Entry) -> Vec<String>
|
|
||||||
{ 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<usize>)>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Roftl {
|
pub struct Roftl {
|
||||||
sources: Arc<RwLock<Vec<SourceRef>>>,
|
sources: Arc<RwLock<Vec<SourceRef>>>,
|
||||||
source_chan: Option<std::sync::mpsc::Receiver<Vec<Entry>>>,
|
source_chan: Option<std::sync::mpsc::Receiver<Vec<Entry>>>,
|
||||||
|
@ -66,7 +46,7 @@ impl Roftl {
|
||||||
self
|
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())) {
|
if self.sources.read().unwrap().par_iter().any(|s| s.name().eq(source.name())) {
|
||||||
panic! {"Source with name '{}' already exists", source.name()}
|
panic! {"Source with name '{}' already exists", source.name()}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +65,12 @@ impl Roftl {
|
||||||
self
|
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
|
self.entries = self.primary_source
|
||||||
.par_iter_mut()
|
.par_iter_mut()
|
||||||
.flat_map(|s| s.entries())
|
.flat_map(|s| s.entries())
|
||||||
|
@ -94,7 +79,7 @@ impl Roftl {
|
||||||
debug!("Sourced {} primary entries", self.entries.len());
|
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();
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
self.source_chan = Some(rx);
|
self.source_chan = Some(rx);
|
||||||
|
|
||||||
|
@ -153,7 +138,6 @@ impl Roftl {
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(idx, entry)| {
|
.filter_map(|(idx, entry)| {
|
||||||
if entry.source == "window" { return None }
|
|
||||||
if input.is_empty() { return Some((0.0, idx, entry, vec![])) }
|
if input.is_empty() { return Some((0.0, idx, entry, vec![])) }
|
||||||
|
|
||||||
let match_result = matcher.try_match(&entry.name, input);
|
let match_result = matcher.try_match(&entry.name, input);
|
||||||
|
|
|
@ -5,7 +5,7 @@ use log::{debug, error, trace};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
use nix::unistd::{getpid, setpgid};
|
use nix::unistd::{getpid, setpgid};
|
||||||
|
|
||||||
use crate::roftl::{Entry, Source};
|
use super::core::traits::{Entry, Source};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct App {
|
struct App {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use super::core;
|
||||||
|
|
||||||
mod test;
|
mod test;
|
||||||
pub use test::TestSource;
|
pub use test::TestSource;
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use log::{debug, error};
|
||||||
use nix::unistd::{getpid, setpgid};
|
use nix::unistd::{getpid, setpgid};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
use crate::roftl::{Entry, Source};
|
use super::core::traits::{Entry, Source};
|
||||||
|
|
||||||
pub struct ShellHost {
|
pub struct ShellHost {
|
||||||
scripts: Vec<PathBuf>,
|
scripts: Vec<PathBuf>,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::roftl::{Entry, Source};
|
use super::core::traits::{Entry, Source};
|
||||||
|
|
||||||
pub struct TestSource {
|
pub struct TestSource {
|
||||||
entries: Vec<Entry>,
|
entries: Vec<Entry>,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
use crate::roftl::{Entry, Source};
|
use super::core::traits::{Entry, Source};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use xcb::ffi::XCB_CURRENT_TIME;
|
use xcb::ffi::XCB_CURRENT_TIME;
|
||||||
use xcb_util::ewmh;
|
use xcb_util::ewmh;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use super::core;
|
||||||
|
|
||||||
mod ui;
|
mod ui;
|
||||||
pub use ui::draw;
|
pub use ui::draw;
|
||||||
pub use ui::redraw_quick;
|
pub use ui::redraw_quick;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use rayon::result;
|
|
||||||
|
|
||||||
use super::theme::{Theme, ThemedContextExt};
|
use super::theme::{Theme, ThemedContextExt};
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::ffi::c_void;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use winit::{platform::unix::WindowExtUnix, window::Window};
|
use winit::{platform::unix::WindowExtUnix, window::Window};
|
||||||
|
|
||||||
use crate::roftl::Entry;
|
use super::core::traits::Entry;
|
||||||
|
|
||||||
use super::painter;
|
use super::painter;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue