This commit is contained in:
Karthikey Hegde 2026-05-23 11:45:43 +08:00 committed by GitHub
commit e033a98159
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -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()
}

View File

@ -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 }
}