From 953d567c63f36099cbbb301c3aeeccb1a102077a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Wed, 26 Feb 2025 03:01:17 +0800 Subject: [PATCH] feat: allow to specify layer for keymap commands (#2399) --- yazi-config/preset/keymap-default.toml | 2 +- yazi-config/preset/theme-dark.toml | 2 +- yazi-config/preset/theme-light.toml | 2 +- yazi-config/src/keymap/chord.rs | 22 +++++-- yazi-config/src/keymap/keymap.rs | 57 ++++++++++--------- yazi-config/src/theme/theme.rs | 28 ++++----- .../{completion/completion.rs => cmp/cmp.rs} | 4 +- .../src/{completion => cmp}/commands/arrow.rs | 4 +- .../src/{completion => cmp}/commands/close.rs | 4 +- .../src/{completion => cmp}/commands/mod.rs | 0 .../src/{completion => cmp}/commands/show.rs | 4 +- .../{completion => cmp}/commands/trigger.rs | 13 ++--- yazi-core/src/cmp/mod.rs | 3 + yazi-core/src/completion/mod.rs | 3 - yazi-core/src/input/commands/close.rs | 4 +- yazi-core/src/input/commands/escape.rs | 4 +- yazi-core/src/lib.rs | 2 +- yazi-core/src/notify/commands/push.rs | 4 +- yazi-core/src/notify/commands/tick.rs | 4 +- yazi-core/src/tab/commands/cd.rs | 4 +- yazi-core/src/tab/commands/filter.rs | 7 +-- yazi-core/src/tab/commands/find.rs | 7 +-- yazi-core/src/tasks/tasks.rs | 4 +- yazi-core/src/which/commands/show.rs | 11 +--- yazi-core/src/which/which.rs | 10 ++-- yazi-dds/src/payload.rs | 4 +- yazi-fm/src/app/app.rs | 16 +++--- .../{completion/completion.rs => cmp/cmp.rs} | 25 ++++---- yazi-fm/src/cmp/mod.rs | 1 + yazi-fm/src/completion/mod.rs | 1 - yazi-fm/src/context.rs | 42 +++++++------- yazi-fm/src/executor.rs | 14 ++--- yazi-fm/src/main.rs | 2 +- yazi-fm/src/root.rs | 6 +- yazi-fm/src/router.rs | 4 +- yazi-fs/src/op.rs | 9 +-- yazi-macro/src/event.rs | 8 +-- yazi-plugin/src/utils/call.rs | 6 +- yazi-plugin/src/utils/layer.rs | 10 ++-- yazi-plugin/src/utils/preview.rs | 6 +- yazi-plugin/src/utils/spot.rs | 6 +- yazi-proxy/src/app.rs | 12 ++-- yazi-proxy/src/cmp.rs | 16 ++++++ yazi-proxy/src/completion.rs | 16 ------ yazi-proxy/src/confirm.rs | 4 +- yazi-proxy/src/input.rs | 6 +- yazi-proxy/src/lib.rs | 2 +- yazi-proxy/src/mgr.rs | 27 ++++----- yazi-proxy/src/pick.rs | 4 +- yazi-proxy/src/tab.rs | 11 ++-- yazi-proxy/src/tasks.rs | 26 ++++----- yazi-shared/src/event/cmd.rs | 44 +++++++------- yazi-shared/src/event/event.rs | 6 +- yazi-shared/src/layer.rs | 4 +- 54 files changed, 263 insertions(+), 284 deletions(-) rename yazi-core/src/{completion/completion.rs => cmp/cmp.rs} (94%) rename yazi-core/src/{completion => cmp}/commands/arrow.rs (95%) rename yazi-core/src/{completion => cmp}/commands/close.rs (90%) rename yazi-core/src/{completion => cmp}/commands/mod.rs (100%) rename yazi-core/src/{completion => cmp}/commands/show.rs (97%) rename yazi-core/src/{completion => cmp}/commands/trigger.rs (92%) create mode 100644 yazi-core/src/cmp/mod.rs delete mode 100644 yazi-core/src/completion/mod.rs rename yazi-fm/src/{completion/completion.rs => cmp/cmp.rs} (69%) create mode 100644 yazi-fm/src/cmp/mod.rs delete mode 100644 yazi-fm/src/completion/mod.rs create mode 100644 yazi-proxy/src/cmp.rs delete mode 100644 yazi-proxy/src/completion.rs diff --git a/yazi-config/preset/keymap-default.toml b/yazi-config/preset/keymap-default.toml index 31f0cc97..8dcf4601 100644 --- a/yazi-config/preset/keymap-default.toml +++ b/yazi-config/preset/keymap-default.toml @@ -321,7 +321,7 @@ keymap = [ { on = "", run = "help", desc = "Open help" }, ] -[completion] +[cmp] keymap = [ { on = "", run = "close", desc = "Cancel completion" }, diff --git a/yazi-config/preset/theme-dark.toml b/yazi-config/preset/theme-dark.toml index eb23277e..01efac6c 100644 --- a/yazi-config/preset/theme-dark.toml +++ b/yazi-config/preset/theme-dark.toml @@ -173,7 +173,7 @@ selected = { reversed = true } # : Completion {{{ -[completion] +[cmp] border = { fg = "blue" } active = { reversed = true } inactive = {} diff --git a/yazi-config/preset/theme-light.toml b/yazi-config/preset/theme-light.toml index f202d5a6..dc34a636 100644 --- a/yazi-config/preset/theme-light.toml +++ b/yazi-config/preset/theme-light.toml @@ -173,7 +173,7 @@ selected = { reversed = true } # : Completion {{{ -[completion] +[cmp] border = { fg = "blue" } active = { reversed = true } inactive = {} diff --git a/yazi-config/src/keymap/chord.rs b/yazi-config/src/keymap/chord.rs index b3d6a0d2..045aa15f 100644 --- a/yazi-config/src/keymap/chord.rs +++ b/yazi-config/src/keymap/chord.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, hash::{Hash, Hasher}, sync::OnceLock}; use regex::Regex; use serde::Deserialize; -use yazi_shared::event::Cmd; +use yazi_shared::{Layer, event::Cmd}; use super::Key; @@ -42,15 +42,25 @@ impl Chord { pub fn desc_or_run(&self) -> Cow { self.desc().unwrap_or_else(|| self.run().into()) } - #[inline] - pub fn noop(&self) -> bool { - self.run.len() == 1 && self.run[0].name == "noop" && self.run[0].args.is_empty() - } - pub fn contains(&self, s: &str) -> bool { let s = s.to_lowercase(); self.desc().map(|d| d.to_lowercase().contains(&s)) == Some(true) || self.run().to_lowercase().contains(&s) || self.on().to_lowercase().contains(&s) } + + #[inline] + pub(super) fn noop(&self) -> bool { + self.run.len() == 1 && self.run[0].name == "noop" && self.run[0].args.is_empty() + } + + #[inline] + pub(super) fn with_layer(mut self, layer: Layer) -> Self { + for c in &mut self.run { + if c.layer == Default::default() { + c.layer = layer; + } + } + self + } } diff --git a/yazi-config/src/keymap/keymap.rs b/yazi-config/src/keymap/keymap.rs index 4fd94d91..d9803323 100644 --- a/yazi-config/src/keymap/keymap.rs +++ b/yazi-config/src/keymap/keymap.rs @@ -10,21 +10,21 @@ use crate::{Preset, keymap::Key}; #[derive(Debug)] pub struct Keymap { - pub mgr: Vec, - pub tasks: Vec, - pub spot: Vec, - pub pick: Vec, - pub input: Vec, - pub confirm: Vec, - pub help: Vec, - pub completion: Vec, + pub mgr: Vec, + pub tasks: Vec, + pub spot: Vec, + pub pick: Vec, + pub input: Vec, + pub confirm: Vec, + pub help: Vec, + pub cmp: Vec, } impl Keymap { #[inline] pub fn get(&self, layer: Layer) -> &[Chord] { match layer { - Layer::App => unreachable!(), + Layer::App => &[], Layer::Mgr => &self.mgr, Layer::Tasks => &self.tasks, Layer::Spot => &self.spot, @@ -32,8 +32,8 @@ impl Keymap { Layer::Input => &self.input, Layer::Confirm => &self.confirm, Layer::Help => &self.help, - Layer::Completion => &self.completion, - Layer::Which => unreachable!(), + Layer::Cmp => &self.cmp, + Layer::Which => &[], } } } @@ -54,14 +54,14 @@ impl<'de> Deserialize<'de> for Keymap { #[derive(Deserialize)] struct Shadow { #[serde(rename = "manager")] - mgr: Inner, // TODO: remove serde(rename) - tasks: Inner, - spot: Inner, - pick: Inner, - input: Inner, - confirm: Inner, - help: Inner, - completion: Inner, + mgr: Inner, // TODO: remove serde(rename) + tasks: Inner, + spot: Inner, + pick: Inner, + input: Inner, + confirm: Inner, + help: Inner, + cmp: Inner, } #[derive(Deserialize)] struct Inner { @@ -72,7 +72,7 @@ impl<'de> Deserialize<'de> for Keymap { append_keymap: IndexSet, } - fn mix(a: IndexSet, b: IndexSet, c: IndexSet) -> Vec { + fn mix(l: Layer, a: IndexSet, b: IndexSet, c: IndexSet) -> Vec { #[inline] fn on(Chord { on, .. }: &Chord) -> [Key; 2] { [on.first().copied().unwrap_or_default(), on.get(1).copied().unwrap_or_default()] @@ -87,27 +87,28 @@ impl<'de> Deserialize<'de> for Keymap { c.into_iter().filter(|v| !b_seen.contains(&on(v))), ) .filter(|chord| !chord.noop()) + .map(|chord| chord.with_layer(l)) .collect() } let shadow = Shadow::deserialize(deserializer)?; Ok(Self { #[rustfmt::skip] - mgr: mix(shadow.mgr.prepend_keymap, shadow.mgr.keymap, shadow.mgr.append_keymap), + mgr: mix(Layer::Mgr, shadow.mgr.prepend_keymap, shadow.mgr.keymap, shadow.mgr.append_keymap), #[rustfmt::skip] - tasks: mix(shadow.tasks.prepend_keymap, shadow.tasks.keymap, shadow.tasks.append_keymap), + tasks: mix(Layer::Tasks, shadow.tasks.prepend_keymap, shadow.tasks.keymap, shadow.tasks.append_keymap), #[rustfmt::skip] - spot: mix(shadow.spot.prepend_keymap, shadow.spot.keymap, shadow.spot.append_keymap), + spot: mix(Layer::Spot, shadow.spot.prepend_keymap, shadow.spot.keymap, shadow.spot.append_keymap), #[rustfmt::skip] - pick: mix(shadow.pick.prepend_keymap, shadow.pick.keymap, shadow.pick.append_keymap), + pick: mix(Layer::Pick, shadow.pick.prepend_keymap, shadow.pick.keymap, shadow.pick.append_keymap), #[rustfmt::skip] - input: mix(shadow.input.prepend_keymap, shadow.input.keymap, shadow.input.append_keymap), + input: mix(Layer::Input, shadow.input.prepend_keymap, shadow.input.keymap, shadow.input.append_keymap), #[rustfmt::skip] - confirm: mix(shadow.confirm.prepend_keymap, shadow.confirm.keymap, shadow.confirm.append_keymap), + confirm: mix(Layer::Confirm, shadow.confirm.prepend_keymap, shadow.confirm.keymap, shadow.confirm.append_keymap), #[rustfmt::skip] - help: mix(shadow.help.prepend_keymap, shadow.help.keymap, shadow.help.append_keymap), + help: mix(Layer::Help, shadow.help.prepend_keymap, shadow.help.keymap, shadow.help.append_keymap), #[rustfmt::skip] - completion: mix(shadow.completion.prepend_keymap, shadow.completion.keymap, shadow.completion.append_keymap), + cmp: mix(Layer::Cmp, shadow.cmp.prepend_keymap, shadow.cmp.keymap, shadow.cmp.append_keymap), }) } } diff --git a/yazi-config/src/theme/theme.rs b/yazi-config/src/theme/theme.rs index dd3806cb..517523f0 100644 --- a/yazi-config/src/theme/theme.rs +++ b/yazi-config/src/theme/theme.rs @@ -9,20 +9,20 @@ use super::{Filetype, Flavor, Icons}; #[derive(Deserialize, Serialize)] pub struct Theme { - pub flavor: Flavor, + pub flavor: Flavor, #[serde(rename = "manager")] - pub mgr: Mgr, // TODO: Remove `serde(rename)` - pub mode: Mode, - pub status: Status, - pub which: Which, - pub confirm: Confirm, - pub spot: Spot, - pub notify: Notify, - pub pick: Pick, - pub input: Input, - pub completion: Completion, - pub tasks: Tasks, - pub help: Help, + pub mgr: Mgr, // TODO: Remove `serde(rename)` + pub mode: Mode, + pub status: Status, + pub which: Which, + pub confirm: Confirm, + pub spot: Spot, + pub notify: Notify, + pub pick: Pick, + pub input: Input, + pub cmp: Cmp, + pub tasks: Tasks, + pub help: Help, // File-specific styles #[serde(rename = "filetype", deserialize_with = "Filetype::deserialize", skip_serializing)] @@ -178,7 +178,7 @@ pub struct Input { } #[derive(Deserialize, Serialize)] -pub struct Completion { +pub struct Cmp { pub border: Style, pub active: Style, pub inactive: Style, diff --git a/yazi-core/src/completion/completion.rs b/yazi-core/src/cmp/cmp.rs similarity index 94% rename from yazi-core/src/completion/completion.rs rename to yazi-core/src/cmp/cmp.rs index c283c960..7e799a8b 100644 --- a/yazi-core/src/completion/completion.rs +++ b/yazi-core/src/cmp/cmp.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, path::PathBuf}; #[derive(Default)] -pub struct Completion { +pub struct Cmp { pub(super) caches: HashMap>, pub(super) cands: Vec, pub(super) offset: usize, @@ -11,7 +11,7 @@ pub struct Completion { pub visible: bool, } -impl Completion { +impl Cmp { // --- Cands #[inline] pub fn window(&self) -> &[String] { diff --git a/yazi-core/src/completion/commands/arrow.rs b/yazi-core/src/cmp/commands/arrow.rs similarity index 95% rename from yazi-core/src/completion/commands/arrow.rs rename to yazi-core/src/cmp/commands/arrow.rs index da810ec7..43722558 100644 --- a/yazi-core/src/completion/commands/arrow.rs +++ b/yazi-core/src/cmp/commands/arrow.rs @@ -1,7 +1,7 @@ use yazi_macro::render; use yazi_shared::event::{CmdCow, Data}; -use crate::completion::Completion; +use crate::cmp::Cmp; struct Opt { step: isize, @@ -11,7 +11,7 @@ impl From for Opt { fn from(c: CmdCow) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } } } -impl Completion { +impl Cmp { #[yazi_codegen::command] pub fn arrow(&mut self, opt: Opt) { if opt.step > 0 { diff --git a/yazi-core/src/completion/commands/close.rs b/yazi-core/src/cmp/commands/close.rs similarity index 90% rename from yazi-core/src/completion/commands/close.rs rename to yazi-core/src/cmp/commands/close.rs index fb3f162d..81e80634 100644 --- a/yazi-core/src/completion/commands/close.rs +++ b/yazi-core/src/cmp/commands/close.rs @@ -2,7 +2,7 @@ use yazi_macro::render; use yazi_proxy::InputProxy; use yazi_shared::event::CmdCow; -use crate::completion::Completion; +use crate::cmp::Cmp; struct Opt { submit: bool, @@ -15,7 +15,7 @@ impl From for Opt { fn from(submit: bool) -> Self { Self { submit } } } -impl Completion { +impl Cmp { #[yazi_codegen::command] pub fn close(&mut self, opt: Opt) { if let Some(s) = self.selected().filter(|_| opt.submit) { diff --git a/yazi-core/src/completion/commands/mod.rs b/yazi-core/src/cmp/commands/mod.rs similarity index 100% rename from yazi-core/src/completion/commands/mod.rs rename to yazi-core/src/cmp/commands/mod.rs diff --git a/yazi-core/src/completion/commands/show.rs b/yazi-core/src/cmp/commands/show.rs similarity index 97% rename from yazi-core/src/completion/commands/show.rs rename to yazi-core/src/cmp/commands/show.rs index 8c3e4461..be328893 100644 --- a/yazi-core/src/completion/commands/show.rs +++ b/yazi-core/src/cmp/commands/show.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, mem, ops::ControlFlow, path::PathBuf}; use yazi_macro::render; use yazi_shared::event::{Cmd, CmdCow, Data}; -use crate::completion::Completion; +use crate::cmp::Cmp; const LIMIT: usize = 30; @@ -29,7 +29,7 @@ impl From for Opt { fn from(c: Cmd) -> Self { Self::from(CmdCow::from(c)) } } -impl Completion { +impl Cmp { #[yazi_codegen::command] pub fn show(&mut self, opt: Opt) { if self.ticket != opt.ticket { diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/cmp/commands/trigger.rs similarity index 92% rename from yazi-core/src/completion/commands/trigger.rs rename to yazi-core/src/cmp/commands/trigger.rs index 8a1f742f..49203df3 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/cmp/commands/trigger.rs @@ -3,9 +3,9 @@ use std::{borrow::Cow, mem, path::{MAIN_SEPARATOR_STR, PathBuf}}; use tokio::fs; use yazi_fs::{CWD, expand_path}; use yazi_macro::{emit, render}; -use yazi_shared::{Layer, event::{Cmd, CmdCow, Data}}; +use yazi_shared::event::{Cmd, CmdCow, Data}; -use crate::completion::Completion; +use crate::cmp::Cmp; struct Opt { word: Cow<'static, str>, @@ -21,7 +21,7 @@ impl From for Opt { } } -impl Completion { +impl Cmp { #[yazi_codegen::command] pub fn trigger(&mut self, opt: Opt) { if opt.ticket < self.ticket { @@ -55,12 +55,11 @@ impl Completion { if !cache.is_empty() { emit!(Call( - Cmd::new("show") + Cmd::new("cmp:show") .with_any("cache", cache) .with_any("cache-name", parent) .with("word", word) - .with("ticket", ticket), - Layer::Completion + .with("ticket", ticket) )); } @@ -95,7 +94,7 @@ mod tests { use super::*; fn compare(s: &str, parent: &str, child: &str) -> bool { - let (p, c) = Completion::split_path(s).unwrap(); + let (p, c) = Cmp::split_path(s).unwrap(); let p = p.strip_prefix(yazi_fs::CWD.load().as_ref()).unwrap_or(&p); p == Path::new(parent) && c == child } diff --git a/yazi-core/src/cmp/mod.rs b/yazi-core/src/cmp/mod.rs new file mode 100644 index 00000000..7a21d159 --- /dev/null +++ b/yazi-core/src/cmp/mod.rs @@ -0,0 +1,3 @@ +yazi_macro::mod_pub!(commands); + +yazi_macro::mod_flat!(cmp); diff --git a/yazi-core/src/completion/mod.rs b/yazi-core/src/completion/mod.rs deleted file mode 100644 index bcc82403..00000000 --- a/yazi-core/src/completion/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -yazi_macro::mod_pub!(commands); - -yazi_macro::mod_flat!(completion); diff --git a/yazi-core/src/input/commands/close.rs b/yazi-core/src/input/commands/close.rs index d8e0c45c..a3a5bc26 100644 --- a/yazi-core/src/input/commands/close.rs +++ b/yazi-core/src/input/commands/close.rs @@ -1,5 +1,5 @@ use yazi_macro::render; -use yazi_proxy::CompletionProxy; +use yazi_proxy::CmpProxy; use yazi_shared::{errors::InputError, event::CmdCow}; use crate::input::Input; @@ -19,7 +19,7 @@ impl Input { #[yazi_codegen::command] pub fn close(&mut self, opt: Opt) { if self.completion { - CompletionProxy::close(); + CmpProxy::close(); } if let Some(cb) = self.callback.take() { diff --git a/yazi-core/src/input/commands/escape.rs b/yazi-core/src/input/commands/escape.rs index 72bb062d..f376a807 100644 --- a/yazi-core/src/input/commands/escape.rs +++ b/yazi-core/src/input/commands/escape.rs @@ -1,5 +1,5 @@ use yazi_macro::render; -use yazi_proxy::CompletionProxy; +use yazi_proxy::CmpProxy; use yazi_shared::event::CmdCow; use crate::input::{Input, InputMode, op::InputOp}; @@ -29,7 +29,7 @@ impl Input { self.move_(-1); if self.completion { - CompletionProxy::close(); + CmpProxy::close(); } } InputMode::Replace => { diff --git a/yazi-core/src/lib.rs b/yazi-core/src/lib.rs index e9499104..e921f8be 100644 --- a/yazi-core/src/lib.rs +++ b/yazi-core/src/lib.rs @@ -6,7 +6,7 @@ clippy::unit_arg )] -yazi_macro::mod_pub!(completion confirm help input mgr notify pick spot tab tasks which); +yazi_macro::mod_pub!(cmp confirm help input mgr notify pick spot tab tasks which); pub fn init() { mgr::WATCHED.with(<_>::default); diff --git a/yazi-core/src/notify/commands/push.rs b/yazi-core/src/notify/commands/push.rs index b217b20f..73e535ff 100644 --- a/yazi-core/src/notify/commands/push.rs +++ b/yazi-core/src/notify/commands/push.rs @@ -1,7 +1,7 @@ use std::time::Instant; use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd}; +use yazi_shared::event::Cmd; use crate::notify::{Message, Notify}; @@ -14,7 +14,7 @@ impl Notify { if self.messages.iter().all(|m| m != &msg) { self.messages.push(msg); - emit!(Call(Cmd::args("update_notify", &[0]), Layer::App)); + emit!(Call(Cmd::args("app:update_notify", &[0]))); } } } diff --git a/yazi-core/src/notify/commands/tick.rs b/yazi-core/src/notify/commands/tick.rs index c7b19ae5..c9ba8021 100644 --- a/yazi-core/src/notify/commands/tick.rs +++ b/yazi-core/src/notify/commands/tick.rs @@ -2,7 +2,7 @@ use std::time::Duration; use ratatui::layout::Rect; use yazi_macro::emit; -use yazi_shared::{Layer, event::{Cmd, CmdCow, Data}}; +use yazi_shared::event::{Cmd, CmdCow, Data}; use crate::notify::Notify; @@ -69,7 +69,7 @@ impl Notify { self.tick_handle = Some(tokio::spawn(async move { tokio::time::sleep(interval).await; - emit!(Call(Cmd::args("update_notify", &[interval.as_secs_f64()]), Layer::App)); + emit!(Call(Cmd::args("app:update_notify", &[interval.as_secs_f64()]))); })); } } diff --git a/yazi-core/src/tab/commands/cd.rs b/yazi-core/src/tab/commands/cd.rs index 6268244c..ffb36330 100644 --- a/yazi-core/src/tab/commands/cd.rs +++ b/yazi-core/src/tab/commands/cd.rs @@ -6,7 +6,7 @@ use yazi_config::popup::InputCfg; use yazi_dds::Pubsub; use yazi_fs::expand_path; use yazi_macro::render; -use yazi_proxy::{CompletionProxy, InputProxy, MgrProxy, TabProxy}; +use yazi_proxy::{CmpProxy, InputProxy, MgrProxy, TabProxy}; use yazi_shared::{Debounce, errors::InputError, event::CmdCow, url::Url}; use crate::tab::Tab; @@ -98,7 +98,7 @@ impl Tab { } } Err(InputError::Completed(before, ticket)) => { - CompletionProxy::trigger(&before, ticket); + CmpProxy::trigger(&before, ticket); } _ => break, } diff --git a/yazi-core/src/tab/commands/filter.rs b/yazi-core/src/tab/commands/filter.rs index 3768a48c..e0b93078 100644 --- a/yazi-core/src/tab/commands/filter.rs +++ b/yazi-core/src/tab/commands/filter.rs @@ -6,7 +6,7 @@ use yazi_config::popup::InputCfg; use yazi_fs::FilterCase; use yazi_macro::emit; use yazi_proxy::InputProxy; -use yazi_shared::{Debounce, Layer, errors::InputError, event::{Cmd, CmdCow}}; +use yazi_shared::{Debounce, errors::InputError, event::{Cmd, CmdCow}}; use crate::tab::Tab; @@ -41,11 +41,10 @@ impl Tab { let (Ok(s) | Err(InputError::Typed(s))) = result else { continue }; emit!(Call( - Cmd::args("filter_do", &[s]) + Cmd::args("mgr:filter_do", &[s]) .with_bool("smart", opt.case == FilterCase::Smart) .with_bool("insensitive", opt.case == FilterCase::Insensitive) - .with_bool("done", done), - Layer::Mgr + .with_bool("done", done) )); } }); diff --git a/yazi-core/src/tab/commands/find.rs b/yazi-core/src/tab/commands/find.rs index b247699d..d301d554 100644 --- a/yazi-core/src/tab/commands/find.rs +++ b/yazi-core/src/tab/commands/find.rs @@ -6,7 +6,7 @@ use yazi_config::popup::InputCfg; use yazi_fs::FilterCase; use yazi_macro::emit; use yazi_proxy::InputProxy; -use yazi_shared::{Debounce, Layer, errors::InputError, event::{Cmd, CmdCow}}; +use yazi_shared::{Debounce, errors::InputError, event::{Cmd, CmdCow}}; use crate::tab::Tab; @@ -33,11 +33,10 @@ impl Tab { while let Some(Ok(s)) | Some(Err(InputError::Typed(s))) = rx.next().await { emit!(Call( - Cmd::args("find_do", &[s]) + Cmd::args("mgr:find_do", &[s]) .with_bool("previous", opt.prev) .with_bool("smart", opt.case == FilterCase::Smart) - .with_bool("insensitive", opt.case == FilterCase::Insensitive), - Layer::Mgr + .with_bool("insensitive", opt.case == FilterCase::Insensitive) )); } }); diff --git a/yazi-core/src/tasks/tasks.rs b/yazi-core/src/tasks/tasks.rs index ff3feecb..11a906d2 100644 --- a/yazi-core/src/tasks/tasks.rs +++ b/yazi-core/src/tasks/tasks.rs @@ -5,7 +5,7 @@ use tokio::{task::JoinHandle, time::sleep}; use yazi_adapter::Dimension; use yazi_macro::emit; use yazi_scheduler::{Ongoing, Scheduler, TaskSummary}; -use yazi_shared::{Layer, event::Cmd}; +use yazi_shared::event::Cmd; use super::{TASKS_BORDER, TASKS_PADDING, TASKS_PERCENT, TasksProgress}; @@ -32,7 +32,7 @@ impl Tasks { let new = TasksProgress::from(&*ongoing.lock()); if last != new { last = new; - emit!(Call(Cmd::new("update_progress").with_any("progress", new), Layer::App)); + emit!(Call(Cmd::new("app:update_progress").with_any("progress", new))); } } }); diff --git a/yazi-core/src/which/commands/show.rs b/yazi-core/src/which/commands/show.rs index d2860b66..5d45dd7b 100644 --- a/yazi-core/src/which/commands/show.rs +++ b/yazi-core/src/which/commands/show.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use yazi_config::{KEYMAP, keymap::{Chord, Key}}; use yazi_macro::render; use yazi_shared::{Layer, event::CmdCow}; @@ -8,7 +6,6 @@ use crate::which::{Which, WhichSorter}; pub struct Opt { cands: Vec, - layer: Layer, silent: bool, } @@ -16,11 +13,7 @@ impl TryFrom for Opt { type Error = anyhow::Error; fn try_from(mut c: CmdCow) -> Result { - Ok(Self { - cands: c.take_any("candidates").unwrap_or_default(), - layer: Layer::from_str(&c.take_str("layer").unwrap_or_default())?, - silent: c.bool("silent"), - }) + Ok(Self { cands: c.take_any("candidates").unwrap_or_default(), silent: c.bool("silent") }) } } @@ -34,7 +27,6 @@ impl Which { return; } - self.layer = opt.layer; self.times = 0; self.cands = opt.cands.into_iter().map(|c| c.into()).collect(); @@ -44,7 +36,6 @@ impl Which { } pub fn show_with(&mut self, key: Key, layer: Layer) { - self.layer = layer; self.times = 1; self.cands = KEYMAP .get(layer) diff --git a/yazi-core/src/which/which.rs b/yazi-core/src/which/which.rs index 32e1bd2f..73993625 100644 --- a/yazi-core/src/which/which.rs +++ b/yazi-core/src/which/which.rs @@ -1,12 +1,10 @@ use yazi_config::keymap::{ChordCow, Key}; use yazi_macro::{emit, render_and}; -use yazi_shared::Layer; #[derive(Default)] pub struct Which { - pub(super) layer: Layer, - pub times: usize, - pub cands: Vec, + pub times: usize, + pub cands: Vec, // Visibility pub visible: bool, @@ -21,10 +19,10 @@ impl Which { if self.cands.is_empty() { self.reset(); } else if self.cands.len() == 1 { - emit!(Seq(self.cands.remove(0).into_seq(), self.layer)); + emit!(Seq(self.cands.remove(0).into_seq())); self.reset(); } else if let Some(i) = self.cands.iter().position(|c| c.on.len() == self.times) { - emit!(Seq(self.cands.remove(i).into_seq(), self.layer)); + emit!(Seq(self.cands.remove(i).into_seq())); self.reset(); } diff --git a/yazi-dds/src/payload.rs b/yazi-dds/src/payload.rs index 29a5bf41..1175ac95 100644 --- a/yazi-dds/src/payload.rs +++ b/yazi-dds/src/payload.rs @@ -3,7 +3,7 @@ use std::{fmt::Display, io::Write, str::FromStr}; use anyhow::{Result, anyhow}; use yazi_boot::BOOT; use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd}; +use yazi_shared::event::Cmd; use crate::{ID, body::Body}; @@ -47,7 +47,7 @@ impl<'a> Payload<'a> { impl Payload<'static> { pub(super) fn emit(self) { self.try_flush(); - emit!(Call(Cmd::new("accept_payload").with_any("payload", self), Layer::App)); + emit!(Call(Cmd::new("app:accept_payload").with_any("payload", self))); } } diff --git a/yazi-fm/src/app/app.rs b/yazi-fm/src/app/app.rs index 2a8fb106..5d0253ea 100644 --- a/yazi-fm/src/app/app.rs +++ b/yazi-fm/src/app/app.rs @@ -5,7 +5,7 @@ use crossterm::event::KeyEvent; use yazi_config::keymap::Key; use yazi_core::input::InputMode; use yazi_macro::emit; -use yazi_shared::{Layer, event::{CmdCow, Event, NEED_RENDER}}; +use yazi_shared::event::{CmdCow, Event, NEED_RENDER}; use crate::{Ctx, Executor, Router, Signals, Term, lives::Lives}; @@ -53,8 +53,8 @@ impl App { #[inline] fn dispatch(&mut self, event: Event) -> Result<()> { match event { - Event::Call(cmd, layer) => self.dispatch_call(cmd, layer), - Event::Seq(cmds, layer) => self.dispatch_seq(cmds, layer), + Event::Call(cmd) => self.dispatch_call(cmd), + Event::Seq(cmds) => self.dispatch_seq(cmds), Event::Render => self.dispatch_render(), Event::Key(key) => self.dispatch_key(key), Event::Mouse(mouse) => self.mouse(mouse), @@ -66,17 +66,15 @@ impl App { } #[inline] - fn dispatch_call(&mut self, cmd: CmdCow, layer: Layer) { - Executor::new(self).execute(cmd, layer); - } + fn dispatch_call(&mut self, cmd: CmdCow) { Executor::new(self).execute(cmd); } #[inline] - fn dispatch_seq(&mut self, mut cmds: Vec, layer: Layer) { + fn dispatch_seq(&mut self, mut cmds: Vec) { if let Some(last) = cmds.pop() { - Executor::new(self).execute(last, layer); + Executor::new(self).execute(last); } if !cmds.is_empty() { - emit!(Seq(cmds, layer)); + emit!(Seq(cmds)); } } diff --git a/yazi-fm/src/completion/completion.rs b/yazi-fm/src/cmp/cmp.rs similarity index 69% rename from yazi-fm/src/completion/completion.rs rename to yazi-fm/src/cmp/cmp.rs index 5b1890d3..f8284591 100644 --- a/yazi-fm/src/completion/completion.rs +++ b/yazi-fm/src/cmp/cmp.rs @@ -6,34 +6,31 @@ use yazi_config::{THEME, popup::{Offset, Position}}; use crate::Ctx; -pub(crate) struct Completion<'a> { +pub(crate) struct Cmp<'a> { cx: &'a Ctx, } -impl<'a> Completion<'a> { +impl<'a> Cmp<'a> { pub(crate) fn new(cx: &'a Ctx) -> Self { Self { cx } } } -impl Widget for Completion<'_> { +impl Widget for Cmp<'_> { fn render(self, rect: Rect, buf: &mut Buffer) { let items: Vec<_> = self .cx - .completion + .cmp .window() .iter() .enumerate() .map(|(i, x)| { - let icon = if x.ends_with(MAIN_SEPARATOR) { - &THEME.completion.icon_folder - } else { - &THEME.completion.icon_file - }; + let icon = + if x.ends_with(MAIN_SEPARATOR) { &THEME.cmp.icon_folder } else { &THEME.cmp.icon_file }; let mut item = ListItem::new(format!(" {icon} {x}")); - if i == self.cx.completion.rel_cursor() { - item = item.style(THEME.completion.active); + if i == self.cx.cmp.rel_cursor() { + item = item.style(THEME.cmp.active); } else { - item = item.style(THEME.completion.inactive); + item = item.style(THEME.cmp.inactive); } item @@ -57,9 +54,7 @@ impl Widget for Completion<'_> { yazi_plugin::elements::Clear::default().render(area, buf); List::new(items) - .block( - Block::bordered().border_type(BorderType::Rounded).border_style(THEME.completion.border), - ) + .block(Block::bordered().border_type(BorderType::Rounded).border_style(THEME.cmp.border)) .render(area, buf); } } diff --git a/yazi-fm/src/cmp/mod.rs b/yazi-fm/src/cmp/mod.rs new file mode 100644 index 00000000..4fc18fd3 --- /dev/null +++ b/yazi-fm/src/cmp/mod.rs @@ -0,0 +1 @@ +yazi_macro::mod_flat!(cmp); diff --git a/yazi-fm/src/completion/mod.rs b/yazi-fm/src/completion/mod.rs deleted file mode 100644 index 20a9eaee..00000000 --- a/yazi-fm/src/completion/mod.rs +++ /dev/null @@ -1 +0,0 @@ -yazi_macro::mod_flat!(completion); diff --git a/yazi-fm/src/context.rs b/yazi-fm/src/context.rs index f2683816..f8d5b1b7 100644 --- a/yazi-fm/src/context.rs +++ b/yazi-fm/src/context.rs @@ -1,31 +1,31 @@ use ratatui::layout::Rect; -use yazi_core::{completion::Completion, confirm::Confirm, help::Help, input::Input, mgr::Mgr, notify::Notify, pick::Pick, tab::{Folder, Tab}, tasks::Tasks, which::Which}; +use yazi_core::{cmp::Cmp, confirm::Confirm, help::Help, input::Input, mgr::Mgr, notify::Notify, pick::Pick, tab::{Folder, Tab}, tasks::Tasks, which::Which}; use yazi_shared::Layer; pub struct Ctx { - pub mgr: Mgr, - pub tasks: Tasks, - pub pick: Pick, - pub input: Input, - pub confirm: Confirm, - pub help: Help, - pub completion: Completion, - pub which: Which, - pub notify: Notify, + pub mgr: Mgr, + pub tasks: Tasks, + pub pick: Pick, + pub input: Input, + pub confirm: Confirm, + pub help: Help, + pub cmp: Cmp, + pub which: Which, + pub notify: Notify, } impl Ctx { pub fn make() -> Self { Self { - mgr: Mgr::make(), - tasks: Tasks::serve(), - pick: Default::default(), - input: Default::default(), - confirm: Default::default(), - help: Default::default(), - completion: Default::default(), - which: Default::default(), - notify: Default::default(), + mgr: Mgr::make(), + tasks: Tasks::serve(), + pick: Default::default(), + input: Default::default(), + confirm: Default::default(), + help: Default::default(), + cmp: Default::default(), + which: Default::default(), + notify: Default::default(), } } @@ -45,8 +45,8 @@ impl Ctx { pub fn layer(&self) -> Layer { if self.which.visible { Layer::Which - } else if self.completion.visible { - Layer::Completion + } else if self.cmp.visible { + Layer::Cmp } else if self.help.visible { Layer::Help } else if self.confirm.visible { diff --git a/yazi-fm/src/executor.rs b/yazi-fm/src/executor.rs index 758711a5..aef7b93f 100644 --- a/yazi-fm/src/executor.rs +++ b/yazi-fm/src/executor.rs @@ -12,8 +12,8 @@ impl<'a> Executor<'a> { pub(super) fn new(app: &'a mut App) -> Self { Self { app } } #[inline] - pub(super) fn execute(&mut self, cmd: CmdCow, layer: Layer) { - match layer { + pub(super) fn execute(&mut self, cmd: CmdCow) { + match cmd.layer { Layer::App => self.app(cmd), Layer::Mgr => self.mgr(cmd), Layer::Tasks => self.tasks(cmd), @@ -22,7 +22,7 @@ impl<'a> Executor<'a> { Layer::Input => self.input(cmd), Layer::Confirm => self.confirm(cmd), Layer::Help => self.help(cmd), - Layer::Completion => self.completion(cmd), + Layer::Cmp => self.cmp(cmd), Layer::Which => self.which(cmd), } } @@ -249,7 +249,7 @@ impl<'a> Executor<'a> { if cmd.name.as_str() == "complete" { return if cmd.bool("trigger") { - self.app.cx.completion.trigger(cmd) + self.app.cx.cmp.trigger(cmd) } else { self.app.cx.input.complete(cmd) }; @@ -319,11 +319,11 @@ impl<'a> Executor<'a> { } } - fn completion(&mut self, cmd: CmdCow) { + fn cmp(&mut self, cmd: CmdCow) { macro_rules! on { ($name:ident) => { if cmd.name == stringify!($name) { - return self.app.cx.completion.$name(cmd); + return self.app.cx.cmp.$name(cmd); } }; } @@ -336,7 +336,7 @@ impl<'a> Executor<'a> { match cmd.name.as_str() { "close_input" => self.app.cx.input.close(cmd), // Help - "help" => self.app.cx.help.toggle(Layer::Completion), + "help" => self.app.cx.help.toggle(Layer::Cmp), // Plugin "plugin" => self.app.plugin(cmd), _ => {} diff --git a/yazi-fm/src/main.rs b/yazi-fm/src/main.rs index 7cdd0f4a..53fa047a 100644 --- a/yazi-fm/src/main.rs +++ b/yazi-fm/src/main.rs @@ -4,7 +4,7 @@ #[global_allocator] static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; -yazi_macro::mod_pub!(app completion confirm help input lives mgr notify pick spot tasks which); +yazi_macro::mod_pub!(app cmp confirm help input lives mgr notify pick spot tasks which); yazi_macro::mod_flat!(context executor logs panic root router signals term); diff --git a/yazi-fm/src/root.rs b/yazi-fm/src/root.rs index 67d1aa5a..bd80c142 100644 --- a/yazi-fm/src/root.rs +++ b/yazi-fm/src/root.rs @@ -3,7 +3,7 @@ use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget}; use tracing::error; use yazi_plugin::{LUA, elements::render_once}; -use super::{completion, confirm, help, input, mgr, pick, spot, tasks, which}; +use super::{cmp, confirm, help, input, mgr, pick, spot, tasks, which}; use crate::Ctx; pub(super) struct Root<'a> { @@ -60,8 +60,8 @@ impl Widget for Root<'_> { help::Help::new(self.cx).render(area, buf); } - if self.cx.completion.visible { - completion::Completion::new(self.cx).render(area, buf); + if self.cx.cmp.visible { + cmp::Cmp::new(self.cx).render(area, buf); } if self.cx.which.visible { diff --git a/yazi-fm/src/router.rs b/yazi-fm/src/router.rs index 7a900537..7ffc5ee1 100644 --- a/yazi-fm/src/router.rs +++ b/yazi-fm/src/router.rs @@ -30,7 +30,7 @@ impl<'a> Router<'a> { L::Mgr | L::Tasks | L::Spot | L::Pick | L::Input | L::Confirm | L::Help => { self.matches(layer, key) } - L::Completion => self.matches(L::Completion, key) || self.matches(L::Input, key), + L::Cmp => self.matches(L::Cmp, key) || self.matches(L::Input, key), L::Which => cx.which.type_(key), } } @@ -45,7 +45,7 @@ impl<'a> Router<'a> { if on.len() > 1 { self.app.cx.which.show_with(key, layer); } else { - emit!(Seq(ChordCow::from(chord).into_seq(), layer)); + emit!(Seq(ChordCow::from(chord).into_seq())); } return true; } diff --git a/yazi-fs/src/op.rs b/yazi-fs/src/op.rs index ff5150ff..d1f4cd70 100644 --- a/yazi-fs/src/op.rs +++ b/yazi-fs/src/op.rs @@ -1,6 +1,6 @@ use std::collections::{HashMap, HashSet}; -use yazi_shared::{Id, Ids, Layer, event::Cmd, url::{Url, UrnBuf}}; +use yazi_shared::{Id, Ids, event::Cmd, url::{Url, UrnBuf}}; use super::{Cha, File}; @@ -39,11 +39,8 @@ impl FilesOp { #[inline] pub fn emit(self) { - yazi_shared::event::Event::Call( - Cmd::new("update_files").with_any("op", self).into(), - Layer::Mgr, - ) - .emit(); + yazi_shared::event::Event::Call(Cmd::new("mgr:update_files").with_any("op", self).into()) + .emit(); } pub fn prepare(cwd: &Url) -> Id { diff --git a/yazi-macro/src/event.rs b/yazi-macro/src/event.rs index 31fd73b8..c2f5c48b 100644 --- a/yazi-macro/src/event.rs +++ b/yazi-macro/src/event.rs @@ -3,11 +3,11 @@ macro_rules! emit { (Quit($opt:expr)) => { yazi_shared::event::Event::Quit($opt).emit(); }; - (Call($cmd:expr, $layer:expr)) => { - yazi_shared::event::Event::Call(yazi_shared::event::CmdCow::from($cmd), $layer).emit(); + (Call($cmd:expr)) => { + yazi_shared::event::Event::Call(yazi_shared::event::CmdCow::from($cmd)).emit(); }; - (Seq($cmds:expr, $layer:expr)) => { - yazi_shared::event::Event::Seq($cmds, $layer).emit(); + (Seq($cmds:expr)) => { + yazi_shared::event::Event::Seq($cmds).emit(); }; ($event:ident) => { yazi_shared::event::Event::$event.emit(); diff --git a/yazi-plugin/src/utils/call.rs b/yazi-plugin/src/utils/call.rs index 1bcd4d4f..6cbcd196 100644 --- a/yazi-plugin/src/utils/call.rs +++ b/yazi-plugin/src/utils/call.rs @@ -40,21 +40,21 @@ impl Utils { pub(super) fn app_emit(lua: &Lua) -> mlua::Result { lua.create_function(|_, (name, args): (String, Table)| { - emit!(Call(Cmd { name, args: Sendable::table_to_args(args)? }, Layer::App)); + emit!(Call(Cmd { name, args: Sendable::table_to_args(args)?, layer: Layer::App })); Ok(()) }) } pub(super) fn mgr_emit(lua: &Lua) -> mlua::Result { lua.create_function(|_, (name, args): (String, Table)| { - emit!(Call(Cmd { name, args: Sendable::table_to_args(args)? }, Layer::Mgr)); + emit!(Call(Cmd { name, args: Sendable::table_to_args(args)?, layer: Layer::Mgr })); Ok(()) }) } pub(super) fn input_emit(lua: &Lua) -> mlua::Result { lua.create_function(|_, (name, args): (String, Table)| { - emit!(Call(Cmd { name, args: Sendable::table_to_args(args)? }, Layer::Input)); + emit!(Call(Cmd { name, args: Sendable::table_to_args(args)?, layer: Layer::Input })); Ok(()) }) } diff --git a/yazi-plugin/src/utils/layer.rs b/yazi-plugin/src/utils/layer.rs index e9b4960c..8f4cf515 100644 --- a/yazi-plugin/src/utils/layer.rs +++ b/yazi-plugin/src/utils/layer.rs @@ -6,7 +6,7 @@ use tokio_stream::wrappers::UnboundedReceiverStream; use yazi_config::{keymap::{Chord, Key}, popup::{ConfirmCfg, InputCfg}}; use yazi_macro::emit; use yazi_proxy::{AppProxy, ConfirmProxy, InputProxy}; -use yazi_shared::{Debounce, Layer, event::Cmd}; +use yazi_shared::{Debounce, event::Cmd}; use super::Utils; use crate::{bindings::InputRx, elements::{Line, Pos, Text}}; @@ -21,18 +21,16 @@ impl Utils { let cand = cand?; cands.push(Chord { on: Self::parse_keys(cand.raw_get("on")?)?, - run: vec![Cmd::args("callback", &[i]).with_any("tx", tx.clone())], + run: vec![Cmd::args("which:callback", &[i]).with_any("tx", tx.clone())], desc: cand.raw_get("desc").ok(), }); } drop(tx); emit!(Call( - Cmd::new("show") - .with("layer", Layer::Which) + Cmd::new("which:show") .with_any("candidates", cands) - .with_bool("silent", t.raw_get("silent").unwrap_or_default()), - Layer::Which + .with_bool("silent", t.raw_get("silent").unwrap_or_default()) )); Ok(rx.recv().await.map(|idx| idx + 1)) diff --git a/yazi-plugin/src/utils/preview.rs b/yazi-plugin/src/utils/preview.rs index 407b49bb..19915519 100644 --- a/yazi-plugin/src/utils/preview.rs +++ b/yazi-plugin/src/utils/preview.rs @@ -1,7 +1,7 @@ use mlua::{AnyUserData, Function, IntoLuaMulti, Lua, Table, Value}; use yazi_config::{PREVIEW, preview::PreviewWrap}; use yazi_macro::emit; -use yazi_shared::{Layer, errors::PeekError, event::Cmd}; +use yazi_shared::{errors::PeekError, event::Cmd}; use super::Utils; use crate::{elements::{Area, Rect, Renderable, Text, WRAP, WRAP_NO}, external::Highlighter, file::FileRef}; @@ -54,7 +54,7 @@ impl Utils { wrap: if PREVIEW.wrap == PreviewWrap::Yes { WRAP } else { WRAP_NO }, })]; - emit!(Call(Cmd::new("update_peeked").with_any("lock", lock), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:update_peeked").with_any("lock", lock))); (Value::Nil, Value::Nil).into_lua_multi(&lua) }) } @@ -64,7 +64,7 @@ impl Utils { let mut lock = PreviewLock::try_from(t)?; lock.data = widgets.into_iter().map(Renderable::try_from).collect::>()?; - emit!(Call(Cmd::new("update_peeked").with_any("lock", lock), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:update_peeked").with_any("lock", lock))); Ok(()) }) } diff --git a/yazi-plugin/src/utils/spot.rs b/yazi-plugin/src/utils/spot.rs index 23d4bfbb..70e39368 100644 --- a/yazi-plugin/src/utils/spot.rs +++ b/yazi-plugin/src/utils/spot.rs @@ -1,7 +1,7 @@ use mlua::{AnyUserData, Function, Lua, Table}; use yazi_config::THEME; use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd}; +use yazi_shared::event::Cmd; use super::Utils; use crate::{elements::Renderable, file::FileRef}; @@ -80,7 +80,7 @@ impl Utils { }), Renderable::Table(table), ]; - emit!(Call(Cmd::new("update_spotted").with_any("lock", lock), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:update_spotted").with_any("lock", lock))); Ok(()) }) @@ -91,7 +91,7 @@ impl Utils { let mut lock = SpotLock::try_from(t)?; lock.data = widgets.into_iter().map(Renderable::try_from).collect::>()?; - emit!(Call(Cmd::new("update_spotted").with_any("lock", lock), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:update_spotted").with_any("lock", lock))); Ok(()) }) } diff --git a/yazi-proxy/src/app.rs b/yazi-proxy/src/app.rs index bef61e18..a06d05fb 100644 --- a/yazi-proxy/src/app.rs +++ b/yazi-proxy/src/app.rs @@ -2,7 +2,7 @@ use std::time::Duration; use tokio::sync::oneshot; use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd}; +use yazi_shared::event::Cmd; use crate::options::{NotifyLevel, NotifyOpt, PluginOpt}; @@ -12,18 +12,18 @@ impl AppProxy { #[inline] pub async fn stop() { let (tx, rx) = oneshot::channel::<()>(); - emit!(Call(Cmd::new("stop").with_any("tx", tx), Layer::App)); + emit!(Call(Cmd::new("app:stop").with_any("tx", tx))); rx.await.ok(); } #[inline] pub fn resume() { - emit!(Call(Cmd::new("resume"), Layer::App)); + emit!(Call(Cmd::new("app:resume"))); } #[inline] pub fn notify(opt: NotifyOpt) { - emit!(Call(Cmd::new("notify").with_any("option", opt), Layer::App)); + emit!(Call(Cmd::new("app:notify").with_any("option", opt))); } #[inline] @@ -48,11 +48,11 @@ impl AppProxy { #[inline] pub fn plugin(opt: PluginOpt) { - emit!(Call(Cmd::new("plugin").with_any("opt", opt), Layer::App)); + emit!(Call(Cmd::new("app:plugin").with_any("opt", opt))); } #[inline] pub fn plugin_do(opt: PluginOpt) { - emit!(Call(Cmd::new("plugin_do").with_any("opt", opt), Layer::App)); + emit!(Call(Cmd::new("app:plugin_do").with_any("opt", opt))); } } diff --git a/yazi-proxy/src/cmp.rs b/yazi-proxy/src/cmp.rs new file mode 100644 index 00000000..cb02e11c --- /dev/null +++ b/yazi-proxy/src/cmp.rs @@ -0,0 +1,16 @@ +use yazi_macro::emit; +use yazi_shared::event::Cmd; + +pub struct CmpProxy; + +impl CmpProxy { + #[inline] + pub fn close() { + emit!(Call(Cmd::new("cmp:close"))); + } + + #[inline] + pub fn trigger(word: &str, ticket: usize) { + emit!(Call(Cmd::args("cmp:trigger", &[word]).with("ticket", ticket))); + } +} diff --git a/yazi-proxy/src/completion.rs b/yazi-proxy/src/completion.rs deleted file mode 100644 index 3d9331bd..00000000 --- a/yazi-proxy/src/completion.rs +++ /dev/null @@ -1,16 +0,0 @@ -use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd}; - -pub struct CompletionProxy; - -impl CompletionProxy { - #[inline] - pub fn close() { - emit!(Call(Cmd::new("close"), Layer::Completion)); - } - - #[inline] - pub fn trigger(word: &str, ticket: usize) { - emit!(Call(Cmd::args("trigger", &[word]).with("ticket", ticket), Layer::Completion)); - } -} diff --git a/yazi-proxy/src/confirm.rs b/yazi-proxy/src/confirm.rs index 2cca1267..5a0e16e1 100644 --- a/yazi-proxy/src/confirm.rs +++ b/yazi-proxy/src/confirm.rs @@ -1,7 +1,7 @@ use tokio::sync::oneshot; use yazi_config::popup::ConfirmCfg; use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd}; +use yazi_shared::event::Cmd; pub struct ConfirmProxy; @@ -12,7 +12,7 @@ impl ConfirmProxy { #[inline] pub fn show_rx(cfg: ConfirmCfg) -> oneshot::Receiver { let (tx, rx) = oneshot::channel(); - emit!(Call(Cmd::new("show").with_any("tx", tx).with_any("cfg", cfg), Layer::Confirm)); + emit!(Call(Cmd::new("confirm:show").with_any("tx", tx).with_any("cfg", cfg))); rx } } diff --git a/yazi-proxy/src/input.rs b/yazi-proxy/src/input.rs index b026f8d7..187b56b4 100644 --- a/yazi-proxy/src/input.rs +++ b/yazi-proxy/src/input.rs @@ -1,7 +1,7 @@ use tokio::sync::mpsc; use yazi_config::popup::InputCfg; use yazi_macro::emit; -use yazi_shared::{Layer, errors::InputError, event::Cmd}; +use yazi_shared::{errors::InputError, event::Cmd}; pub struct InputProxy; @@ -9,12 +9,12 @@ impl InputProxy { #[inline] pub fn show(cfg: InputCfg) -> mpsc::UnboundedReceiver> { let (tx, rx) = mpsc::unbounded_channel(); - emit!(Call(Cmd::new("show").with_any("tx", tx).with_any("cfg", cfg), Layer::Input)); + emit!(Call(Cmd::new("input:show").with_any("tx", tx).with_any("cfg", cfg))); rx } #[inline] pub fn complete(word: &str, ticket: usize) { - emit!(Call(Cmd::args("complete", &[word]).with("ticket", ticket), Layer::Input)); + emit!(Call(Cmd::args("input:complete", &[word]).with("ticket", ticket))); } } diff --git a/yazi-proxy/src/lib.rs b/yazi-proxy/src/lib.rs index 03cf4c6c..519474ca 100644 --- a/yazi-proxy/src/lib.rs +++ b/yazi-proxy/src/lib.rs @@ -2,6 +2,6 @@ mod macros; yazi_macro::mod_pub!(options); -yazi_macro::mod_flat!(app completion confirm input mgr pick semaphore tab tasks); +yazi_macro::mod_flat!(app cmp confirm input mgr pick semaphore tab tasks); pub fn init() { crate::init_semaphore(); } diff --git a/yazi-proxy/src/mgr.rs b/yazi-proxy/src/mgr.rs index fbc3eff0..f8a44c46 100644 --- a/yazi-proxy/src/mgr.rs +++ b/yazi-proxy/src/mgr.rs @@ -1,5 +1,5 @@ use yazi_macro::emit; -use yazi_shared::{Id, Layer, event::Cmd, url::Url}; +use yazi_shared::{Id, event::Cmd, url::Url}; use crate::options::OpenDoOpt; @@ -8,55 +8,48 @@ pub struct MgrProxy; impl MgrProxy { #[inline] pub fn spot(skip: Option) { - emit!(Call(Cmd::new("spot").with_opt("skip", skip), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:spot").with_opt("skip", skip))); } #[inline] pub fn peek(force: bool) { - emit!(Call(Cmd::new("peek").with_bool("force", force), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:peek").with_bool("force", force))); } #[inline] pub fn hover(url: Option, tab: Id) { - emit!(Call( - Cmd::args("hover", &url.map_or_else(Vec::new, |u| vec![u])).with("tab", tab), - Layer::Mgr - )); + emit!(Call(Cmd::args("mgr:hover", &url.map_or_else(Vec::new, |u| vec![u])).with("tab", tab))); } #[inline] pub fn refresh() { - emit!(Call(Cmd::new("refresh"), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:refresh"))); } #[inline] pub fn open_do(opt: OpenDoOpt) { - emit!(Call(Cmd::new("open_do").with_any("option", opt), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:open_do").with_any("option", opt))); } #[inline] pub fn remove_do(targets: Vec, permanently: bool) { emit!(Call( - Cmd::new("remove_do").with_bool("permanently", permanently).with_any("targets", targets), - Layer::Mgr + Cmd::new("mgr:remove_do").with_bool("permanently", permanently).with_any("targets", targets) )); } #[inline] pub fn update_tasks(url: &Url) { - emit!(Call(Cmd::new("update_tasks").with_any("urls", vec![url.clone()]), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:update_tasks").with_any("urls", vec![url.clone()]))); } #[inline] pub fn update_paged() { - emit!(Call(Cmd::new("update_paged"), Layer::Mgr)); + emit!(Call(Cmd::new("mgr:update_paged"))); } #[inline] pub fn update_paged_by(page: usize, only_if: &Url) { - emit!(Call( - Cmd::args("update_paged", &[page]).with_any("only-if", only_if.clone()), - Layer::Mgr - )); + emit!(Call(Cmd::args("mgr:update_paged", &[page]).with_any("only-if", only_if.clone()))); } } diff --git a/yazi-proxy/src/pick.rs b/yazi-proxy/src/pick.rs index 5375898b..f91694ef 100644 --- a/yazi-proxy/src/pick.rs +++ b/yazi-proxy/src/pick.rs @@ -1,7 +1,7 @@ use tokio::sync::oneshot; use yazi_config::popup::PickCfg; use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd}; +use yazi_shared::event::Cmd; pub struct PickProxy; @@ -9,7 +9,7 @@ impl PickProxy { #[inline] pub async fn show(cfg: PickCfg) -> anyhow::Result { let (tx, rx) = oneshot::channel(); - emit!(Call(Cmd::new("show").with_any("tx", tx).with_any("cfg", cfg), Layer::Pick)); + emit!(Call(Cmd::new("pick:show").with_any("tx", tx).with_any("cfg", cfg))); rx.await? } } diff --git a/yazi-proxy/src/tab.rs b/yazi-proxy/src/tab.rs index 1d0b71fb..ef846588 100644 --- a/yazi-proxy/src/tab.rs +++ b/yazi-proxy/src/tab.rs @@ -1,5 +1,5 @@ use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd, url::Url}; +use yazi_shared::{event::Cmd, url::Url}; use crate::options::SearchOpt; @@ -8,25 +8,24 @@ pub struct TabProxy; impl TabProxy { #[inline] pub fn cd(target: &Url) { - emit!(Call(Cmd::args("cd", &[target]), Layer::Mgr)); + emit!(Call(Cmd::args("mgr:cd", &[target]))); } #[inline] pub fn reveal(target: &Url) { - emit!(Call(Cmd::args("reveal", &[target]), Layer::Mgr)); + emit!(Call(Cmd::args("mgr:reveal", &[target]))); } #[inline] pub fn arrow(step: isize) { - emit!(Call(Cmd::args("arrow", &[step]), Layer::Mgr)); + emit!(Call(Cmd::args("mgr:arrow", &[step]))); } #[inline] pub fn search_do(opt: SearchOpt) { emit!(Call( // TODO: use second positional argument instead of `args` parameter - Cmd::args("search_do", &[opt.subject]).with("via", opt.via).with("args", opt.args_raw), - Layer::Mgr + Cmd::args("mgr:search_do", &[opt.subject]).with("via", opt.via).with("args", opt.args_raw) )); } } diff --git a/yazi-proxy/src/tasks.rs b/yazi-proxy/src/tasks.rs index 667a5a8e..91a82723 100644 --- a/yazi-proxy/src/tasks.rs +++ b/yazi-proxy/src/tasks.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, ffi::OsString}; use tokio::sync::oneshot; use yazi_config::open::Opener; use yazi_macro::emit; -use yazi_shared::{Layer, event::Cmd, url::Url}; +use yazi_shared::{event::Cmd, url::Url}; use crate::options::{OpenWithOpt, ProcessExecOpt}; @@ -12,24 +12,22 @@ pub struct TasksProxy; impl TasksProxy { #[inline] pub fn open_with(opener: Cow<'static, Opener>, cwd: Url, targets: Vec) { - emit!(Call( - Cmd::new("open_with").with_any("option", OpenWithOpt { opener, cwd, targets }), - Layer::Tasks - )); + emit!(Call(Cmd::new("tasks:open_with").with_any("option", OpenWithOpt { + opener, + cwd, + targets + }))); } #[inline] pub async fn process_exec(opener: Cow<'static, Opener>, cwd: Url, args: Vec) { let (tx, rx) = oneshot::channel(); - emit!(Call( - Cmd::new("process_exec").with_any("option", ProcessExecOpt { - cwd, - opener, - args, - done: Some(tx) - }), - Layer::Tasks - )); + emit!(Call(Cmd::new("tasks:process_exec").with_any("option", ProcessExecOpt { + cwd, + opener, + args, + done: Some(tx) + }))); rx.await.ok(); } } diff --git a/yazi-shared/src/event/cmd.rs b/yazi-shared/src/event/cmd.rs index cb3db027..458e76b7 100644 --- a/yazi-shared/src/event/cmd.rs +++ b/yazi-shared/src/event/cmd.rs @@ -1,31 +1,36 @@ -use std::{any::Any, borrow::Cow, collections::HashMap, fmt::{self, Display}, mem, str::FromStr}; +use std::{any::Any, borrow::Cow, collections::HashMap, fmt::{self, Display}, str::FromStr}; use anyhow::{Result, bail}; use serde::{Deserialize, de}; use super::{Data, DataKey}; -use crate::url::Url; +use crate::{Layer, url::Url}; #[derive(Debug, Default)] pub struct Cmd { - pub name: String, - pub args: HashMap, + pub name: String, + pub args: HashMap, + pub layer: Layer, } impl Cmd { - #[inline] - pub fn new(name: &str) -> Self { Self { name: name.to_owned(), ..Default::default() } } + pub fn new(s: &str) -> Self { + let (layer, name) = match s.split_once(':') { + Some((l, n)) => (Layer::from_str(l).unwrap_or_default(), n), + None => (Layer::default(), s), + }; + + Self { name: name.to_owned(), args: Default::default(), layer } + } - #[inline] pub fn args(name: &str, args: &[impl ToString]) -> Self { - Self { - name: name.to_owned(), - args: args - .iter() - .enumerate() - .map(|(i, s)| (DataKey::Integer(i as i64), Data::String(s.to_string()))) - .collect(), - } + let mut me = Self::new(name); + me.args = args + .iter() + .enumerate() + .map(|(i, s)| (DataKey::Integer(i as i64), Data::String(s.to_string()))) + .collect(); + me } // --- With @@ -170,15 +175,14 @@ impl FromStr for Cmd { type Err = anyhow::Error; fn from_str(s: &str) -> Result { - let mut args = crate::shell::split_unix(s)?; + let args = crate::shell::split_unix(s)?; if args.is_empty() || args[0].is_empty() { bail!("command name cannot be empty"); } - Ok(Cmd { - name: mem::take(&mut args[0]), - args: Cmd::parse_args(args.into_iter().skip(1), true)?, - }) + let mut me = Self::new(&args[0]); + me.args = Cmd::parse_args(args.into_iter().skip(1), true)?; + Ok(me) } } diff --git a/yazi-shared/src/event/event.rs b/yazi-shared/src/event/event.rs index a18b39d1..9bb24f48 100644 --- a/yazi-shared/src/event/event.rs +++ b/yazi-shared/src/event/event.rs @@ -4,15 +4,15 @@ use crossterm::event::{KeyEvent, MouseEvent}; use tokio::sync::mpsc; use super::CmdCow; -use crate::{Layer, RoCell}; +use crate::RoCell; static TX: RoCell> = RoCell::new(); static RX: RoCell> = RoCell::new(); #[derive(Debug)] pub enum Event { - Call(CmdCow, Layer), - Seq(Vec, Layer), + Call(CmdCow), + Seq(Vec), Render, Key(KeyEvent), Mouse(MouseEvent), diff --git a/yazi-shared/src/layer.rs b/yazi-shared/src/layer.rs index d544a9c9..18fc36a0 100644 --- a/yazi-shared/src/layer.rs +++ b/yazi-shared/src/layer.rs @@ -14,7 +14,7 @@ pub enum Layer { Input, Confirm, Help, - Completion, + Cmp, Which, } @@ -29,7 +29,7 @@ impl Display for Layer { Self::Input => "input", Self::Confirm => "confirm", Self::Help => "help", - Self::Completion => "completion", + Self::Cmp => "cmp", Self::Which => "which", }) }