Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: help menu multi-line text rendering #1347

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions yazi-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand Down
20 changes: 12 additions & 8 deletions yazi-config/src/keymap/control.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/help/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down