From 914a29649249a736c86d9a27ffa298a17bebbe8a Mon Sep 17 00:00:00 2001 From: Zhang ShengYan Date: Tue, 24 Sep 2024 15:59:38 +0800 Subject: [PATCH] feat: apply Plurality --- yazi-config/src/keymap/chord.rs | 30 ++++++++++++++++++++++++++---- yazi-config/src/lib.rs | 3 ++- yazi-core/src/which/sorter.rs | 4 ++-- yazi-fm/src/help/bindings.rs | 4 ++-- yazi-fm/src/which/cand.rs | 4 ++-- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/yazi-config/src/keymap/chord.rs b/yazi-config/src/keymap/chord.rs index 7cfa995d..ee6cf98d 100644 --- a/yazi-config/src/keymap/chord.rs +++ b/yazi-config/src/keymap/chord.rs @@ -7,6 +7,14 @@ use yazi_shared::event::Cmd; use super::Key; static RE: OnceLock = OnceLock::new(); +static PLURAL_RE: OnceLock = OnceLock::new(); + +#[derive(Debug, Clone, Copy, Default)] +pub enum Plurality { + Singular, + #[default] + Plural, +} #[derive(Debug, Default, Deserialize)] pub struct Chord { @@ -36,16 +44,30 @@ impl Chord { .into_owned() } - pub fn desc(&self) -> Option> { - self.desc.as_ref().map(|s| RE.get_or_init(|| Regex::new(r"\s+").unwrap()).replace_all(s, " ")) + pub fn desc(&self, p: Plurality) -> Option> { + self.desc.as_ref().map(|s| { + let pluar_re = PLURAL_RE.get_or_init(|| Regex::new(r"\{([^{}]+)\}").unwrap()); + let result = pluar_re.replace_all(s, |caps: ®ex::Captures| { + let parts: Vec<&str> = caps[1].split('|').collect(); + match p { + Plurality::Singular => parts.get(1).unwrap_or(&"").to_string(), + Plurality::Plural => parts[0].to_string(), + } + }); + let whitespace_re = RE.get_or_init(|| Regex::new(r"\s+").unwrap()); + match result { + Cow::Owned(result) => Cow::Owned(whitespace_re.replace_all(&result, "").into_owned()), + Cow::Borrowed(result) => whitespace_re.replace_all(result, " "), + } + }) } - pub fn desc_or_run(&self) -> Cow { self.desc().unwrap_or_else(|| self.run().into()) } + pub fn desc_or_run(&self, p: Plurality) -> Cow { self.desc(p).unwrap_or_else(|| self.run().into()) } #[inline] pub fn contains(&self, s: &str) -> bool { let s = s.to_lowercase(); - self.desc().map(|d| d.to_lowercase().contains(&s)) == Some(true) + self.desc(Plurality::default()).map(|d| d.to_lowercase().contains(&s)) == Some(true) || self.run().to_lowercase().contains(&s) || self.on().to_lowercase().contains(&s) } diff --git a/yazi-config/src/lib.rs b/yazi-config/src/lib.rs index 910f9eed..cdf8968c 100644 --- a/yazi-config/src/lib.rs +++ b/yazi-config/src/lib.rs @@ -2,6 +2,7 @@ use std::str::FromStr; +use keymap::Plurality; use yazi_shared::{RoCell, Xdg}; pub mod keymap; @@ -68,7 +69,7 @@ pub fn init() -> anyhow::Result<()> { continue; } if !r.bool("confirm") && !r.bool("interactive") { - let s = format!("`{}` ({})", c.on(), c.desc_or_run()); + let s = format!("`{}` ({})", c.on(), c.desc_or_run(Plurality::default())); eprintln!( r#"WARNING: In Yazi v0.3, the behavior of the interactive `shell` (i.e., shell templates) must be explicitly specified with either `--interactive` or `--confirm`. diff --git a/yazi-core/src/which/sorter.rs b/yazi-core/src/which/sorter.rs index d6804872..61051235 100644 --- a/yazi-core/src/which/sorter.rs +++ b/yazi-core/src/which/sorter.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, mem}; -use yazi_config::{WHICH, keymap::ChordCow, which::SortBy}; +use yazi_config::{keymap::{ChordCow, Plurality}, which::SortBy, WHICH}; use yazi_shared::{Transliterator, natsort}; #[derive(Clone, Copy, PartialEq)] @@ -35,7 +35,7 @@ impl WhichSorter { entities.push(match self.by { SortBy::None => unreachable!(), SortBy::Key => Cow::Owned(ctrl.on()), - SortBy::Desc => ctrl.desc_or_run(), + SortBy::Desc => ctrl.desc_or_run(Plurality::Plural), }); } diff --git a/yazi-fm/src/help/bindings.rs b/yazi-fm/src/help/bindings.rs index 99b61e49..11eee947 100644 --- a/yazi-fm/src/help/bindings.rs +++ b/yazi-fm/src/help/bindings.rs @@ -1,5 +1,5 @@ use ratatui::{buffer::Buffer, layout::{self, Constraint, Rect}, widgets::{List, ListItem, Widget}}; -use yazi_config::THEME; +use yazi_config::{keymap::Plurality, THEME}; use crate::Ctx; @@ -29,7 +29,7 @@ impl Widget for Bindings<'_> { // Desc let col3: Vec<_> = bindings .iter() - .map(|c| ListItem::new(c.desc().unwrap_or("-".into())).style(THEME.help.desc)) + .map(|c| ListItem::new(c.desc(Plurality::default()).unwrap_or("-".into())).style(THEME.help.desc)) .collect(); let chunks = layout::Layout::horizontal([ diff --git a/yazi-fm/src/which/cand.rs b/yazi-fm/src/which/cand.rs index c47fa088..83657702 100644 --- a/yazi-fm/src/which/cand.rs +++ b/yazi-fm/src/which/cand.rs @@ -1,5 +1,5 @@ use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Span}, widgets::Widget}; -use yazi_config::{THEME, keymap::Chord}; +use yazi_config::{keymap::{Chord, Plurality}, THEME}; pub(super) struct Cand<'a> { cand: &'a Chord, @@ -32,7 +32,7 @@ impl Widget for Cand<'_> { spans.push(Span::styled(&THEME.which.separator, THEME.which.separator_style)); // Description - spans.push(Span::styled(self.cand.desc_or_run(), THEME.which.desc)); + spans.push(Span::styled(self.cand.desc_or_run(Plurality::Plural), THEME.which.desc)); Line::from(spans).render(area, buf); }