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
|
// Sorting
|
||||||
"sort" => {
|
"sort" => {
|
||||||
let b = cx.manager.active_mut().set_sorter(FilesSorter {
|
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(),
|
.unwrap_or_default(),
|
||||||
reverse: exec.named.contains_key("reverse"),
|
reverse: exec.named.contains_key("reverse"),
|
||||||
dir_first: exec.named.contains_key("dir_first"),
|
dir_first: exec.named.contains_key("dir_first"),
|
||||||
|
ignore_case: exec.named.contains_key("ignore_case"),
|
||||||
});
|
});
|
||||||
cx.tasks.precache_size(&cx.manager.current().files);
|
cx.tasks.precache_size(&cx.manager.current().files);
|
||||||
b
|
b
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,11 @@
|
||||||
- `true`: Directories first
|
- `true`: Directories first
|
||||||
- `false`: Respects `sort_by` and `sort_reverse` only
|
- `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
|
- show_hidden: Show hidden files
|
||||||
|
|
||||||
- `true`: Show
|
- `true`: Show
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
[manager]
|
[manager]
|
||||||
layout = [ 1, 4, 3 ]
|
layout = [ 1, 4, 3 ]
|
||||||
sort_by = "modified"
|
sort_by = "modified"
|
||||||
sort_reverse = true
|
sort_reverse = true
|
||||||
sort_dir_first = true
|
sort_dir_first = true
|
||||||
show_hidden = false
|
sort_ignore_case = false
|
||||||
show_symlink = true
|
show_hidden = false
|
||||||
|
show_symlink = true
|
||||||
|
|
||||||
[preview]
|
[preview]
|
||||||
tab_size = 2
|
tab_size = 2
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,10 @@ pub struct Manager {
|
||||||
pub layout: ManagerLayout,
|
pub layout: ManagerLayout,
|
||||||
|
|
||||||
// Sorting
|
// Sorting
|
||||||
pub sort_by: SortBy,
|
pub sort_by: SortBy,
|
||||||
pub sort_reverse: bool,
|
pub sort_reverse: bool,
|
||||||
pub sort_dir_first: bool,
|
pub sort_dir_first: bool,
|
||||||
|
pub sort_ignore_case: bool,
|
||||||
|
|
||||||
// Display
|
// Display
|
||||||
pub show_hidden: bool,
|
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 config::{manager::SortBy, MANAGER};
|
||||||
use shared::Url;
|
use shared::Url;
|
||||||
|
|
@ -7,17 +7,19 @@ use super::File;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
pub struct FilesSorter {
|
pub struct FilesSorter {
|
||||||
pub by: SortBy,
|
pub by: SortBy,
|
||||||
pub reverse: bool,
|
pub reverse: bool,
|
||||||
pub dir_first: bool,
|
pub dir_first: bool,
|
||||||
|
pub ignore_case: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FilesSorter {
|
impl Default for FilesSorter {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
by: MANAGER.sort_by,
|
by: MANAGER.sort_by,
|
||||||
reverse: MANAGER.sort_reverse,
|
reverse: MANAGER.sort_reverse,
|
||||||
dir_first: MANAGER.sort_dir_first,
|
dir_first: MANAGER.sort_dir_first,
|
||||||
|
ignore_case: MANAGER.sort_ignore_case,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -29,9 +31,17 @@ impl FilesSorter {
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.by {
|
match self.by {
|
||||||
SortBy::Alphabetical => {
|
SortBy::Alphabetical => items.sort_unstable_by(|a, b| {
|
||||||
items.sort_unstable_by(|a, b| self.cmp(&*a.url, &*b.url, self.promote(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| {
|
SortBy::Created => items.sort_unstable_by(|a, b| {
|
||||||
if let (Ok(aa), Ok(bb)) = (a.meta.created(), b.meta.created()) {
|
if let (Ok(aa), Ok(bb)) = (a.meta.created(), b.meta.created()) {
|
||||||
return self.cmp(aa, bb, self.promote(a, b));
|
return self.cmp(aa, bb, self.promote(a, b));
|
||||||
|
|
@ -65,12 +75,16 @@ impl FilesSorter {
|
||||||
indices.sort_unstable_by(|&a, &b| {
|
indices.sort_unstable_by(|&a, &b| {
|
||||||
let promote = self.promote(entities[a].1, entities[b].1);
|
let promote = self.promote(entities[a].1, entities[b].1);
|
||||||
if promote != Ordering::Equal {
|
if promote != Ordering::Equal {
|
||||||
promote
|
return promote;
|
||||||
} else if self.reverse {
|
}
|
||||||
natord::compare(&entities[b].0, &entities[a].0)
|
|
||||||
|
let ordering = if self.ignore_case {
|
||||||
|
natord::compare_ignore_case(&entities[a].0, &entities[b].0)
|
||||||
} else {
|
} else {
|
||||||
natord::compare(&entities[a].0, &entities[b].0)
|
natord::compare(&entities[a].0, &entities[b].0)
|
||||||
}
|
};
|
||||||
|
|
||||||
|
if self.reverse { ordering.reverse() } else { ordering }
|
||||||
});
|
});
|
||||||
|
|
||||||
let dummy = File {
|
let dummy = File {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue