Add sort_dir_first option and strip reverse

This commit is contained in:
Yifan Song 2023-08-12 19:20:02 +08:00
parent eccc1a3f95
commit 672b8001da
5 changed files with 38 additions and 12 deletions

View file

@ -129,6 +129,7 @@ impl Executor {
by: SortBy::try_from(exec.args.get(0).cloned().unwrap_or_default())
.unwrap_or_default(),
reverse: exec.named.contains_key("reverse"),
dir_first: exec.named.contains_key("dir_first")
});
cx.tasks.precache_size(&cx.manager.current().files);
b

View file

@ -14,6 +14,11 @@
- `true`: Reverse order
- `false`: Normal order
- sort_dir_first: Display files in reverse order
- `true`: Directory at first
- `false`: Sort According to `sort_by`
- show_hidden: Show hidden files
- `true`: Show

View file

@ -2,6 +2,7 @@
sort_by = "modified"
sort_reverse = true
show_hidden = false
sort_dir_first = true
[preview]
tab_size = 2

View file

@ -8,6 +8,7 @@ pub struct Manager {
// Sorting
pub sort_by: SortBy,
pub sort_reverse: bool,
pub sort_dir_first: bool,
// Display
pub show_hidden: bool,

View file

@ -116,29 +116,40 @@ impl Files {
return false;
}
fn cmp<T: Ord>(a: T, b: T, reverse: bool) -> std::cmp::Ordering {
if reverse { b.cmp(&a) } else { a.cmp(&b) }
}
let reverse = self.sort.reverse;
match self.sort.by {
SortBy::Alphabetical => self.items.sort_by(|_, a, _, b| cmp(&a.path, &b.path, reverse)),
SortBy::Alphabetical => self.items.sort_by(|_, a, _, b| (&a.path).cmp(&b.path)),
SortBy::Created => self.items.sort_by(|_, a, _, b| {
if let (Ok(a), Ok(b)) = (a.meta.created(), b.meta.created()) {
return cmp(a, b, reverse);
return (&a).cmp(&b);
}
std::cmp::Ordering::Equal
}),
SortBy::Modified => self.items.sort_by(|_, a, _, b| {
if let (Ok(a), Ok(b)) = (a.meta.modified(), b.meta.modified()) {
return cmp(a, b, reverse);
return (&a).cmp(&b);
}
std::cmp::Ordering::Equal
}),
SortBy::Size => {
self.items.sort_by(|_, a, _, b| cmp(a.length.unwrap_or(0), b.length.unwrap_or(0), reverse))
self.items.sort_by(|_, a, _, b| (a.length.unwrap_or(0)).cmp(&b.length.unwrap_or(0)))
}
}
if self.sort.dir_first {
self.items.sort_by(|_, a, _, b| {
if a.meta.is_dir() && !b.meta.is_dir() {
return std::cmp::Ordering::Less;
} else if !a.meta.is_dir() && b.meta.is_dir() {
return std::cmp::Ordering::Greater;
}
std::cmp::Ordering::Equal
});
}
if self.sort.reverse {
self.items.reverse();
}
true
}
}
@ -155,12 +166,19 @@ impl DerefMut for Files {
#[derive(PartialEq)]
pub struct FilesSort {
pub by: SortBy,
pub reverse: bool,
pub by: SortBy,
pub reverse: bool,
pub dir_first: bool,
}
impl Default for FilesSort {
fn default() -> Self { Self { by: MANAGER.sort_by, reverse: MANAGER.sort_reverse } }
fn default() -> Self {
Self {
by: MANAGER.sort_by,
reverse: MANAGER.sort_reverse,
dir_first: MANAGER.sort_dir_first,
}
}
}
#[derive(Debug)]