From 672b8001dae1483f478bee80250e9cf6048b7d50 Mon Sep 17 00:00:00 2001 From: Yifan Song Date: Sat, 12 Aug 2023 19:20:02 +0800 Subject: [PATCH] Add sort_dir_first option and strip reverse --- app/src/executor.rs | 1 + config/docs/yazi.md | 5 +++++ config/preset/yazi.toml | 1 + config/src/manager/manager.rs | 1 + core/src/files/files.rs | 42 +++++++++++++++++++++++++---------- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/app/src/executor.rs b/app/src/executor.rs index 78414152..d1b015a7 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -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 diff --git a/config/docs/yazi.md b/config/docs/yazi.md index d34637d8..8c8d0ef2 100644 --- a/config/docs/yazi.md +++ b/config/docs/yazi.md @@ -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 diff --git a/config/preset/yazi.toml b/config/preset/yazi.toml index d9584bce..5871e4e1 100644 --- a/config/preset/yazi.toml +++ b/config/preset/yazi.toml @@ -2,6 +2,7 @@ sort_by = "modified" sort_reverse = true show_hidden = false +sort_dir_first = true [preview] tab_size = 2 diff --git a/config/src/manager/manager.rs b/config/src/manager/manager.rs index 319ef7bd..a438a440 100644 --- a/config/src/manager/manager.rs +++ b/config/src/manager/manager.rs @@ -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, diff --git a/core/src/files/files.rs b/core/src/files/files.rs index d2c39eb6..5ce3dca0 100644 --- a/core/src/files/files.rs +++ b/core/src/files/files.rs @@ -116,29 +116,40 @@ impl Files { return false; } - fn cmp(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)]