feat: added sorting by file extension feature to

yazi
This commit is contained in:
JYShaw 2023-11-28 14:34:10 +08:00
parent a1984725e4
commit 33b547d894
3 changed files with 11 additions and 0 deletions

View file

@ -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" },

View file

@ -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()
}

View file

@ -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
}