Better sorting in opener

This commit is contained in:
Joshua Barretto 2025-10-29 12:14:30 +00:00
parent 78e2dbfaff
commit c4b4bc365f
3 changed files with 17 additions and 9 deletions

View file

@ -20,4 +20,9 @@
## Todo
- [ ] Replace
- [ ] Replace
## Issues to fix
- Can't use left/right to edit text in finder
- Pressing return on `foo {|bar` should wrap `bar` in a block

View file

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

View file

@ -371,15 +371,18 @@ impl Opener {
.ok()
.and_then(|m| Some(m.modified().ok()?.elapsed().ok()?.as_secs()))
.unwrap_or(!0);
if matches!(e.kind, FileKind::New) {
if filter == "" {
// When no filter is specified, simply order alphabetically
Some((0, 0, 0, name))
} else if matches!(e.kind, FileKind::New) {
// Special-case: the 'new file' entry always matches last
Some((1000, 0, 0))
Some((1000, 0, 0, String::new()))
} else if name == filter {
Some((0, modify_time, name.chars().count()))
Some((0, modify_time, name.chars().count(), String::new()))
} else if name.starts_with(&filter) {
Some((1, modify_time, name.chars().count()))
Some((1, modify_time, name.chars().count(), String::new()))
} else if name.contains(&filter) {
Some((2, modify_time, name.chars().count()))
Some((2, modify_time, name.chars().count(), String::new()))
} else {
None
}