Skip to content

Commit

Permalink
feat: new sort_sensitive option, and sort case-insensitively by def…
Browse files Browse the repository at this point in the history
…ault (sxyazi#155)
  • Loading branch information
Linus789 authored and sxyazi committed Sep 14, 2023
1 parent b62307d commit 902c094
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl Executor {
let b = cx.manager.active_mut().set_sorter(FilesSorter {
by: SortBy::try_from(exec.args.get(0).cloned().unwrap_or_default())
.unwrap_or_default(),
sensitive: exec.named.contains_key("sensitive"),
reverse: exec.named.contains_key("reverse"),
dir_first: exec.named.contains_key("dir_first"),
});
Expand Down
5 changes: 5 additions & 0 deletions config/docs/yazi.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
- `"natural"`: Sort naturally, e.g. `1.md` < `2.md` < `10.md`
- `"size"`: Sort by file size

- sort_sensitive: Sort case-sensitively

- `true`: Case-sensitive
- `false`: Case-insensitive

- sort_reverse: Display files in reverse order

- `true`: Reverse order
Expand Down
1 change: 1 addition & 0 deletions config/preset/yazi.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[manager]
layout = [ 1, 4, 3 ]
sort_by = "modified"
sort_sensitive = false
sort_reverse = true
sort_dir_first = true
show_hidden = false
Expand Down
1 change: 1 addition & 0 deletions config/src/manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Manager {

// Sorting
pub sort_by: SortBy,
pub sort_sensitive: bool,
pub sort_reverse: bool,
pub sort_dir_first: bool,

Expand Down
32 changes: 23 additions & 9 deletions core/src/files/sorter.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +8,7 @@ use super::File;
#[derive(Clone, Copy, PartialEq)]
pub struct FilesSorter {
pub by: SortBy,
pub sensitive: bool,
pub reverse: bool,
pub dir_first: bool,
}
Expand All @@ -16,6 +17,7 @@ impl Default for FilesSorter {
fn default() -> Self {
Self {
by: MANAGER.sort_by,
sensitive: MANAGER.sort_sensitive,
reverse: MANAGER.sort_reverse,
dir_first: MANAGER.sort_dir_first,
}
Expand All @@ -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.sensitive {
return self.cmp(&*a.url, &*b.url, self.promote(a, b));
}

self.cmp(
a.url.as_os_str().to_ascii_lowercase(),
b.url.as_os_str().to_ascii_lowercase(),
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));
Expand Down Expand Up @@ -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)
} else {
natord::compare(&entities[a].0, &entities[b].0)
return promote;
}

let ordering = if self.sensitive {
natord::compare(&entities[a].0, &entities[b].0)
} else {
natord::compare_ignore_case(&entities[a].0, &entities[b].0)
};

if self.reverse { ordering.reverse() } else { ordering }
});

let dummy = File {
Expand Down

0 comments on commit 902c094

Please sign in to comment.