mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: apply Plurality
This commit is contained in:
parent
d6ade9921f
commit
914a296492
5 changed files with 34 additions and 11 deletions
|
|
@ -7,6 +7,14 @@ use yazi_shared::event::Cmd;
|
||||||
use super::Key;
|
use super::Key;
|
||||||
|
|
||||||
static RE: OnceLock<Regex> = OnceLock::new();
|
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)]
|
#[derive(Debug, Default, Deserialize)]
|
||||||
pub struct Chord {
|
pub struct Chord {
|
||||||
|
|
@ -36,16 +44,30 @@ impl Chord {
|
||||||
.into_owned()
|
.into_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn desc(&self) -> Option<Cow<str>> {
|
pub fn desc(&self, p: Plurality) -> Option<Cow<str>> {
|
||||||
self.desc.as_ref().map(|s| RE.get_or_init(|| Regex::new(r"\s+").unwrap()).replace_all(s, " "))
|
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<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]
|
#[inline]
|
||||||
pub fn contains(&self, s: &str) -> bool {
|
pub fn contains(&self, s: &str) -> bool {
|
||||||
let s = s.to_lowercase();
|
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.run().to_lowercase().contains(&s)
|
||||||
|| self.on().to_lowercase().contains(&s)
|
|| self.on().to_lowercase().contains(&s)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use keymap::Plurality;
|
||||||
use yazi_shared::{RoCell, Xdg};
|
use yazi_shared::{RoCell, Xdg};
|
||||||
|
|
||||||
pub mod keymap;
|
pub mod keymap;
|
||||||
|
|
@ -68,7 +69,7 @@ pub fn init() -> anyhow::Result<()> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if !r.bool("confirm") && !r.bool("interactive") {
|
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!(
|
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`.
|
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`.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{borrow::Cow, mem};
|
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};
|
use yazi_shared::{Transliterator, natsort};
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
|
|
@ -35,7 +35,7 @@ impl WhichSorter {
|
||||||
entities.push(match self.by {
|
entities.push(match self.by {
|
||||||
SortBy::None => unreachable!(),
|
SortBy::None => unreachable!(),
|
||||||
SortBy::Key => Cow::Owned(ctrl.on()),
|
SortBy::Key => Cow::Owned(ctrl.on()),
|
||||||
SortBy::Desc => ctrl.desc_or_run(),
|
SortBy::Desc => ctrl.desc_or_run(Plurality::Plural),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use ratatui::{buffer::Buffer, layout::{self, Constraint, Rect}, widgets::{List, ListItem, Widget}};
|
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;
|
use crate::Ctx;
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ impl Widget for Bindings<'_> {
|
||||||
// Desc
|
// Desc
|
||||||
let col3: Vec<_> = bindings
|
let col3: Vec<_> = bindings
|
||||||
.iter()
|
.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();
|
.collect();
|
||||||
|
|
||||||
let chunks = layout::Layout::horizontal([
|
let chunks = layout::Layout::horizontal([
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Span}, widgets::Widget};
|
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> {
|
pub(super) struct Cand<'a> {
|
||||||
cand: &'a Chord,
|
cand: &'a Chord,
|
||||||
|
|
@ -32,7 +32,7 @@ impl Widget for Cand<'_> {
|
||||||
spans.push(Span::styled(&THEME.which.separator, THEME.which.separator_style));
|
spans.push(Span::styled(&THEME.which.separator, THEME.which.separator_style));
|
||||||
|
|
||||||
// Description
|
// 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);
|
Line::from(spans).render(area, buf);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue