From 33b547d8944434d5be8fc79bae86162b23159bda Mon Sep 17 00:00:00 2001 From: JYShaw <5haw@protonmail.com> Date: Tue, 28 Nov 2023 14:34:10 +0800 Subject: [PATCH] feat: added sorting by file extension feature to yazi --- yazi-config/preset/keymap.toml | 2 ++ yazi-config/src/manager/sorting.rs | 3 +++ yazi-core/src/files/sorter.rs | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/yazi-config/preset/keymap.toml b/yazi-config/preset/keymap.toml index 31bdb5b6..c5709915 100644 --- a/yazi-config/preset/keymap.toml +++ b/yazi-config/preset/keymap.toml @@ -106,6 +106,8 @@ keymap = [ { on = [ ",", "N" ], exec = "sort natural --reverse --dir_first", desc = "Sort naturally (reverse)" }, { on = [ ",", "s" ], exec = "sort size --dir_first", desc = "Sort by size" }, { on = [ ",", "S" ], exec = "sort size --reverse --dir_first", desc = "Sort by size (reverse)" }, + { on = [ ",", "e" ], exec = "sort extension --dir_first", desc = "Sort by extension" }, + { on = [ ",", "E" ], exec = "sort extension --reverse --dir_first", desc = "Sort by extension (reverse)" }, # Tabs { on = [ "t" ], exec = "tab_create --current", desc = "Create a new tab using the current path" }, diff --git a/yazi-config/src/manager/sorting.rs b/yazi-config/src/manager/sorting.rs index 41dc29f8..17dc0d2a 100644 --- a/yazi-config/src/manager/sorting.rs +++ b/yazi-config/src/manager/sorting.rs @@ -13,6 +13,7 @@ pub enum SortBy { Modified, Natural, Size, + Extension, } impl FromStr for SortBy { @@ -26,6 +27,7 @@ impl FromStr for SortBy { "modified" => Self::Modified, "natural" => Self::Natural, "size" => Self::Size, + "extension" => Self::Extension, _ => bail!("invalid sort_by value: {s}"), }) } @@ -46,6 +48,7 @@ impl ToString for SortBy { Self::Modified => "modified", Self::Natural => "natural", Self::Size => "size", + Self::Extension => "extension", } .to_string() } diff --git a/yazi-core/src/files/sorter.rs b/yazi-core/src/files/sorter.rs index 9eca8551..a1f4b3a4 100644 --- a/yazi-core/src/files/sorter.rs +++ b/yazi-core/src/files/sorter.rs @@ -47,6 +47,12 @@ impl FilesSorter { let ord = self.cmp(aa.unwrap_or(a.len), bb.unwrap_or(b.len), self.promote(a, b)); if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } }), + SortBy::Extension => items.sort_unstable_by(|a,b| { + let a_ext = a.url.extension().unwrap_or_default().to_ascii_lowercase(); + let b_ext = b.url.extension().unwrap_or_default().to_ascii_lowercase(); + let ord = self.cmp(a_ext, b_ext, self.promote(a, b)); + if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } + }), } true }