fix: help menu multi-line text rendering

This commit is contained in:
sxyazi 2024-07-25 07:48:56 +08:00
parent af0fb5955b
commit e5cc2d3682
No known key found for this signature in database
4 changed files with 13 additions and 10 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::LazyLock};
use regex::Regex;
use serde::Deserialize;
use yazi_shared::event::Cmd;
use super::Key;
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
#[derive(Debug, Default, Deserialize)]
pub struct Control {
#[serde(deserialize_with = "super::deserialize_on")]
@ -20,23 +23,21 @@ 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.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.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([