diff --git a/Cargo.lock b/Cargo.lock index 32818939..87dd0ac9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2885,6 +2885,7 @@ dependencies = [ "globset", "indexmap", "ratatui", + "regex", "serde", "toml", "validator", diff --git a/yazi-config/Cargo.toml b/yazi-config/Cargo.toml index 7800ed27..fe08e566 100644 --- a/yazi-config/Cargo.toml +++ b/yazi-config/Cargo.toml @@ -19,6 +19,7 @@ crossterm = "0.27.0" globset = "0.4.14" indexmap = "2.2.6" ratatui = "0.27.0" +regex = "1.10.5" serde = { version = "1.0.204", features = [ "derive" ] } toml = { version = "0.8.15", features = [ "preserve_order" ] } validator = { version = "0.18.1", features = [ "derive" ] } diff --git a/yazi-config/src/keymap/control.rs b/yazi-config/src/keymap/control.rs index 85c6e906..456cc7fa 100644 --- a/yazi-config/src/keymap/control.rs +++ b/yazi-config/src/keymap/control.rs @@ -1,10 +1,13 @@ -use std::{borrow::Cow, collections::VecDeque}; +use std::{borrow::Cow, collections::VecDeque, sync::OnceLock}; +use regex::Regex; use serde::Deserialize; use yazi_shared::event::Cmd; use super::Key; +static RE: OnceLock = OnceLock::new(); + #[derive(Debug, Default, Deserialize)] pub struct Control { #[serde(deserialize_with = "super::deserialize_on")] @@ -20,23 +23,24 @@ impl Control { } impl Control { - #[inline] pub fn on(&self) -> String { self.on.iter().map(ToString::to_string).collect() } - #[inline] pub fn run(&self) -> String { - self.run.iter().map(|c| c.to_string()).collect::>().join("; ") + RE.get_or_init(|| Regex::new(r"\s+").unwrap()) + .replace_all(&self.run.iter().map(|c| c.to_string()).collect::>().join("; "), " ") + .into_owned() } - #[inline] - pub fn desc_or_run(&self) -> Cow { - if let Some(ref s) = self.desc { Cow::Borrowed(s) } else { self.run().into() } + 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_or_run(&self) -> Cow { self.desc().unwrap_or_else(|| self.run().into()) } + #[inline] pub fn contains(&self, s: &str) -> bool { let s = s.to_lowercase(); - self.desc.as_ref().map(|d| d.to_lowercase().contains(&s)) == Some(true) + self.desc().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-fm/src/help/bindings.rs b/yazi-fm/src/help/bindings.rs index 8e9ebec2..99b61e49 100644 --- a/yazi-fm/src/help/bindings.rs +++ b/yazi-fm/src/help/bindings.rs @@ -29,7 +29,7 @@ impl Widget for Bindings<'_> { // Desc let col3: Vec<_> = bindings .iter() - .map(|c| ListItem::new(c.desc.as_deref().unwrap_or("-")).style(THEME.help.desc)) + .map(|c| ListItem::new(c.desc().unwrap_or("-".into())).style(THEME.help.desc)) .collect(); let chunks = layout::Layout::horizontal([