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

feat: add filtering tip in help component #1361

Merged
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 scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cargo publish -p yazi-shared
cargo publish -p yazi-config
cargo publish -p yazi-proxy
cargo publish -p yazi-fs
cargo publish -p yazi-adapter
cargo publish -p yazi-boot
cargo publish -p yazi-dds
Expand Down
2 changes: 1 addition & 1 deletion yazi-config/preset/keymap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -315,5 +315,5 @@ keymap = [
{ on = "<S-Down>", run = "arrow 5", desc = "Move cursor down 5 lines" },

# Filtering
{ on = "/", run = "filter", desc = "Apply a filter for the help items" },
{ on = "f", run = "filter", desc = "Apply a filter for the help items" },
]
3 changes: 2 additions & 1 deletion yazi-core/src/help/commands/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::help::Help;

impl Help {
pub fn escape(&mut self, _: Cmd) {
if self.in_filter.is_none() {
if self.keyword().is_none() {
return self.toggle(self.layer);
}

self.keyword = String::new();
self.in_filter = None;
self.filter_apply();
render!();
Expand Down
22 changes: 10 additions & 12 deletions yazi-core/src/help/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Help {
pub(super) bindings: Vec<&'static Control>,

// Filter
keyword: Option<String>,
pub(super) keyword: String,
pub(super) in_filter: Option<Input>,

pub(super) offset: usize,
Expand All @@ -29,7 +29,7 @@ impl Help {
self.visible = !self.visible;
self.layer = layer;

self.keyword = Some(String::new());
self.keyword = String::new();
self.in_filter = None;
self.filter_apply();

Expand Down Expand Up @@ -65,18 +65,16 @@ impl Help {
}

pub(super) fn filter_apply(&mut self) {
let kw = self.in_filter.as_ref().map(|i| i.value()).filter(|v| !v.is_empty());
if self.keyword.as_deref() == kw {
return;
}
let kw = self.in_filter.as_ref().map_or("", |i| i.value());

if let Some(kw) = kw {
self.bindings = KEYMAP.get(self.layer).iter().filter(|&c| c.contains(kw)).collect();
} else {
if kw.is_empty() {
self.keyword = String::new();
self.bindings = KEYMAP.get(self.layer).iter().collect();
} else if self.keyword != kw {
self.keyword = kw.to_owned();
self.bindings = KEYMAP.get(self.layer).iter().filter(|&c| c.contains(kw)).collect();
}

self.keyword = kw.map(|s| s.to_owned());
self.arrow(0);
}
}
Expand All @@ -89,8 +87,8 @@ impl Help {
.in_filter
.as_ref()
.map(|i| i.value())
.or(self.keyword.as_deref())
.map(|s| format!("/{}", s))
.or(Some(self.keyword.as_str()).filter(|&s| !s.is_empty()))
.map(|s| format!("Filter: {}", s))
}

// --- Bindings
Expand Down
11 changes: 9 additions & 2 deletions yazi-fm/src/help/layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ratatui::{buffer::Buffer, layout::{self, Constraint, Rect}, text::Line, widgets::Widget};
use yazi_config::THEME;
use yazi_config::{KEYMAP, THEME};

use super::Bindings;
use crate::Ctx;
Expand All @@ -10,6 +10,13 @@ pub(crate) struct Layout<'a> {

impl<'a> Layout<'a> {
pub fn new(cx: &'a Ctx) -> Self { Self { cx } }

fn tips() -> String {
match KEYMAP.help.iter().find(|&c| c.run.iter().any(|c| c.name == "filter")) {
Some(c) => format!(" (Press `{}` to filter)", c.on()),
None => String::new(),
}
}
}

impl<'a> Widget for Layout<'a> {
Expand All @@ -19,7 +26,7 @@ impl<'a> Widget for Layout<'a> {

let chunks = layout::Layout::vertical([Constraint::Fill(1), Constraint::Length(1)]).split(area);
Line::styled(
help.keyword().unwrap_or_else(|| format!("{}.help", help.layer)),
help.keyword().unwrap_or_else(|| format!("{}.help{}", help.layer, Self::tips())),
THEME.help.footer,
)
.render(chunks[1], buf);
Expand Down