Better prioritisation

This commit is contained in:
Joshua Barretto 2025-06-13 12:14:03 +01:00
parent 846cc31174
commit 5901a6cd1f
2 changed files with 10 additions and 10 deletions

View file

@ -120,7 +120,7 @@ impl<T> Options<T> {
} }
} }
pub fn set_options<F: FnMut(&T) -> Option<u32>>( pub fn set_options<F: FnMut(&T) -> Option<S>, S: Ord + Copy>(
&mut self, &mut self,
options: impl IntoIterator<Item = T>, options: impl IntoIterator<Item = T>,
mut f: F, mut f: F,
@ -129,14 +129,14 @@ impl<T> Options<T> {
self.apply_scoring(f); self.apply_scoring(f);
} }
pub fn apply_scoring<F: FnMut(&T) -> Option<u32>>(&mut self, mut f: F) { pub fn apply_scoring<F: FnMut(&T) -> Option<S>, S: Ord + Copy>(&mut self, mut f: F) {
let mut ranking = self let mut ranking = self
.options .options
.iter() .iter()
.enumerate() .enumerate()
.filter_map(|(i, o)| Some((i, f(o)?))) .filter_map(|(i, o)| Some((i, f(o)?)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ranking.sort_by_key(|(_, score)| std::cmp::Reverse(*score)); ranking.sort_by_key(|(_, score)| *score);
self.ranking = ranking.into_iter().map(|(i, _)| i).collect(); self.ranking = ranking.into_iter().map(|(i, _)| i).collect();
self.selected = 0; self.selected = 0;
} }

View file

@ -232,9 +232,9 @@ impl Element<()> for Switcher {
}; };
let name = buffer.name()?; let name = buffer.name()?;
if name.starts_with(&filter) { if name.starts_with(&filter) {
Some(2)
} else if name.contains(&filter) {
Some(1) Some(1)
} else if name.contains(&filter) {
Some(2)
} else { } else {
None None
} }
@ -341,19 +341,19 @@ impl Opener {
let name = e.path.file_name()?.to_str()?.to_lowercase(); let name = e.path.file_name()?.to_str()?.to_lowercase();
if matches!(e.kind, FileKind::New) { if matches!(e.kind, FileKind::New) {
// Special-case: the 'new file' entry always matches last // Special-case: the 'new file' entry always matches last
Some(0) Some((3, name.chars().count()))
} else if name == filter { } else if name == filter {
Some(3) Some((0, name.chars().count()))
} else if name.starts_with(&filter) { } else if name.starts_with(&filter) {
Some(2) Some((1, name.chars().count()))
} else if name.contains(&filter) { } else if name.contains(&filter) {
Some(1) Some((2, name.chars().count()))
} else { } else {
None None
} }
}) })
} }
Err(err) => self.options.set_options(Vec::new(), |_| None), Err(err) => self.options.set_options::<_, ()>(Vec::new(), |_| None),
} }
} }
} }