Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable the ability to sort the which key #662

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion yazi-config/src/keymap/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use yazi_shared::event::Cmd;

use super::Key;

#[derive(Debug, Deserialize)]
#[derive(Debug, Default, Deserialize)]
pub struct Control {
pub on: Vec<Key>,
#[serde(deserialize_with = "super::exec_deserialize")]
Expand Down Expand Up @@ -68,6 +68,10 @@ impl Deref for ControlCow {
}
}

impl Default for ControlCow {
fn default() -> Self { Self::Owned(Control::default()) }
}

impl ControlCow {
pub fn into_seq(self) -> VecDeque<Cmd> {
match self {
Expand Down
7 changes: 2 additions & 5 deletions yazi-config/src/which/which.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use validator::Validate;

use super::SortBy;
use crate::{validation::check_validation, MERGED_YAZI};
use crate::MERGED_YAZI;

#[derive(Debug, Deserialize, Serialize, Validate)]
pub struct Which {
Expand All @@ -19,9 +19,6 @@ impl Default for Which {
which: Which,
}

let which = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().which;

check_validation(which.validate());
which
toml::from_str::<Outer>(&MERGED_YAZI).unwrap().which
}
}
7 changes: 3 additions & 4 deletions yazi-core/src/folder/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub struct FilesSorter {
}

impl FilesSorter {
pub(super) fn sort(&self, items: &mut Vec<File>, sizes: &BTreeMap<Url, u64>) -> bool {
pub(super) fn sort(&self, items: &mut Vec<File>, sizes: &BTreeMap<Url, u64>) {
if items.is_empty() {
return false;
return;
}

let by_alphabetical = |a: &File, b: &File| {
Expand All @@ -30,7 +30,7 @@ impl FilesSorter {
};

match self.by {
SortBy::None => return false,
SortBy::None => {}
SortBy::Modified => items.sort_unstable_by(|a, b| {
let ord = self.cmp(a.modified, b.modified, self.promote(a, b));
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
Expand Down Expand Up @@ -60,7 +60,6 @@ impl FilesSorter {
if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord }
}),
}
true
}

fn sort_naturally(&self, items: &mut Vec<File>) {
Expand Down
43 changes: 23 additions & 20 deletions yazi-core/src/which/sorter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{borrow::Cow, mem};

use yazi_config::{keymap::ControlCow, which::SortBy};
use yazi_shared::natsort;

Expand All @@ -9,30 +11,31 @@ pub struct WhichSorter {
}

impl WhichSorter {
pub(super) fn sort(&self, items: &mut Vec<ControlCow>) -> bool {
if items.is_empty() {
return false;
pub(super) fn sort(&self, items: &mut Vec<ControlCow>) {
if self.by == SortBy::None || items.is_empty() {
return;
}

let mut indices = Vec::with_capacity(items.len());
let mut entities = Vec::with_capacity(items.len());
for (i, ctrl) in items.iter().enumerate() {
indices.push(i);
entities.push(match self.by {
SortBy::None => unreachable!(),
SortBy::Key => Cow::Owned(ctrl.on()),
SortBy::Desc => ctrl.desc_or_exec(),
});
}

let by_alphabetical = |a: &str, b: &str| {
let ordering = natsort(a.as_bytes(), b.as_bytes(), !self.sensitive);
indices.sort_unstable_by(|&a, &b| {
let ordering = natsort(entities[a].as_bytes(), entities[b].as_bytes(), !self.sensitive);
if self.reverse { ordering.reverse() } else { ordering }
};
});

match self.by {
SortBy::None => return false,
SortBy::Key => items.sort_unstable_by(|a, b| {
let a = a.on.iter().map(|c| c.to_string()).collect::<String>();
let b = b.on.iter().map(|c| c.to_string()).collect::<String>();
by_alphabetical(&a, &b)
}),
SortBy::Desc => items.sort_unstable_by(|a, b| {
// what if description isn't present (need to check if it's mandatory or not)
// in case if it is not present, should I just panic ?
by_alphabetical(a.desc.as_ref().unwrap(), b.desc.as_ref().unwrap())
}),
let mut new = Vec::with_capacity(indices.len());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this:

*items = indices.into_iter().map(|i| mem::take(&mut items[i])).collect();

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, will apply this suggestion

for i in indices {
new.push(mem::take(&mut items[i]));
}

true
*items = new;
}
}