fix: help menu multi-line text rendering (#1347)

This commit is contained in:
三咲雅 · Misaki Masa 2024-07-25 07:54:24 +08:00 committed by GitHub
parent af0fb5955b
commit 152b9ea75e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 9 deletions

1
Cargo.lock generated
View file

@ -2885,6 +2885,7 @@ dependencies = [
"globset",
"indexmap",
"ratatui",
"regex",
"serde",
"toml",
"validator",

View file

@ -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" ] }

View file

@ -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<Regex> = 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::<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()
}
#[inline]
pub fn desc_or_run(&self) -> Cow<str> {
if let Some(ref s) = self.desc { Cow::Borrowed(s) } else { self.run().into() }
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()) }
#[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)
}

View file

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