diff --git a/app/src/executor.rs b/app/src/executor.rs index 6c7e41ae..69612813 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -149,10 +149,11 @@ impl Executor { // Sorting "sort" => { let b = cx.manager.active_mut().set_sorter(FilesSorter { - by: SortBy::try_from(exec.args.get(0).cloned().unwrap_or_default()) + 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"), + reverse: exec.named.contains_key("reverse"), + dir_first: exec.named.contains_key("dir_first"), + ignore_case: exec.named.contains_key("ignore_case"), }); cx.tasks.precache_size(&cx.manager.current().files); b diff --git a/config/docs/yazi.md b/config/docs/yazi.md index 7e6f4255..02eeabb6 100644 --- a/config/docs/yazi.md +++ b/config/docs/yazi.md @@ -24,6 +24,11 @@ - `true`: Directories first - `false`: Respects `sort_by` and `sort_reverse` only +- sort_ignore_case: Ignore case while sorting + + - `true`: Ignore case + - `false`: Uppercase has precedence over lowercase + - show_hidden: Show hidden files - `true`: Show diff --git a/config/preset/yazi.toml b/config/preset/yazi.toml index ed101007..812d7765 100644 --- a/config/preset/yazi.toml +++ b/config/preset/yazi.toml @@ -1,10 +1,11 @@ [manager] -layout = [ 1, 4, 3 ] -sort_by = "modified" -sort_reverse = true -sort_dir_first = true -show_hidden = false -show_symlink = true +layout = [ 1, 4, 3 ] +sort_by = "modified" +sort_reverse = true +sort_dir_first = true +sort_ignore_case = false +show_hidden = false +show_symlink = true [preview] tab_size = 2 diff --git a/config/src/manager/manager.rs b/config/src/manager/manager.rs index ddc1627a..864fe2d3 100644 --- a/config/src/manager/manager.rs +++ b/config/src/manager/manager.rs @@ -8,9 +8,10 @@ pub struct Manager { pub layout: ManagerLayout, // Sorting - pub sort_by: SortBy, - pub sort_reverse: bool, - pub sort_dir_first: bool, + pub sort_by: SortBy, + pub sort_reverse: bool, + pub sort_dir_first: bool, + pub sort_ignore_case: bool, // Display pub show_hidden: bool, diff --git a/core/src/files/sorter.rs b/core/src/files/sorter.rs index 36ce4794..6e3e55ea 100644 --- a/core/src/files/sorter.rs +++ b/core/src/files/sorter.rs @@ -1,4 +1,4 @@ -use std::{cmp::Ordering, collections::BTreeMap, mem}; +use std::{cmp::Ordering, collections::BTreeMap, mem, ops::Deref}; use config::{manager::SortBy, MANAGER}; use shared::Url; @@ -7,17 +7,19 @@ use super::File; #[derive(Clone, Copy, PartialEq)] pub struct FilesSorter { - pub by: SortBy, - pub reverse: bool, - pub dir_first: bool, + pub by: SortBy, + pub reverse: bool, + pub dir_first: bool, + pub ignore_case: bool, } impl Default for FilesSorter { fn default() -> Self { Self { - by: MANAGER.sort_by, - reverse: MANAGER.sort_reverse, - dir_first: MANAGER.sort_dir_first, + by: MANAGER.sort_by, + reverse: MANAGER.sort_reverse, + dir_first: MANAGER.sort_dir_first, + ignore_case: MANAGER.sort_ignore_case, } } } @@ -29,9 +31,17 @@ impl FilesSorter { } match self.by { - SortBy::Alphabetical => { - items.sort_unstable_by(|a, b| self.cmp(&*a.url, &*b.url, self.promote(a, b))) - } + SortBy::Alphabetical => items.sort_unstable_by(|a, b| { + if self.ignore_case { + self.cmp( + a.url.deref().as_os_str().to_ascii_lowercase(), + b.url.deref().as_os_str().to_ascii_lowercase(), + self.promote(a, b), + ) + } else { + self.cmp(&*a.url, &*b.url, self.promote(a, b)) + } + }), SortBy::Created => items.sort_unstable_by(|a, b| { if let (Ok(aa), Ok(bb)) = (a.meta.created(), b.meta.created()) { return self.cmp(aa, bb, self.promote(a, b)); @@ -65,12 +75,16 @@ impl FilesSorter { indices.sort_unstable_by(|&a, &b| { let promote = self.promote(entities[a].1, entities[b].1); if promote != Ordering::Equal { - promote - } else if self.reverse { - natord::compare(&entities[b].0, &entities[a].0) + return promote; + } + + let ordering = if self.ignore_case { + natord::compare_ignore_case(&entities[a].0, &entities[b].0) } else { natord::compare(&entities[a].0, &entities[b].0) - } + }; + + if self.reverse { ordering.reverse() } else { ordering } }); let dummy = File {