mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Add sort_dir_first option and strip reverse
This commit is contained in:
parent
eccc1a3f95
commit
672b8001da
5 changed files with 38 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
sort_by = "modified"
|
||||
sort_reverse = true
|
||||
show_hidden = false
|
||||
sort_dir_first = true
|
||||
|
||||
[preview]
|
||||
tab_size = 2
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -157,10 +168,17 @@ impl DerefMut for Files {
|
|||
pub struct FilesSort {
|
||||
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)]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue