From f6b4c923b4480cce18c8f7da3c555586d8c5ca13 Mon Sep 17 00:00:00 2001 From: coolabhays Date: Sun, 11 Feb 2024 18:43:06 +0530 Subject: [PATCH 1/5] feat: Enable the ability to sort the which key * sort "which" window in following way: - sort_by: "key|desc|none" - sort_sensitive: true|false - sort_reverse: true|false --- yazi-config/preset/yazi.toml | 5 ++++ yazi-config/src/keymap/control.rs | 1 + yazi-config/src/lib.rs | 3 ++ yazi-config/src/which/mod.rs | 5 ++++ yazi-config/src/which/sorting.rs | 44 ++++++++++++++++++++++++++++ yazi-config/src/which/which.rs | 27 +++++++++++++++++ yazi-core/src/which/commands/show.rs | 31 +++++++++++--------- yazi-core/src/which/config.rs | 41 ++++++++++++++++++++++++++ yazi-core/src/which/mod.rs | 4 +++ yazi-core/src/which/sorter.rs | 39 ++++++++++++++++++++++++ yazi-core/src/which/which.rs | 6 +++- 11 files changed, 191 insertions(+), 15 deletions(-) create mode 100644 yazi-config/src/which/mod.rs create mode 100644 yazi-config/src/which/sorting.rs create mode 100644 yazi-config/src/which/which.rs create mode 100644 yazi-core/src/which/config.rs create mode 100644 yazi-core/src/which/sorter.rs diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 789263ccf..53005265f 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -2,6 +2,11 @@ # If you encounter any issues, please make an issue at https://github.com/yazi-rs/schemas. "$schema" = "https://yazi-rs.github.io/schemas/yazi.json" +[which] +sort_by = "none" +sort_sensitive = false +sort_reverse = false + [manager] ratio = [ 1, 4, 3 ] sort_by = "alphabetical" diff --git a/yazi-config/src/keymap/control.rs b/yazi-config/src/keymap/control.rs index 5fae59002..23ae3cf7e 100644 --- a/yazi-config/src/keymap/control.rs +++ b/yazi-config/src/keymap/control.rs @@ -43,6 +43,7 @@ impl Control { } } +#[derive(Debug)] pub enum ControlCow { Owned(Control), Borrowed(&'static Control), diff --git a/yazi-config/src/lib.rs b/yazi-config/src/lib.rs index 3347986c8..6ef59ed80 100644 --- a/yazi-config/src/lib.rs +++ b/yazi-config/src/lib.rs @@ -18,6 +18,7 @@ mod tasks; pub mod theme; mod validation; mod xdg; +pub mod which; pub use layout::*; pub(crate) use pattern::*; @@ -43,6 +44,7 @@ pub static TASKS: RoCell = RoCell::new(); pub static THEME: RoCell = RoCell::new(); pub static INPUT: RoCell = RoCell::new(); pub static SELECT: RoCell = RoCell::new(); +pub static WHICH: RoCell = RoCell::new(); pub fn init() { ARGS.with(Default::default); @@ -63,4 +65,5 @@ pub fn init() { THEME.with(Default::default); INPUT.with(Default::default); SELECT.with(Default::default); + WHICH.with(Default::default); } diff --git a/yazi-config/src/which/mod.rs b/yazi-config/src/which/mod.rs new file mode 100644 index 000000000..2033591be --- /dev/null +++ b/yazi-config/src/which/mod.rs @@ -0,0 +1,5 @@ +mod sorting; +mod which; + +pub use sorting::*; +pub use which::*; diff --git a/yazi-config/src/which/sorting.rs b/yazi-config/src/which/sorting.rs new file mode 100644 index 000000000..d9a0566fb --- /dev/null +++ b/yazi-config/src/which/sorting.rs @@ -0,0 +1,44 @@ +use std::str::FromStr; + +use anyhow::bail; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] +#[serde(try_from = "String")] +pub enum SortBy { + #[default] + None, + Key, + Desc, +} + +impl FromStr for SortBy { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + Ok(match s { + "none" => Self::None, + "key" => Self::Key, + "desc" => Self::Desc, + _ => bail!("Invalid sort option: {s}") + }) + } +} + +impl TryFrom for SortBy { + type Error = anyhow::Error; + + fn try_from(value: String) -> Result { + Self::from_str(&value) + } +} + +impl ToString for SortBy { + fn to_string(&self) -> String { + match self { + Self::None => "none", + Self::Key => "key", + Self::Desc => "desc", + }.to_string() + } +} diff --git a/yazi-config/src/which/which.rs b/yazi-config/src/which/which.rs new file mode 100644 index 000000000..96caccc81 --- /dev/null +++ b/yazi-config/src/which/which.rs @@ -0,0 +1,27 @@ +use serde::{Deserialize, Serialize}; +use validator::Validate; + +use super::SortBy; +use crate::{validation::check_validation, MERGED_YAZI}; + +#[derive(Debug, Deserialize, Serialize, Validate)] +pub struct Which { + // Sorting + pub sort_by: SortBy, + pub sort_sensitive: bool, + pub sort_reverse: bool, +} + +impl Default for Which { + fn default() -> Self { + #[derive(Deserialize)] + struct Outer { + which: Which, + } + + let which = toml::from_str::(&MERGED_YAZI).unwrap().which; + + check_validation(which.validate()); + which + } +} diff --git a/yazi-core/src/which/commands/show.rs b/yazi-core/src/which/commands/show.rs index 639704627..092eb5d4d 100644 --- a/yazi-core/src/which/commands/show.rs +++ b/yazi-core/src/which/commands/show.rs @@ -42,18 +42,21 @@ impl Which { render!(); } - pub fn show_with(&mut self, key: &Key, layer: Layer) { - self.layer = layer; - self.times = 1; - self.cands = KEYMAP - .get(layer) - .iter() - .filter(|c| c.on.len() > 1 && &c.on[0] == key) - .map(|c| c.into()) - .collect(); - - self.visible = true; - self.silent = false; - render!(); - } + pub fn show_with(&mut self, key: &Key, layer: Layer) { + self.layer = layer; + self.times = 1; + self.cands = KEYMAP + .get(layer) + .iter() + .filter(|c| c.on.len() > 1 && &c.on[0] == key) + .map(|c| c.into()) + .collect(); + + // sort "which" + self.conf.sorter().sort(&mut self.cands); + + self.visible = true; + self.silent = false; + render!(); + } } diff --git a/yazi-core/src/which/config.rs b/yazi-core/src/which/config.rs new file mode 100644 index 000000000..986eb3a57 --- /dev/null +++ b/yazi-core/src/which/config.rs @@ -0,0 +1,41 @@ +use yazi_config::{which::SortBy, WHICH}; + +use crate::which::WhichSorter; + +#[derive(Clone, PartialEq, Debug)] +pub struct Config { + // Sorting + pub sort_by: SortBy, + pub sort_sensitive: bool, + pub sort_reverse: bool, +} + +impl Default for Config { + fn default() -> Self { + Self { + // Sorting + sort_by: WHICH.sort_by, + sort_sensitive: WHICH.sort_sensitive, + sort_reverse: WHICH.sort_reverse, + } + } +} + +impl Config { + #[allow(unused)] + pub(super) fn patch(&mut self, f: F) -> bool { + let old = self.clone(); + f(self); + *self != old + } + + #[inline] + pub(super) fn sorter(&self) -> WhichSorter { + WhichSorter { + by: self.sort_by, + sensitive: self.sort_sensitive, + reverse: self.sort_reverse, + } + } +} + diff --git a/yazi-core/src/which/mod.rs b/yazi-core/src/which/mod.rs index b2098e80f..824b99b8c 100644 --- a/yazi-core/src/which/mod.rs +++ b/yazi-core/src/which/mod.rs @@ -1,4 +1,8 @@ mod commands; mod which; +mod config; +mod sorter; pub use which::*; +pub use config::*; +pub use sorter::*; diff --git a/yazi-core/src/which/sorter.rs b/yazi-core/src/which/sorter.rs new file mode 100644 index 000000000..d73d9f912 --- /dev/null +++ b/yazi-core/src/which/sorter.rs @@ -0,0 +1,39 @@ +use yazi_config::{which::SortBy, keymap::ControlCow}; +use yazi_shared::natsort; + +#[derive(Clone, Copy, Default, PartialEq)] +pub struct WhichSorter { + pub by: SortBy, + pub sensitive: bool, + pub reverse: bool, +} + +impl WhichSorter { + pub(super) fn sort(&self, items: &mut Vec) -> bool { + if items.is_empty() { + return false; + } + + let by_alphabetical = |a: &str, b: &str| { + let ordering = natsort(a.as_bytes(), 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()) + }) + } + + true + } + +} diff --git a/yazi-core/src/which/which.rs b/yazi-core/src/which/which.rs index 8cc627c80..b5e69aaa9 100644 --- a/yazi-core/src/which/which.rs +++ b/yazi-core/src/which/which.rs @@ -1,12 +1,16 @@ use yazi_config::keymap::{ControlCow, Key}; use yazi_shared::{emit, render, Layer}; -#[derive(Default)] +use crate::which::Config; + +#[derive(Default, Debug)] pub struct Which { pub(super) layer: Layer, pub times: usize, pub cands: Vec, + pub conf: Config, + pub visible: bool, pub silent: bool, } From 79f63a9ae21c7e57e4c8438002d0c803dff2d899 Mon Sep 17 00:00:00 2001 From: coolabhays Date: Sun, 11 Feb 2024 20:36:03 +0530 Subject: [PATCH 2/5] feat: Enable the ability to sort the which key formatted properly --- yazi-config/src/lib.rs | 4 +-- yazi-config/src/which/sorting.rs | 43 ++++++++++++++-------------- yazi-core/src/which/commands/show.rs | 34 +++++++++++----------- yazi-core/src/which/config.rs | 17 ++++------- yazi-core/src/which/mod.rs | 4 +-- yazi-core/src/which/sorter.rs | 37 ++++++++++++------------ yazi-core/src/which/which.rs | 2 +- 7 files changed, 66 insertions(+), 75 deletions(-) diff --git a/yazi-config/src/lib.rs b/yazi-config/src/lib.rs index 6ef59ed80..cf7826dc1 100644 --- a/yazi-config/src/lib.rs +++ b/yazi-config/src/lib.rs @@ -17,8 +17,8 @@ mod priority; mod tasks; pub mod theme; mod validation; -mod xdg; pub mod which; +mod xdg; pub use layout::*; pub(crate) use pattern::*; @@ -65,5 +65,5 @@ pub fn init() { THEME.with(Default::default); INPUT.with(Default::default); SELECT.with(Default::default); - WHICH.with(Default::default); + WHICH.with(Default::default); } diff --git a/yazi-config/src/which/sorting.rs b/yazi-config/src/which/sorting.rs index d9a0566fb..4ce103e4d 100644 --- a/yazi-config/src/which/sorting.rs +++ b/yazi-config/src/which/sorting.rs @@ -8,37 +8,36 @@ use serde::{Deserialize, Serialize}; pub enum SortBy { #[default] None, - Key, - Desc, + Key, + Desc, } impl FromStr for SortBy { - type Err = anyhow::Error; + type Err = anyhow::Error; - fn from_str(s: &str) -> Result { - Ok(match s { - "none" => Self::None, - "key" => Self::Key, - "desc" => Self::Desc, - _ => bail!("Invalid sort option: {s}") - }) - } + fn from_str(s: &str) -> Result { + Ok(match s { + "none" => Self::None, + "key" => Self::Key, + "desc" => Self::Desc, + _ => bail!("Invalid sort option: {s}"), + }) + } } impl TryFrom for SortBy { - type Error = anyhow::Error; + type Error = anyhow::Error; - fn try_from(value: String) -> Result { - Self::from_str(&value) - } + fn try_from(value: String) -> Result { Self::from_str(&value) } } impl ToString for SortBy { - fn to_string(&self) -> String { - match self { - Self::None => "none", - Self::Key => "key", - Self::Desc => "desc", - }.to_string() - } + fn to_string(&self) -> String { + match self { + Self::None => "none", + Self::Key => "key", + Self::Desc => "desc", + } + .to_string() + } } diff --git a/yazi-core/src/which/commands/show.rs b/yazi-core/src/which/commands/show.rs index 092eb5d4d..2afb66492 100644 --- a/yazi-core/src/which/commands/show.rs +++ b/yazi-core/src/which/commands/show.rs @@ -42,21 +42,21 @@ impl Which { render!(); } - pub fn show_with(&mut self, key: &Key, layer: Layer) { - self.layer = layer; - self.times = 1; - self.cands = KEYMAP - .get(layer) - .iter() - .filter(|c| c.on.len() > 1 && &c.on[0] == key) - .map(|c| c.into()) - .collect(); - - // sort "which" - self.conf.sorter().sort(&mut self.cands); - - self.visible = true; - self.silent = false; - render!(); - } + pub fn show_with(&mut self, key: &Key, layer: Layer) { + self.layer = layer; + self.times = 1; + self.cands = KEYMAP + .get(layer) + .iter() + .filter(|c| c.on.len() > 1 && &c.on[0] == key) + .map(|c| c.into()) + .collect(); + + // sort "which" + self.conf.sorter().sort(&mut self.cands); + + self.visible = true; + self.silent = false; + render!(); + } } diff --git a/yazi-core/src/which/config.rs b/yazi-core/src/which/config.rs index 986eb3a57..6471972ee 100644 --- a/yazi-core/src/which/config.rs +++ b/yazi-core/src/which/config.rs @@ -22,20 +22,13 @@ impl Default for Config { } impl Config { - #[allow(unused)] - pub(super) fn patch(&mut self, f: F) -> bool { - let old = self.clone(); - f(self); - *self != old - } #[inline] pub(super) fn sorter(&self) -> WhichSorter { - WhichSorter { - by: self.sort_by, - sensitive: self.sort_sensitive, - reverse: self.sort_reverse, - } + WhichSorter { + by: self.sort_by, + sensitive: self.sort_sensitive, + reverse: self.sort_reverse, + } } } - diff --git a/yazi-core/src/which/mod.rs b/yazi-core/src/which/mod.rs index 824b99b8c..76a9d453c 100644 --- a/yazi-core/src/which/mod.rs +++ b/yazi-core/src/which/mod.rs @@ -1,8 +1,8 @@ mod commands; -mod which; mod config; mod sorter; +mod which; -pub use which::*; pub use config::*; pub use sorter::*; +pub use which::*; diff --git a/yazi-core/src/which/sorter.rs b/yazi-core/src/which/sorter.rs index d73d9f912..0fd92f16e 100644 --- a/yazi-core/src/which/sorter.rs +++ b/yazi-core/src/which/sorter.rs @@ -1,4 +1,4 @@ -use yazi_config::{which::SortBy, keymap::ControlCow}; +use yazi_config::{keymap::ControlCow, which::SortBy}; use yazi_shared::natsort; #[derive(Clone, Copy, Default, PartialEq)] @@ -14,26 +14,25 @@ impl WhichSorter { return false; } - let by_alphabetical = |a: &str, b: &str| { - let ordering = natsort(a.as_bytes(), b.as_bytes(), !self.sensitive); - if self.reverse { ordering.reverse() } else { ordering } - }; + let by_alphabetical = |a: &str, b: &str| { + let ordering = natsort(a.as_bytes(), 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()) - }) - } + 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()) + }), + } true } - } diff --git a/yazi-core/src/which/which.rs b/yazi-core/src/which/which.rs index b5e69aaa9..fc8110efe 100644 --- a/yazi-core/src/which/which.rs +++ b/yazi-core/src/which/which.rs @@ -9,7 +9,7 @@ pub struct Which { pub times: usize, pub cands: Vec, - pub conf: Config, + pub conf: Config, pub visible: bool, pub silent: bool, From b44452dbd914142d1ef5b5edfd37716e23e2d2d2 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 12 Feb 2024 18:18:27 +0800 Subject: [PATCH 3/5] Simplify the code --- yazi-config/preset/yazi.toml | 10 ++++---- yazi-config/src/which/sorting.rs | 11 --------- yazi-core/src/which/commands/show.rs | 4 +--- yazi-core/src/which/config.rs | 34 ---------------------------- yazi-core/src/which/mod.rs | 2 -- yazi-core/src/which/which.rs | 34 +++++++++++++++++++--------- 6 files changed, 29 insertions(+), 66 deletions(-) delete mode 100644 yazi-core/src/which/config.rs diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 53005265f..82c4cdde9 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -2,11 +2,6 @@ # If you encounter any issues, please make an issue at https://github.com/yazi-rs/schemas. "$schema" = "https://yazi-rs.github.io/schemas/yazi.json" -[which] -sort_by = "none" -sort_sensitive = false -sort_reverse = false - [manager] ratio = [ 1, 4, 3 ] sort_by = "alphabetical" @@ -188,5 +183,10 @@ open_title = "Open with:" open_origin = "hovered" open_offset = [ 0, 1, 50, 7 ] +[which] +sort_by = "none" +sort_sensitive = false +sort_reverse = false + [log] enabled = false diff --git a/yazi-config/src/which/sorting.rs b/yazi-config/src/which/sorting.rs index 4ce103e4d..cb38e8030 100644 --- a/yazi-config/src/which/sorting.rs +++ b/yazi-config/src/which/sorting.rs @@ -30,14 +30,3 @@ impl TryFrom for SortBy { fn try_from(value: String) -> Result { Self::from_str(&value) } } - -impl ToString for SortBy { - fn to_string(&self) -> String { - match self { - Self::None => "none", - Self::Key => "key", - Self::Desc => "desc", - } - .to_string() - } -} diff --git a/yazi-core/src/which/commands/show.rs b/yazi-core/src/which/commands/show.rs index 2afb66492..4794e6ecd 100644 --- a/yazi-core/src/which/commands/show.rs +++ b/yazi-core/src/which/commands/show.rs @@ -52,9 +52,7 @@ impl Which { .map(|c| c.into()) .collect(); - // sort "which" - self.conf.sorter().sort(&mut self.cands); - + self.sorter().sort(&mut self.cands); self.visible = true; self.silent = false; render!(); diff --git a/yazi-core/src/which/config.rs b/yazi-core/src/which/config.rs deleted file mode 100644 index 6471972ee..000000000 --- a/yazi-core/src/which/config.rs +++ /dev/null @@ -1,34 +0,0 @@ -use yazi_config::{which::SortBy, WHICH}; - -use crate::which::WhichSorter; - -#[derive(Clone, PartialEq, Debug)] -pub struct Config { - // Sorting - pub sort_by: SortBy, - pub sort_sensitive: bool, - pub sort_reverse: bool, -} - -impl Default for Config { - fn default() -> Self { - Self { - // Sorting - sort_by: WHICH.sort_by, - sort_sensitive: WHICH.sort_sensitive, - sort_reverse: WHICH.sort_reverse, - } - } -} - -impl Config { - - #[inline] - pub(super) fn sorter(&self) -> WhichSorter { - WhichSorter { - by: self.sort_by, - sensitive: self.sort_sensitive, - reverse: self.sort_reverse, - } - } -} diff --git a/yazi-core/src/which/mod.rs b/yazi-core/src/which/mod.rs index 76a9d453c..ebc217a84 100644 --- a/yazi-core/src/which/mod.rs +++ b/yazi-core/src/which/mod.rs @@ -1,8 +1,6 @@ mod commands; -mod config; mod sorter; mod which; -pub use config::*; pub use sorter::*; pub use which::*; diff --git a/yazi-core/src/which/which.rs b/yazi-core/src/which/which.rs index fc8110efe..3b34d6f59 100644 --- a/yazi-core/src/which/which.rs +++ b/yazi-core/src/which/which.rs @@ -1,7 +1,7 @@ -use yazi_config::keymap::{ControlCow, Key}; +use yazi_config::{keymap::{ControlCow, Key}, which::SortBy}; use yazi_shared::{emit, render, Layer}; -use crate::which::Config; +use super::WhichSorter; #[derive(Default, Debug)] pub struct Which { @@ -9,21 +9,17 @@ pub struct Which { pub times: usize, pub cands: Vec, - pub conf: Config, + // Sorting + sort_by: SortBy, + sort_sensitive: bool, + sort_reverse: bool, + // Visibility pub visible: bool, pub silent: bool, } impl Which { - fn reset(&mut self) { - self.times = 0; - self.cands.clear(); - - self.visible = false; - self.silent = false; - } - pub fn type_(&mut self, key: Key) -> bool { self.cands.retain(|c| c.on.len() > self.times && c.on[self.times] == key); self.times += 1; @@ -41,4 +37,20 @@ impl Which { render!(); true } + + pub(super) fn sorter(&self) -> WhichSorter { + WhichSorter { + by: self.sort_by, + sensitive: self.sort_sensitive, + reverse: self.sort_reverse, + } + } + + fn reset(&mut self) { + self.times = 0; + self.cands.clear(); + + self.visible = false; + self.silent = false; + } } From 8a9b7279f857969354e3f8bbccefbed876662acb Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 12 Feb 2024 19:25:44 +0800 Subject: [PATCH 4/5] Avoid unnecessary allocations by caching the sorting factors --- yazi-config/src/keymap/control.rs | 6 ++++- yazi-config/src/which/which.rs | 7 ++--- yazi-core/src/folder/sorter.rs | 7 +++-- yazi-core/src/which/sorter.rs | 43 +++++++++++++++++-------------- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/yazi-config/src/keymap/control.rs b/yazi-config/src/keymap/control.rs index 23ae3cf7e..a41b310a6 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 96caccc81..b8e3305cb 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 4dea60833..850e02490 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 0fd92f16e..d9b196318 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 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::(); - 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; } } From f2976400f646b5210efd0353784d34fc05262c6b Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 12 Feb 2024 21:58:23 +0800 Subject: [PATCH 5/5] Fix sorting not working and apply the suggested changes --- yazi-core/src/folder/sorter.rs | 6 +----- yazi-core/src/which/commands/show.rs | 4 ++-- yazi-core/src/which/sorter.rs | 20 +++++++++++++------- yazi-core/src/which/which.rs | 19 ++----------------- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/yazi-core/src/folder/sorter.rs b/yazi-core/src/folder/sorter.rs index 850e02490..779b22e29 100644 --- a/yazi-core/src/folder/sorter.rs +++ b/yazi-core/src/folder/sorter.rs @@ -80,11 +80,7 @@ impl FilesSorter { if self.reverse { ordering.reverse() } else { ordering } }); - let mut new = Vec::with_capacity(indices.len()); - for i in indices { - new.push(mem::take(&mut items[i])); - } - *items = new; + *items = indices.into_iter().map(|i| mem::take(&mut items[i])).collect(); } #[inline(always)] diff --git a/yazi-core/src/which/commands/show.rs b/yazi-core/src/which/commands/show.rs index 4794e6ecd..22994c6d7 100644 --- a/yazi-core/src/which/commands/show.rs +++ b/yazi-core/src/which/commands/show.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use yazi_config::{keymap::{Control, Key}, KEYMAP}; use yazi_shared::{event::Cmd, render, Layer}; -use crate::which::Which; +use crate::which::{Which, WhichSorter}; pub struct Opt { cands: Vec, @@ -52,7 +52,7 @@ impl Which { .map(|c| c.into()) .collect(); - self.sorter().sort(&mut self.cands); + WhichSorter::default().sort(&mut self.cands); self.visible = true; self.silent = false; render!(); diff --git a/yazi-core/src/which/sorter.rs b/yazi-core/src/which/sorter.rs index d9b196318..6fd64dc28 100644 --- a/yazi-core/src/which/sorter.rs +++ b/yazi-core/src/which/sorter.rs @@ -1,15 +1,25 @@ use std::{borrow::Cow, mem}; -use yazi_config::{keymap::ControlCow, which::SortBy}; +use yazi_config::{keymap::ControlCow, which::SortBy, WHICH}; use yazi_shared::natsort; -#[derive(Clone, Copy, Default, PartialEq)] +#[derive(Clone, Copy, PartialEq)] pub struct WhichSorter { pub by: SortBy, pub sensitive: bool, pub reverse: bool, } +impl Default for WhichSorter { + fn default() -> Self { + Self { + by: WHICH.sort_by, + sensitive: WHICH.sort_sensitive, + reverse: WHICH.sort_reverse, + } + } +} + impl WhichSorter { pub(super) fn sort(&self, items: &mut Vec) { if self.by == SortBy::None || items.is_empty() { @@ -32,10 +42,6 @@ impl WhichSorter { if self.reverse { ordering.reverse() } else { ordering } }); - let mut new = Vec::with_capacity(indices.len()); - for i in indices { - new.push(mem::take(&mut items[i])); - } - *items = new; + *items = indices.into_iter().map(|i| mem::take(&mut items[i])).collect(); } } diff --git a/yazi-core/src/which/which.rs b/yazi-core/src/which/which.rs index 3b34d6f59..d6227775c 100644 --- a/yazi-core/src/which/which.rs +++ b/yazi-core/src/which/which.rs @@ -1,19 +1,12 @@ -use yazi_config::{keymap::{ControlCow, Key}, which::SortBy}; +use yazi_config::keymap::{ControlCow, Key}; use yazi_shared::{emit, render, Layer}; -use super::WhichSorter; - -#[derive(Default, Debug)] +#[derive(Default)] pub struct Which { pub(super) layer: Layer, pub times: usize, pub cands: Vec, - // Sorting - sort_by: SortBy, - sort_sensitive: bool, - sort_reverse: bool, - // Visibility pub visible: bool, pub silent: bool, @@ -38,14 +31,6 @@ impl Which { true } - pub(super) fn sorter(&self) -> WhichSorter { - WhichSorter { - by: self.sort_by, - sensitive: self.sort_sensitive, - reverse: self.sort_reverse, - } - } - fn reset(&mut self) { self.times = 0; self.cands.clear();