diff --git a/yazi-config/src/keymap/control.rs b/yazi-config/src/keymap/control.rs index 23ae3cf7..a41b310a 100644 --- a/yazi-config/src/keymap/control.rs +++ b/yazi-config/src/keymap/control.rs @@ -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, #[serde(deserialize_with = "super::exec_deserialize")] @@ -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 { match self { diff --git a/yazi-config/src/which/which.rs b/yazi-config/src/which/which.rs index 96caccc8..b8e3305c 100644 --- a/yazi-config/src/which/which.rs +++ b/yazi-config/src/which/which.rs @@ -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 { @@ -19,9 +19,6 @@ impl Default for Which { which: Which, } - let which = toml::from_str::(&MERGED_YAZI).unwrap().which; - - check_validation(which.validate()); - which + toml::from_str::(&MERGED_YAZI).unwrap().which } } diff --git a/yazi-core/src/folder/sorter.rs b/yazi-core/src/folder/sorter.rs index 4dea6083..850e0249 100644 --- a/yazi-core/src/folder/sorter.rs +++ b/yazi-core/src/folder/sorter.rs @@ -12,9 +12,9 @@ pub struct FilesSorter { } impl FilesSorter { - pub(super) fn sort(&self, items: &mut Vec, sizes: &BTreeMap) -> bool { + pub(super) fn sort(&self, items: &mut Vec, sizes: &BTreeMap) { if items.is_empty() { - return false; + return; } let by_alphabetical = |a: &File, b: &File| { @@ -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 } @@ -60,7 +60,6 @@ impl FilesSorter { if ord == Ordering::Equal { by_alphabetical(a, b) } else { ord } }), } - true } fn sort_naturally(&self, items: &mut Vec) { diff --git a/yazi-core/src/which/sorter.rs b/yazi-core/src/which/sorter.rs index 0fd92f16..d9b19631 100644 --- a/yazi-core/src/which/sorter.rs +++ b/yazi-core/src/which/sorter.rs @@ -1,3 +1,5 @@ +use std::{borrow::Cow, mem}; + use yazi_config::{keymap::ControlCow, which::SortBy}; use yazi_shared::natsort; @@ -9,30 +11,31 @@ pub struct WhichSorter { } impl WhichSorter { - pub(super) fn sort(&self, items: &mut Vec) -> bool { - if items.is_empty() { - return false; + pub(super) fn sort(&self, items: &mut Vec) { + if self.by == SortBy::None || items.is_empty() { + return; } - let by_alphabetical = |a: &str, b: &str| { - let ordering = natsort(a.as_bytes(), b.as_bytes(), !self.sensitive); + 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(), + }); + } + + 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::(); - let b = b.on.iter().map(|c| c.to_string()).collect::(); - 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()); + for i in indices { + new.push(mem::take(&mut items[i])); } - - true + *items = new; } }