feat: apply Plurality

This commit is contained in:
Zhang ShengYan 2024-09-24 15:59:38 +08:00
parent d6ade9921f
commit 914a296492
5 changed files with 34 additions and 11 deletions

View file

@ -7,6 +7,14 @@ use yazi_shared::event::Cmd;
use super::Key;
static RE: OnceLock<Regex> = OnceLock::new();
static PLURAL_RE: OnceLock<Regex> = 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<Cow<str>> {
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<Cow<str>> {
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: &regex::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<str> { self.desc().unwrap_or_else(|| self.run().into()) }
pub fn desc_or_run(&self, p: Plurality) -> Cow<str> { 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)
}

View file

@ -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`.

View file

@ -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),
});
}

View file

@ -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([

View file

@ -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);
}