This commit is contained in:
sxyazi 2023-11-28 15:11:25 +08:00
parent 33b547d894
commit 4a2c122c2e
No known key found for this signature in database
3 changed files with 32 additions and 26 deletions

View file

@ -96,18 +96,18 @@ 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" },
{ on = [ ",", "S" ], exec = "sort size --reverse --dir_first", desc = "Sort by size (reverse)" }, { 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 # Tabs
{ on = [ "t" ], exec = "tab_create --current", desc = "Create a new tab using the current path" }, { on = [ "t" ], exec = "tab_create --current", desc = "Create a new tab using the current path" },

View file

@ -8,12 +8,12 @@ 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,
Extension,
} }
impl FromStr for SortBy { impl FromStr for SortBy {
@ -22,12 +22,12 @@ 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,
"extension" => Self::Extension,
_ => bail!("invalid sort_by value: {s}"), _ => bail!("invalid sort_by value: {s}"),
}) })
} }
@ -43,12 +43,12 @@ 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",
Self::Extension => "extension",
} }
.to_string() .to_string()
} }

View file

@ -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 };
@ -47,12 +59,6 @@ impl FilesSorter {
let ord = self.cmp(aa.unwrap_or(a.len), bb.unwrap_or(b.len), self.promote(a, b)); 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 } 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 true
} }