feat: support sorting by file extension (#405)

This commit is contained in:
JYShaw 2023-11-28 15:16:42 +08:00 committed by GitHub
parent a1984725e4
commit b41bea9e8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 15 deletions

View file

@ -96,12 +96,14 @@ keymap = [
{ on = [ "N" ], exec = "find_arrow --previous" },
# Sorting
{ on = [ ",", "a" ], exec = "sort alphabetical --dir_first", desc = "Sort alphabetically" },
{ on = [ ",", "A" ], exec = "sort alphabetical --reverse --dir_first", desc = "Sort alphabetically (reverse)" },
{ on = [ ",", "c" ], exec = "sort created --dir_first", desc = "Sort by creation time" },
{ on = [ ",", "C" ], exec = "sort created --reverse --dir_first", desc = "Sort by creation time (reverse)" },
{ on = [ ",", "m" ], exec = "sort modified --dir_first", desc = "Sort by modified time" },
{ on = [ ",", "M" ], exec = "sort modified --reverse --dir_first", desc = "Sort by modified time (reverse)" },
{ on = [ ",", "c" ], exec = "sort created --dir_first", desc = "Sort by created time" },
{ on = [ ",", "C" ], exec = "sort created --reverse --dir_first", desc = "Sort by created time (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)" },
{ on = [ ",", "a" ], exec = "sort alphabetical --dir_first", desc = "Sort alphabetically" },
{ on = [ ",", "A" ], exec = "sort alphabetical --reverse --dir_first", desc = "Sort alphabetically (reverse)" },
{ on = [ ",", "n" ], exec = "sort natural --dir_first", desc = "Sort naturally" },
{ on = [ ",", "N" ], exec = "sort natural --reverse --dir_first", desc = "Sort naturally (reverse)" },
{ on = [ ",", "s" ], exec = "sort size --dir_first", desc = "Sort by size" },

View file

@ -8,9 +8,10 @@ use serde::{Deserialize, Serialize};
pub enum SortBy {
#[default]
None,
Alphabetical,
Created,
Modified,
Created,
Extension,
Alphabetical,
Natural,
Size,
}
@ -21,9 +22,10 @@ impl FromStr for SortBy {
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"none" => Self::None,
"alphabetical" => Self::Alphabetical,
"created" => Self::Created,
"modified" => Self::Modified,
"created" => Self::Created,
"extension" => Self::Extension,
"alphabetical" => Self::Alphabetical,
"natural" => Self::Natural,
"size" => Self::Size,
_ => bail!("invalid sort_by value: {s}"),
@ -41,9 +43,10 @@ impl ToString for SortBy {
fn to_string(&self) -> String {
match self {
Self::None => "none",
Self::Alphabetical => "alphabetical",
Self::Created => "created",
Self::Modified => "modified",
Self::Created => "created",
Self::Extension => "extension",
Self::Alphabetical => "alphabetical",
Self::Natural => "natural",
Self::Size => "size",
}

View file

@ -31,15 +31,27 @@ impl FilesSorter {
match self.by {
SortBy::None => return false,
SortBy::Alphabetical => items.sort_unstable_by(by_alphabetical),
SortBy::Created => items.sort_unstable_by(|a, b| {
let ord = self.cmp(a.created, b.created, self.promote(a, b));
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
}),
SortBy::Modified => items.sort_unstable_by(|a, b| {
let ord = self.cmp(a.modified, b.modified, self.promote(a, b));
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
}),
SortBy::Created => items.sort_unstable_by(|a, b| {
let ord = self.cmp(a.created, b.created, self.promote(a, b));
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
}),
SortBy::Extension => items.sort_unstable_by(|a, b| {
let ord = if self.sensitive {
self.cmp(a.url.extension(), b.url.extension(), self.promote(a, b))
} else {
self.cmp(
a.url.extension().map(|s| s.to_ascii_lowercase()),
b.url.extension().map(|s| s.to_ascii_lowercase()),
self.promote(a, b),
)
};
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
}),
SortBy::Alphabetical => items.sort_unstable_by(by_alphabetical),
SortBy::Natural => self.sort_naturally(items),
SortBy::Size => items.sort_unstable_by(|a, b| {
let aa = if a.is_dir() { sizes.get(&a.url).copied() } else { None };