mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: support sorting by file extension (#405)
This commit is contained in:
parent
a1984725e4
commit
b41bea9e8f
3 changed files with 32 additions and 15 deletions
|
|
@ -96,12 +96,14 @@ keymap = [
|
||||||
{ on = [ "N" ], exec = "find_arrow --previous" },
|
{ on = [ "N" ], exec = "find_arrow --previous" },
|
||||||
|
|
||||||
# Sorting
|
# 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 --dir_first", desc = "Sort by modified time" },
|
||||||
{ on = [ ",", "M" ], exec = "sort modified --reverse --dir_first", desc = "Sort by modified time (reverse)" },
|
{ 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 --dir_first", desc = "Sort naturally" },
|
||||||
{ on = [ ",", "N" ], exec = "sort natural --reverse --dir_first", desc = "Sort naturally (reverse)" },
|
{ 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 --dir_first", desc = "Sort by size" },
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,10 @@ use serde::{Deserialize, Serialize};
|
||||||
pub enum SortBy {
|
pub enum SortBy {
|
||||||
#[default]
|
#[default]
|
||||||
None,
|
None,
|
||||||
Alphabetical,
|
|
||||||
Created,
|
|
||||||
Modified,
|
Modified,
|
||||||
|
Created,
|
||||||
|
Extension,
|
||||||
|
Alphabetical,
|
||||||
Natural,
|
Natural,
|
||||||
Size,
|
Size,
|
||||||
}
|
}
|
||||||
|
|
@ -21,9 +22,10 @@ impl FromStr for SortBy {
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
Ok(match s {
|
Ok(match s {
|
||||||
"none" => Self::None,
|
"none" => Self::None,
|
||||||
"alphabetical" => Self::Alphabetical,
|
|
||||||
"created" => Self::Created,
|
|
||||||
"modified" => Self::Modified,
|
"modified" => Self::Modified,
|
||||||
|
"created" => Self::Created,
|
||||||
|
"extension" => Self::Extension,
|
||||||
|
"alphabetical" => Self::Alphabetical,
|
||||||
"natural" => Self::Natural,
|
"natural" => Self::Natural,
|
||||||
"size" => Self::Size,
|
"size" => Self::Size,
|
||||||
_ => bail!("invalid sort_by value: {s}"),
|
_ => bail!("invalid sort_by value: {s}"),
|
||||||
|
|
@ -41,9 +43,10 @@ impl ToString for SortBy {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Self::None => "none",
|
Self::None => "none",
|
||||||
Self::Alphabetical => "alphabetical",
|
|
||||||
Self::Created => "created",
|
|
||||||
Self::Modified => "modified",
|
Self::Modified => "modified",
|
||||||
|
Self::Created => "created",
|
||||||
|
Self::Extension => "extension",
|
||||||
|
Self::Alphabetical => "alphabetical",
|
||||||
Self::Natural => "natural",
|
Self::Natural => "natural",
|
||||||
Self::Size => "size",
|
Self::Size => "size",
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,15 +31,27 @@ impl FilesSorter {
|
||||||
|
|
||||||
match self.by {
|
match self.by {
|
||||||
SortBy::None => return false,
|
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| {
|
SortBy::Modified => items.sort_unstable_by(|a, b| {
|
||||||
let ord = self.cmp(a.modified, b.modified, self.promote(a, b));
|
let ord = self.cmp(a.modified, b.modified, self.promote(a, b));
|
||||||
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
|
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::Natural => self.sort_naturally(items),
|
||||||
SortBy::Size => items.sort_unstable_by(|a, b| {
|
SortBy::Size => items.sort_unstable_by(|a, b| {
|
||||||
let aa = if a.is_dir() { sizes.get(&a.url).copied() } else { None };
|
let aa = if a.is_dir() { sizes.get(&a.url).copied() } else { None };
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue