This commit is contained in:
sxyazi 2024-07-25 07:53:01 +08:00
parent e5cc2d3682
commit 9ff25120a8
No known key found for this signature in database

View file

@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::VecDeque, sync::LazyLock};
use std::{borrow::Cow, collections::VecDeque, sync::OnceLock};
use regex::Regex;
use serde::Deserialize;
@ -6,7 +6,7 @@ use yazi_shared::event::Cmd;
use super::Key;
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
static RE: OnceLock<Regex> = OnceLock::new();
#[derive(Debug, Default, Deserialize)]
pub struct Control {
@ -26,11 +26,14 @@ impl Control {
pub fn on(&self) -> String { self.on.iter().map(ToString::to_string).collect() }
pub fn run(&self) -> String {
RE.replace_all(&self.run.iter().map(|c| c.to_string()).collect::<Vec<_>>().join("; "), " ")
RE.get_or_init(|| Regex::new(r"\s+").unwrap())
.replace_all(&self.run.iter().map(|c| c.to_string()).collect::<Vec<_>>().join("; "), " ")
.into_owned()
}
pub fn desc(&self) -> Option<Cow<str>> { self.desc.as_ref().map(|s| RE.replace_all(s, " ")) }
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_or_run(&self) -> Cow<str> { self.desc().unwrap_or_else(|| self.run().into()) }