diff --git a/src/db/mod.rs b/src/db/mod.rs index 1856fda..aa3f214 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -180,6 +180,25 @@ impl Database { self.with_dirty_mut(|dirty| *dirty = true); } + pub fn sort_by_keywords(&mut self, keywords: &[String]) { + if keywords.is_empty() { + return; + } + + self.with_dirs_mut(|dirs| { + dirs.sort_by_key(|dir| Self::has_exact_match(&dir.path, keywords)); + }); + self.with_dirty_mut(|dirty| *dirty = true); + } + + fn has_exact_match(path: &str, keywords: &[String]) -> bool { + keywords.last().is_some_and(|keyword| { + let path_lower = util::to_lowercase(path); + let last_component = path_lower.rsplit(std::path::is_separator).next().unwrap_or(""); + last_component == keyword + }) + } + pub fn dirty(&self) -> bool { *self.borrow_dirty() } diff --git a/src/db/stream.rs b/src/db/stream.rs index 24c84e0..8f54dba 100644 --- a/src/db/stream.rs +++ b/src/db/stream.rs @@ -16,7 +16,11 @@ pub struct Stream<'a> { impl<'a> Stream<'a> { pub fn new(db: &'a mut Database, options: StreamOptions) -> Self { - db.sort_by_score(options.now); + let now = options.now; + let keywords = &options.keywords; + + db.sort_by_score(now); + db.sort_by_keywords(keywords); let idxs = (0..db.dirs().len()).rev(); Stream { db, idxs, options } }