From 59ea72a0da99d6fcea0dd50328bec730158738d2 Mon Sep 17 00:00:00 2001 From: Zhang ShengYan Date: Thu, 26 Sep 2024 19:29:30 +0800 Subject: [PATCH] feat: apply logic to check num of selected files --- yazi-config/src/keymap/chord.rs | 2 +- yazi-core/src/tab/commands/copy.rs | 4 ---- yazi-core/src/which/sorter.rs | 2 +- yazi-fm/src/router.rs | 8 ++++++++ yazi-fm/src/which/cand.rs | 5 +++-- yazi-fm/src/which/layout.rs | 5 ++--- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/yazi-config/src/keymap/chord.rs b/yazi-config/src/keymap/chord.rs index ee6cf98d..6c0eedff 100644 --- a/yazi-config/src/keymap/chord.rs +++ b/yazi-config/src/keymap/chord.rs @@ -56,7 +56,7 @@ impl Chord { }); let whitespace_re = RE.get_or_init(|| Regex::new(r"\s+").unwrap()); match result { - Cow::Owned(result) => Cow::Owned(whitespace_re.replace_all(&result, "").into_owned()), + Cow::Owned(result) => Cow::Owned(whitespace_re.replace_all(&result, " ").into_owned()), Cow::Borrowed(result) => whitespace_re.replace_all(result, " "), } }) diff --git a/yazi-core/src/tab/commands/copy.rs b/yazi-core/src/tab/commands/copy.rs index 6c8e0bb4..95af5d90 100644 --- a/yazi-core/src/tab/commands/copy.rs +++ b/yazi-core/src/tab/commands/copy.rs @@ -16,10 +16,6 @@ impl From for Opt { impl Tab { pub fn copy(&mut self, opt: impl Into) { let opt = opt.into() as Opt; - if !self.try_escape_visual() { - return; - } - let mut s = OsString::new(); let mut it = self.selected_or_hovered(true).peekable(); while let Some(u) = it.next() { diff --git a/yazi-core/src/which/sorter.rs b/yazi-core/src/which/sorter.rs index 61051235..4e2b0c69 100644 --- a/yazi-core/src/which/sorter.rs +++ b/yazi-core/src/which/sorter.rs @@ -35,7 +35,7 @@ impl WhichSorter { entities.push(match self.by { SortBy::None => unreachable!(), SortBy::Key => Cow::Owned(ctrl.on()), - SortBy::Desc => ctrl.desc_or_run(Plurality::Plural), + SortBy::Desc => ctrl.desc_or_run(Plurality::default()), }); } diff --git a/yazi-fm/src/router.rs b/yazi-fm/src/router.rs index c6350a30..4cd6a432 100644 --- a/yazi-fm/src/router.rs +++ b/yazi-fm/src/router.rs @@ -1,3 +1,4 @@ +use crossterm::event::KeyCode; use yazi_config::{KEYMAP, keymap::{Chord, Key}}; use yazi_shared::{Layer, emit}; @@ -50,6 +51,13 @@ impl<'a> Router<'a> { } if on.len() > 1 { + //for `copy` hotkeys starting with 'c', we need to escape visual mode first so that the `Which` UI can check how many files + // are selected to decide using plural or singular on command names. + if key.code == KeyCode::Char('c') { + if !self.app.cx.manager.active_mut().try_escape_visual() { + return false; + } + } self.app.cx.which.show_with(key, layer); } else { emit!(Seq(ctrl.to_seq(), layer)); diff --git a/yazi-fm/src/which/cand.rs b/yazi-fm/src/which/cand.rs index 83657702..bf049a32 100644 --- a/yazi-fm/src/which/cand.rs +++ b/yazi-fm/src/which/cand.rs @@ -4,10 +4,11 @@ use yazi_config::{keymap::{Chord, Plurality}, THEME}; pub(super) struct Cand<'a> { cand: &'a Chord, times: usize, + selected: usize, } impl<'a> Cand<'a> { - pub(super) fn new(cand: &'a Chord, times: usize) -> Self { Self { times, cand } } + pub(super) fn new(cand: &'a Chord, times: usize, selected: usize) -> Self { Self { times, cand, selected } } fn keys(&self) -> Vec { self.cand.on[self.times..].iter().map(ToString::to_string).collect() @@ -32,7 +33,7 @@ impl Widget for Cand<'_> { spans.push(Span::styled(&THEME.which.separator, THEME.which.separator_style)); // Description - spans.push(Span::styled(self.cand.desc_or_run(Plurality::Plural), THEME.which.desc)); + spans.push(Span::styled(self.cand.desc_or_run(if self.selected > 1 { Plurality::Plural } else { Plurality::Singular }), THEME.which.desc)); Line::from(spans).render(area, buf); } diff --git a/yazi-fm/src/which/layout.rs b/yazi-fm/src/which/layout.rs index a284e1e8..d1a13f70 100644 --- a/yazi-fm/src/which/layout.rs +++ b/yazi-fm/src/which/layout.rs @@ -48,14 +48,13 @@ impl Widget for Which<'_> { yazi_plugin::elements::Clear::default().render(area, buf); Block::new().style(THEME.which.mask).render(area, buf); - + let selected = self.cx.manager.selected_or_hovered(false).count(); for y in 0..area.height { for (x, chunk) in chunks.iter().enumerate() { let Some(cand) = which.cands.get(y as usize * cols + x) else { break; }; - - Cand::new(cand, which.times).render(Rect { y: chunk.y + y + 1, height: 1, ..*chunk }, buf); + Cand::new(cand, which.times, selected).render(Rect { y: chunk.y + y + 1, height: 1, ..*chunk }, buf); } } }