feat: Enable the ability to sort the which key

formatted properly
This commit is contained in:
coolabhays 2024-02-11 20:36:03 +05:30
parent f6b4c923b4
commit 79f63a9ae2
7 changed files with 64 additions and 73 deletions

View file

@ -17,8 +17,8 @@ mod priority;
mod tasks; mod tasks;
pub mod theme; pub mod theme;
mod validation; mod validation;
mod xdg;
pub mod which; pub mod which;
mod xdg;
pub use layout::*; pub use layout::*;
pub(crate) use pattern::*; pub(crate) use pattern::*;
@ -65,5 +65,5 @@ pub fn init() {
THEME.with(Default::default); THEME.with(Default::default);
INPUT.with(Default::default); INPUT.with(Default::default);
SELECT.with(Default::default); SELECT.with(Default::default);
WHICH.with(Default::default); WHICH.with(Default::default);
} }

View file

@ -8,37 +8,36 @@ use serde::{Deserialize, Serialize};
pub enum SortBy { pub enum SortBy {
#[default] #[default]
None, None,
Key, Key,
Desc, Desc,
} }
impl FromStr for SortBy { impl FromStr for SortBy {
type Err = anyhow::Error; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s { Ok(match s {
"none" => Self::None, "none" => Self::None,
"key" => Self::Key, "key" => Self::Key,
"desc" => Self::Desc, "desc" => Self::Desc,
_ => bail!("Invalid sort option: {s}") _ => bail!("Invalid sort option: {s}"),
}) })
} }
} }
impl TryFrom<String> for SortBy { impl TryFrom<String> for SortBy {
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(value: String) -> Result<Self, Self::Error> { fn try_from(value: String) -> Result<Self, Self::Error> { Self::from_str(&value) }
Self::from_str(&value)
}
} }
impl ToString for SortBy { impl ToString for SortBy {
fn to_string(&self) -> String { fn to_string(&self) -> String {
match self { match self {
Self::None => "none", Self::None => "none",
Self::Key => "key", Self::Key => "key",
Self::Desc => "desc", Self::Desc => "desc",
}.to_string() }
} .to_string()
}
} }

View file

@ -42,21 +42,21 @@ impl Which {
render!(); render!();
} }
pub fn show_with(&mut self, key: &Key, layer: Layer) { pub fn show_with(&mut self, key: &Key, layer: Layer) {
self.layer = layer; self.layer = layer;
self.times = 1; self.times = 1;
self.cands = KEYMAP self.cands = KEYMAP
.get(layer) .get(layer)
.iter() .iter()
.filter(|c| c.on.len() > 1 && &c.on[0] == key) .filter(|c| c.on.len() > 1 && &c.on[0] == key)
.map(|c| c.into()) .map(|c| c.into())
.collect(); .collect();
// sort "which" // sort "which"
self.conf.sorter().sort(&mut self.cands); self.conf.sorter().sort(&mut self.cands);
self.visible = true; self.visible = true;
self.silent = false; self.silent = false;
render!(); render!();
} }
} }

View file

@ -22,20 +22,13 @@ impl Default for Config {
} }
impl Config { impl Config {
#[allow(unused)]
pub(super) fn patch<F: FnOnce(&mut Self)>(&mut self, f: F) -> bool {
let old = self.clone();
f(self);
*self != old
}
#[inline] #[inline]
pub(super) fn sorter(&self) -> WhichSorter { pub(super) fn sorter(&self) -> WhichSorter {
WhichSorter { WhichSorter {
by: self.sort_by, by: self.sort_by,
sensitive: self.sort_sensitive, sensitive: self.sort_sensitive,
reverse: self.sort_reverse, reverse: self.sort_reverse,
} }
} }
} }

View file

@ -1,8 +1,8 @@
mod commands; mod commands;
mod which;
mod config; mod config;
mod sorter; mod sorter;
mod which;
pub use which::*;
pub use config::*; pub use config::*;
pub use sorter::*; pub use sorter::*;
pub use which::*;

View file

@ -1,4 +1,4 @@
use yazi_config::{which::SortBy, keymap::ControlCow}; use yazi_config::{keymap::ControlCow, which::SortBy};
use yazi_shared::natsort; use yazi_shared::natsort;
#[derive(Clone, Copy, Default, PartialEq)] #[derive(Clone, Copy, Default, PartialEq)]
@ -14,26 +14,25 @@ impl WhichSorter {
return false; return false;
} }
let by_alphabetical = |a: &str, b: &str| { let by_alphabetical = |a: &str, b: &str| {
let ordering = natsort(a.as_bytes(), b.as_bytes(), !self.sensitive); let ordering = natsort(a.as_bytes(), b.as_bytes(), !self.sensitive);
if self.reverse { ordering.reverse() } else { ordering } if self.reverse { ordering.reverse() } else { ordering }
}; };
match self.by { match self.by {
SortBy::None => return false, SortBy::None => return false,
SortBy::Key => items.sort_unstable_by(|a, b| { SortBy::Key => items.sort_unstable_by(|a, b| {
let a = a.on.iter().map(|c| c.to_string()).collect::<String>(); let a = a.on.iter().map(|c| c.to_string()).collect::<String>();
let b = b.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) by_alphabetical(&a, &b)
}), }),
SortBy::Desc => items.sort_unstable_by(|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) // 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 ? // in case if it is not present, should I just panic ?
by_alphabetical(a.desc.as_ref().unwrap(), b.desc.as_ref().unwrap()) by_alphabetical(a.desc.as_ref().unwrap(), b.desc.as_ref().unwrap())
}) }),
} }
true true
} }
} }

View file

@ -9,7 +9,7 @@ pub struct Which {
pub times: usize, pub times: usize,
pub cands: Vec<ControlCow>, pub cands: Vec<ControlCow>,
pub conf: Config, pub conf: Config,
pub visible: bool, pub visible: bool,
pub silent: bool, pub silent: bool,