mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: sort ignore case
This commit is contained in:
parent
9c6b3c9d1d
commit
1cf976e831
5 changed files with 48 additions and 26 deletions
|
|
@ -149,10 +149,11 @@ impl Executor {
|
|||
// Sorting
|
||||
"sort" => {
|
||||
let b = cx.manager.active_mut().set_sorter(FilesSorter {
|
||||
by: SortBy::try_from(exec.args.get(0).cloned().unwrap_or_default())
|
||||
by: SortBy::try_from(exec.args.get(0).cloned().unwrap_or_default())
|
||||
.unwrap_or_default(),
|
||||
reverse: exec.named.contains_key("reverse"),
|
||||
dir_first: exec.named.contains_key("dir_first"),
|
||||
reverse: exec.named.contains_key("reverse"),
|
||||
dir_first: exec.named.contains_key("dir_first"),
|
||||
ignore_case: exec.named.contains_key("ignore_case"),
|
||||
});
|
||||
cx.tasks.precache_size(&cx.manager.current().files);
|
||||
b
|
||||
|
|
|
|||
|
|
@ -24,6 +24,11 @@
|
|||
- `true`: Directories first
|
||||
- `false`: Respects `sort_by` and `sort_reverse` only
|
||||
|
||||
- sort_ignore_case: Ignore case while sorting
|
||||
|
||||
- `true`: Ignore case
|
||||
- `false`: Uppercase has precedence over lowercase
|
||||
|
||||
- show_hidden: Show hidden files
|
||||
|
||||
- `true`: Show
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
[manager]
|
||||
layout = [ 1, 4, 3 ]
|
||||
sort_by = "modified"
|
||||
sort_reverse = true
|
||||
sort_dir_first = true
|
||||
show_hidden = false
|
||||
show_symlink = true
|
||||
layout = [ 1, 4, 3 ]
|
||||
sort_by = "modified"
|
||||
sort_reverse = true
|
||||
sort_dir_first = true
|
||||
sort_ignore_case = false
|
||||
show_hidden = false
|
||||
show_symlink = true
|
||||
|
||||
[preview]
|
||||
tab_size = 2
|
||||
|
|
|
|||
|
|
@ -8,9 +8,10 @@ pub struct Manager {
|
|||
pub layout: ManagerLayout,
|
||||
|
||||
// Sorting
|
||||
pub sort_by: SortBy,
|
||||
pub sort_reverse: bool,
|
||||
pub sort_dir_first: bool,
|
||||
pub sort_by: SortBy,
|
||||
pub sort_reverse: bool,
|
||||
pub sort_dir_first: bool,
|
||||
pub sort_ignore_case: bool,
|
||||
|
||||
// Display
|
||||
pub show_hidden: bool,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::{cmp::Ordering, collections::BTreeMap, mem};
|
||||
use std::{cmp::Ordering, collections::BTreeMap, mem, ops::Deref};
|
||||
|
||||
use config::{manager::SortBy, MANAGER};
|
||||
use shared::Url;
|
||||
|
|
@ -7,17 +7,19 @@ use super::File;
|
|||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct FilesSorter {
|
||||
pub by: SortBy,
|
||||
pub reverse: bool,
|
||||
pub dir_first: bool,
|
||||
pub by: SortBy,
|
||||
pub reverse: bool,
|
||||
pub dir_first: bool,
|
||||
pub ignore_case: bool,
|
||||
}
|
||||
|
||||
impl Default for FilesSorter {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
by: MANAGER.sort_by,
|
||||
reverse: MANAGER.sort_reverse,
|
||||
dir_first: MANAGER.sort_dir_first,
|
||||
by: MANAGER.sort_by,
|
||||
reverse: MANAGER.sort_reverse,
|
||||
dir_first: MANAGER.sort_dir_first,
|
||||
ignore_case: MANAGER.sort_ignore_case,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -29,9 +31,17 @@ impl FilesSorter {
|
|||
}
|
||||
|
||||
match self.by {
|
||||
SortBy::Alphabetical => {
|
||||
items.sort_unstable_by(|a, b| self.cmp(&*a.url, &*b.url, self.promote(a, b)))
|
||||
}
|
||||
SortBy::Alphabetical => items.sort_unstable_by(|a, b| {
|
||||
if self.ignore_case {
|
||||
self.cmp(
|
||||
a.url.deref().as_os_str().to_ascii_lowercase(),
|
||||
b.url.deref().as_os_str().to_ascii_lowercase(),
|
||||
self.promote(a, b),
|
||||
)
|
||||
} else {
|
||||
self.cmp(&*a.url, &*b.url, self.promote(a, b))
|
||||
}
|
||||
}),
|
||||
SortBy::Created => items.sort_unstable_by(|a, b| {
|
||||
if let (Ok(aa), Ok(bb)) = (a.meta.created(), b.meta.created()) {
|
||||
return self.cmp(aa, bb, self.promote(a, b));
|
||||
|
|
@ -65,12 +75,16 @@ impl FilesSorter {
|
|||
indices.sort_unstable_by(|&a, &b| {
|
||||
let promote = self.promote(entities[a].1, entities[b].1);
|
||||
if promote != Ordering::Equal {
|
||||
promote
|
||||
} else if self.reverse {
|
||||
natord::compare(&entities[b].0, &entities[a].0)
|
||||
return promote;
|
||||
}
|
||||
|
||||
let ordering = if self.ignore_case {
|
||||
natord::compare_ignore_case(&entities[a].0, &entities[b].0)
|
||||
} else {
|
||||
natord::compare(&entities[a].0, &entities[b].0)
|
||||
}
|
||||
};
|
||||
|
||||
if self.reverse { ordering.reverse() } else { ordering }
|
||||
});
|
||||
|
||||
let dummy = File {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue