From ff14b9a265e8ef09e0313277a1b306b0fe804fde 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: Fri, 19 Apr 2024 16:26:02 +0800 Subject: [PATCH] fix: always persist static messages even when there are no remote clients (#928) --- yazi-core/src/completion/commands/arrow.rs | 6 +-- yazi-core/src/completion/commands/close.rs | 2 +- yazi-core/src/completion/commands/show.rs | 4 +- yazi-core/src/completion/commands/trigger.rs | 4 +- yazi-core/src/folder/filter.rs | 2 +- yazi-core/src/help/commands/arrow.rs | 6 +-- yazi-core/src/input/commands/backspace.rs | 2 +- yazi-core/src/input/commands/close.rs | 2 +- yazi-core/src/input/commands/complete.rs | 4 +- yazi-core/src/input/commands/delete.rs | 2 +- yazi-core/src/input/commands/forward.rs | 2 +- yazi-core/src/input/commands/insert.rs | 2 +- yazi-core/src/input/commands/move_.rs | 8 +-- yazi-core/src/input/commands/paste.rs | 2 +- yazi-core/src/manager/commands/create.rs | 2 +- yazi-core/src/manager/commands/hover.rs | 4 +- yazi-core/src/manager/commands/link.rs | 2 +- yazi-core/src/manager/commands/open.rs | 2 +- yazi-core/src/manager/commands/paste.rs | 2 +- yazi-core/src/manager/commands/peek.rs | 10 ++-- yazi-core/src/manager/commands/quit.rs | 2 +- yazi-core/src/manager/commands/remove.rs | 4 +- yazi-core/src/manager/commands/rename.rs | 2 +- yazi-core/src/manager/commands/seek.rs | 6 +-- yazi-core/src/manager/commands/tab_close.rs | 6 +-- yazi-core/src/manager/commands/tab_create.rs | 6 +-- yazi-core/src/manager/commands/tab_swap.rs | 6 +-- yazi-core/src/manager/commands/tab_switch.rs | 9 ++-- .../src/manager/commands/update_mimetype.rs | 2 +- .../src/manager/commands/update_paged.rs | 6 +-- yazi-core/src/manager/commands/yank.rs | 2 +- yazi-core/src/notify/commands/tick.rs | 6 +-- yazi-core/src/select/commands/arrow.rs | 6 +-- yazi-core/src/select/commands/close.rs | 2 +- yazi-core/src/tab/commands/arrow.rs | 10 +++- yazi-core/src/tab/commands/cd.rs | 6 +-- yazi-core/src/tab/commands/filter.rs | 2 +- yazi-core/src/tab/commands/find.rs | 4 +- yazi-core/src/tab/commands/reveal.rs | 4 +- yazi-core/src/tab/commands/select.rs | 4 +- yazi-core/src/tab/commands/shell.rs | 6 +-- yazi-core/src/tab/commands/sort.rs | 6 +-- yazi-core/src/tab/commands/visual_mode.rs | 2 +- yazi-core/src/tasks/commands/arrow.rs | 6 +-- yazi-core/src/which/commands/callback.rs | 4 +- yazi-core/src/which/commands/show.rs | 2 +- yazi-dds/src/pubsub.rs | 4 +- yazi-dds/src/server.rs | 6 +-- yazi-fm/src/executor.rs | 2 +- yazi-plugin/src/opt.rs | 4 +- yazi-shared/src/event/cmd.rs | 19 +++++-- yazi-shared/src/event/data.rs | 50 ++++++++++++++++++- 52 files changed, 162 insertions(+), 114 deletions(-) diff --git a/yazi-core/src/completion/commands/arrow.rs b/yazi-core/src/completion/commands/arrow.rs index 2aaca25c..ed11a79d 100644 --- a/yazi-core/src/completion/commands/arrow.rs +++ b/yazi-core/src/completion/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::completion::Completion; @@ -7,9 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) } - } + fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } } } impl Completion { diff --git a/yazi-core/src/completion/commands/close.rs b/yazi-core/src/completion/commands/close.rs index c3117ba2..d331978b 100644 --- a/yazi-core/src/completion/commands/close.rs +++ b/yazi-core/src/completion/commands/close.rs @@ -8,7 +8,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { submit: c.get_bool("submit") } } + fn from(c: Cmd) -> Self { Self { submit: c.bool("submit") } } } impl Completion { diff --git a/yazi-core/src/completion/commands/show.rs b/yazi-core/src/completion/commands/show.rs index b5b69ebd..f9cb9011 100644 --- a/yazi-core/src/completion/commands/show.rs +++ b/yazi-core/src/completion/commands/show.rs @@ -1,6 +1,6 @@ use std::{mem, ops::ControlFlow}; -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::completion::Completion; @@ -19,7 +19,7 @@ impl From for Opt { cache: c.take_any("cache").unwrap_or_default(), cache_name: c.take_str("cache-name").unwrap_or_default(), word: c.take_str("word").unwrap_or_default(), - ticket: c.take_str("ticket").and_then(|v| v.parse().ok()).unwrap_or(0), + ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0), } } } diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/completion/commands/trigger.rs index 7b4a5241..535fc3c7 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/completion/commands/trigger.rs @@ -1,7 +1,7 @@ use std::{mem, path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR}}; use tokio::fs; -use yazi_shared::{emit, event::Cmd, render, Layer}; +use yazi_shared::{emit, event::{Cmd, Data}, render, Layer}; use crate::completion::Completion; @@ -20,7 +20,7 @@ impl From for Opt { fn from(mut c: Cmd) -> Self { Self { word: c.take_first_str().unwrap_or_default(), - ticket: c.take_str("ticket").and_then(|s| s.parse().ok()).unwrap_or(0), + ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0), } } } diff --git a/yazi-core/src/folder/filter.rs b/yazi-core/src/folder/filter.rs index 0c40bf89..8d7ca45e 100644 --- a/yazi-core/src/folder/filter.rs +++ b/yazi-core/src/folder/filter.rs @@ -45,7 +45,7 @@ pub enum FilterCase { impl From<&Cmd> for FilterCase { fn from(c: &Cmd) -> Self { - match (c.get_bool("smart"), c.get_bool("insensitive")) { + match (c.bool("smart"), c.bool("insensitive")) { (true, _) => Self::Smart, (_, false) => Self::Sensitive, (_, true) => Self::Insensitive, diff --git a/yazi-core/src/help/commands/arrow.rs b/yazi-core/src/help/commands/arrow.rs index bd98a156..b84f8e30 100644 --- a/yazi-core/src/help/commands/arrow.rs +++ b/yazi-core/src/help/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::help::Help; @@ -7,9 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) } - } + fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } } } impl From for Opt { fn from(step: isize) -> Self { Self { step } } diff --git a/yazi-core/src/input/commands/backspace.rs b/yazi-core/src/input/commands/backspace.rs index 5377529a..37b0e5ac 100644 --- a/yazi-core/src/input/commands/backspace.rs +++ b/yazi-core/src/input/commands/backspace.rs @@ -7,7 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { under: c.get_bool("under") } } + fn from(c: Cmd) -> Self { Self { under: c.bool("under") } } } impl From for Opt { fn from(under: bool) -> Self { Self { under } } diff --git a/yazi-core/src/input/commands/close.rs b/yazi-core/src/input/commands/close.rs index c27927b3..42ecfdaa 100644 --- a/yazi-core/src/input/commands/close.rs +++ b/yazi-core/src/input/commands/close.rs @@ -8,7 +8,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { submit: c.get_bool("submit") } } + fn from(c: Cmd) -> Self { Self { submit: c.bool("submit") } } } impl From for Opt { fn from(submit: bool) -> Self { Self { submit } } diff --git a/yazi-core/src/input/commands/complete.rs b/yazi-core/src/input/commands/complete.rs index e2272695..8c3704c1 100644 --- a/yazi-core/src/input/commands/complete.rs +++ b/yazi-core/src/input/commands/complete.rs @@ -1,6 +1,6 @@ use std::path::MAIN_SEPARATOR_STR; -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::input::Input; @@ -19,7 +19,7 @@ impl From for Opt { fn from(mut c: Cmd) -> Self { Self { word: c.take_first_str().unwrap_or_default(), - ticket: c.take_str("ticket").and_then(|s| s.parse().ok()).unwrap_or(0), + ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0), } } } diff --git a/yazi-core/src/input/commands/delete.rs b/yazi-core/src/input/commands/delete.rs index 367bd7d3..115d6fac 100644 --- a/yazi-core/src/input/commands/delete.rs +++ b/yazi-core/src/input/commands/delete.rs @@ -8,7 +8,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { cut: c.get_bool("cut"), insert: c.get_bool("insert") } } + fn from(c: Cmd) -> Self { Self { cut: c.bool("cut"), insert: c.bool("insert") } } } impl Input { diff --git a/yazi-core/src/input/commands/forward.rs b/yazi-core/src/input/commands/forward.rs index 44514a89..afcbee50 100644 --- a/yazi-core/src/input/commands/forward.rs +++ b/yazi-core/src/input/commands/forward.rs @@ -7,7 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { end_of_word: c.get_bool("end-of-word") } } + fn from(c: Cmd) -> Self { Self { end_of_word: c.bool("end-of-word") } } } impl Input { diff --git a/yazi-core/src/input/commands/insert.rs b/yazi-core/src/input/commands/insert.rs index 28d860e8..d27ec367 100644 --- a/yazi-core/src/input/commands/insert.rs +++ b/yazi-core/src/input/commands/insert.rs @@ -7,7 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { append: c.get_bool("append") } } + fn from(c: Cmd) -> Self { Self { append: c.bool("append") } } } impl From for Opt { fn from(append: bool) -> Self { Self { append } } diff --git a/yazi-core/src/input/commands/move_.rs b/yazi-core/src/input/commands/move_.rs index 2c0536c6..aac48b7e 100644 --- a/yazi-core/src/input/commands/move_.rs +++ b/yazi-core/src/input/commands/move_.rs @@ -1,5 +1,5 @@ use unicode_width::UnicodeWidthStr; -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::input::{op::InputOp, snap::InputSnap, Input}; @@ -9,10 +9,10 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { + fn from(c: Cmd) -> Self { Self { - step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0), - in_operating: c.get_bool("in-operating"), + step: c.first().and_then(Data::as_isize).unwrap_or(0), + in_operating: c.bool("in-operating"), } } } diff --git a/yazi-core/src/input/commands/paste.rs b/yazi-core/src/input/commands/paste.rs index 58417341..4cd88758 100644 --- a/yazi-core/src/input/commands/paste.rs +++ b/yazi-core/src/input/commands/paste.rs @@ -7,7 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { before: c.get_bool("before") } } + fn from(c: Cmd) -> Self { Self { before: c.bool("before") } } } impl Input { diff --git a/yazi-core/src/manager/commands/create.rs b/yazi-core/src/manager/commands/create.rs index 7dc0a5d6..6e41386a 100644 --- a/yazi-core/src/manager/commands/create.rs +++ b/yazi-core/src/manager/commands/create.rs @@ -12,7 +12,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { force: c.get_bool("force") } } + fn from(c: Cmd) -> Self { Self { force: c.bool("force") } } } impl Manager { diff --git a/yazi-core/src/manager/commands/hover.rs b/yazi-core/src/manager/commands/hover.rs index 287e36d9..de934d69 100644 --- a/yazi-core/src/manager/commands/hover.rs +++ b/yazi-core/src/manager/commands/hover.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; -use yazi_shared::{event::Cmd, fs::Url, render}; +use yazi_shared::{event::{Cmd, Data}, fs::Url, render}; use crate::manager::Manager; @@ -9,7 +9,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { Self { url: c.take_first_str().map(Url::from) } } + fn from(mut c: Cmd) -> Self { Self { url: c.take_first().and_then(Data::into_url) } } } impl From> for Opt { fn from(url: Option) -> Self { Self { url } } diff --git a/yazi-core/src/manager/commands/link.rs b/yazi-core/src/manager/commands/link.rs index bb8935c1..4a3857b7 100644 --- a/yazi-core/src/manager/commands/link.rs +++ b/yazi-core/src/manager/commands/link.rs @@ -8,7 +8,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { relative: c.get_bool("relative"), force: c.get_bool("force") } } + fn from(c: Cmd) -> Self { Self { relative: c.bool("relative"), force: c.bool("force") } } } impl Manager { diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index 60d0af98..4e3d6b3e 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -17,7 +17,7 @@ pub struct Opt { impl From for Opt { fn from(c: Cmd) -> Self { - Self { interactive: c.get_bool("interactive"), hovered: c.get_bool("hovered") } + Self { interactive: c.bool("interactive"), hovered: c.bool("hovered") } } } diff --git a/yazi-core/src/manager/commands/paste.rs b/yazi-core/src/manager/commands/paste.rs index c85ed632..61a7c6ab 100644 --- a/yazi-core/src/manager/commands/paste.rs +++ b/yazi-core/src/manager/commands/paste.rs @@ -8,7 +8,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { force: c.get_bool("force"), follow: c.get_bool("follow") } } + fn from(c: Cmd) -> Self { Self { force: c.bool("force"), follow: c.bool("follow") } } } impl Manager { diff --git a/yazi-core/src/manager/commands/peek.rs b/yazi-core/src/manager/commands/peek.rs index 2c3e40b9..0d31033f 100644 --- a/yazi-core/src/manager/commands/peek.rs +++ b/yazi-core/src/manager/commands/peek.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, fs::Url, render}; +use yazi_shared::{event::{Cmd, Data}, fs::Url, render}; use crate::manager::Manager; @@ -13,10 +13,10 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { Self { - skip: c.take_first_str().and_then(|s| s.parse().ok()), - force: c.get_bool("force"), - only_if: c.take_str("only-if").map(Url::from), - upper_bound: c.get_bool("upper-bound"), + skip: c.first().and_then(Data::as_usize), + force: c.bool("force"), + only_if: c.take("only-if").and_then(Data::into_url), + upper_bound: c.bool("upper-bound"), } } } diff --git a/yazi-core/src/manager/commands/quit.rs b/yazi-core/src/manager/commands/quit.rs index 9f5f3483..04d5be98 100644 --- a/yazi-core/src/manager/commands/quit.rs +++ b/yazi-core/src/manager/commands/quit.rs @@ -12,7 +12,7 @@ impl From<()> for Opt { fn from(_: ()) -> Self { Self::default() } } impl From for Opt { - fn from(c: Cmd) -> Self { Self { no_cwd_file: c.get_bool("no-cwd-file") } } + fn from(c: Cmd) -> Self { Self { no_cwd_file: c.bool("no-cwd-file") } } } impl Manager { diff --git a/yazi-core/src/manager/commands/remove.rs b/yazi-core/src/manager/commands/remove.rs index 3826b175..5adeeb4b 100644 --- a/yazi-core/src/manager/commands/remove.rs +++ b/yazi-core/src/manager/commands/remove.rs @@ -13,8 +13,8 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { Self { - force: c.get_bool("force"), - permanently: c.get_bool("permanently"), + force: c.bool("force"), + permanently: c.bool("permanently"), targets: c.take_any("targets").unwrap_or_default(), } } diff --git a/yazi-core/src/manager/commands/rename.rs b/yazi-core/src/manager/commands/rename.rs index d9fffff4..d4d7d491 100644 --- a/yazi-core/src/manager/commands/rename.rs +++ b/yazi-core/src/manager/commands/rename.rs @@ -18,7 +18,7 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { Self { - force: c.get_bool("force"), + force: c.bool("force"), empty: c.take_str("empty").unwrap_or_default(), cursor: c.take_str("cursor").unwrap_or_default(), } diff --git a/yazi-core/src/manager/commands/seek.rs b/yazi-core/src/manager/commands/seek.rs index 936d7b9e..a80c57c2 100644 --- a/yazi-core/src/manager/commands/seek.rs +++ b/yazi-core/src/manager/commands/seek.rs @@ -1,6 +1,6 @@ use yazi_config::PLUGIN; use yazi_plugin::isolate; -use yazi_shared::{event::Cmd, render, MIME_DIR}; +use yazi_shared::{event::{Cmd, Data}, render, MIME_DIR}; use crate::manager::Manager; @@ -10,9 +10,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { units: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) } - } + fn from(c: Cmd) -> Self { Self { units: c.first().and_then(Data::as_i16).unwrap_or(0) } } } impl Manager { diff --git a/yazi-core/src/manager/commands/tab_close.rs b/yazi-core/src/manager/commands/tab_close.rs index 90cf441b..bc0424db 100644 --- a/yazi-core/src/manager/commands/tab_close.rs +++ b/yazi-core/src/manager/commands/tab_close.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::manager::Tabs; @@ -7,9 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { idx: c.take_first_str().and_then(|i| i.parse().ok()).unwrap_or(0) } - } + fn from(c: Cmd) -> Self { Self { idx: c.first().and_then(Data::as_usize).unwrap_or(0) } } } impl From for Opt { diff --git a/yazi-core/src/manager/commands/tab_create.rs b/yazi-core/src/manager/commands/tab_create.rs index 10bf033a..b31814d4 100644 --- a/yazi-core/src/manager/commands/tab_create.rs +++ b/yazi-core/src/manager/commands/tab_create.rs @@ -1,6 +1,6 @@ use yazi_boot::BOOT; use yazi_proxy::AppProxy; -use yazi_shared::{event::Cmd, fs::Url, render}; +use yazi_shared::{event::{Cmd, Data}, fs::Url, render}; use crate::{manager::Tabs, tab::Tab}; @@ -13,11 +13,11 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { - if c.get_bool("current") { + if c.bool("current") { Self { url: Default::default(), current: true } } else { Self { - url: c.take_first_str().map_or_else(|| Url::from(&BOOT.cwd), Url::from), + url: c.take_first().and_then(Data::into_url).unwrap_or_else(|| Url::from(&BOOT.cwd)), current: false, } } diff --git a/yazi-core/src/manager/commands/tab_swap.rs b/yazi-core/src/manager/commands/tab_swap.rs index 4f066047..5068980e 100644 --- a/yazi-core/src/manager/commands/tab_swap.rs +++ b/yazi-core/src/manager/commands/tab_swap.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::manager::Tabs; @@ -7,9 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) } - } + fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } } } impl Tabs { diff --git a/yazi-core/src/manager/commands/tab_switch.rs b/yazi-core/src/manager/commands/tab_switch.rs index ed99615f..b07ee6bd 100644 --- a/yazi-core/src/manager/commands/tab_switch.rs +++ b/yazi-core/src/manager/commands/tab_switch.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::manager::Tabs; @@ -8,11 +8,8 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { - step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0), - relative: c.get_bool("relative"), - } + fn from(c: Cmd) -> Self { + Self { step: c.first().and_then(Data::as_isize).unwrap_or(0), relative: c.bool("relative") } } } diff --git a/yazi-core/src/manager/commands/update_mimetype.rs b/yazi-core/src/manager/commands/update_mimetype.rs index 23c425ec..e9ca16ca 100644 --- a/yazi-core/src/manager/commands/update_mimetype.rs +++ b/yazi-core/src/manager/commands/update_mimetype.rs @@ -12,7 +12,7 @@ impl TryFrom for Opt { type Error = (); fn try_from(mut c: Cmd) -> Result { - Ok(Self { updates: c.take_data("updates").ok_or(())?.into_table_string() }) + Ok(Self { updates: c.take("updates").ok_or(())?.into_table_string() }) } } diff --git a/yazi-core/src/manager/commands/update_paged.rs b/yazi-core/src/manager/commands/update_paged.rs index 378c874e..d9d963b4 100644 --- a/yazi-core/src/manager/commands/update_paged.rs +++ b/yazi-core/src/manager/commands/update_paged.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, fs::Url}; +use yazi_shared::{event::{Cmd, Data}, fs::Url}; use crate::{manager::Manager, tasks::Tasks}; @@ -11,8 +11,8 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { Self { - page: c.take_first_str().and_then(|s| s.parse().ok()), - only_if: c.take_str("only-if").map(Url::from), + page: c.first().and_then(Data::as_usize), + only_if: c.take("only-if").and_then(Data::into_url), } } } diff --git a/yazi-core/src/manager/commands/yank.rs b/yazi-core/src/manager/commands/yank.rs index 676387b9..8fb1a923 100644 --- a/yazi-core/src/manager/commands/yank.rs +++ b/yazi-core/src/manager/commands/yank.rs @@ -8,7 +8,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { cut: c.get_bool("cut") } } + fn from(c: Cmd) -> Self { Self { cut: c.bool("cut") } } } impl Manager { diff --git a/yazi-core/src/notify/commands/tick.rs b/yazi-core/src/notify/commands/tick.rs index b48260e7..d37779c9 100644 --- a/yazi-core/src/notify/commands/tick.rs +++ b/yazi-core/src/notify/commands/tick.rs @@ -1,7 +1,7 @@ use std::time::Duration; use ratatui::layout::Rect; -use yazi_shared::{emit, event::Cmd, Layer}; +use yazi_shared::{emit, event::{Cmd, Data}, Layer}; use crate::notify::Notify; @@ -12,8 +12,8 @@ pub struct Opt { impl TryFrom for Opt { type Error = (); - fn try_from(mut c: Cmd) -> Result { - let interval = c.take_first_str().and_then(|s| s.parse::().ok()).ok_or(())?; + fn try_from(c: Cmd) -> Result { + let interval = c.first().and_then(Data::as_f64).ok_or(())?; if interval < 0.0 { return Err(()); } diff --git a/yazi-core/src/select/commands/arrow.rs b/yazi-core/src/select/commands/arrow.rs index a434cc82..17b1c310 100644 --- a/yazi-core/src/select/commands/arrow.rs +++ b/yazi-core/src/select/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::select::Select; @@ -7,9 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) } - } + fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } } } impl Select { diff --git a/yazi-core/src/select/commands/close.rs b/yazi-core/src/select/commands/close.rs index 01fd9066..69209e14 100644 --- a/yazi-core/src/select/commands/close.rs +++ b/yazi-core/src/select/commands/close.rs @@ -8,7 +8,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { submit: c.get_bool("submit") } } + fn from(c: Cmd) -> Self { Self { submit: c.bool("submit") } } } impl From for Opt { fn from(submit: bool) -> Self { Self { submit } } diff --git a/yazi-core/src/tab/commands/arrow.rs b/yazi-core/src/tab/commands/arrow.rs index bf789cac..1e8a4f7e 100644 --- a/yazi-core/src/tab/commands/arrow.rs +++ b/yazi-core/src/tab/commands/arrow.rs @@ -1,6 +1,6 @@ use yazi_dds::Pubsub; use yazi_proxy::ManagerProxy; -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::{tab::Tab, Step}; @@ -10,7 +10,13 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { - Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or_default() } + let step = match c.take_first() { + Some(Data::Integer(i)) => Step::from(i as isize), + Some(Data::String(s)) => s.parse().unwrap_or_default(), + _ => Step::default(), + }; + + Self { step } } } diff --git a/yazi-core/src/tab/commands/cd.rs b/yazi-core/src/tab/commands/cd.rs index 1990ee75..dfa148af 100644 --- a/yazi-core/src/tab/commands/cd.rs +++ b/yazi-core/src/tab/commands/cd.rs @@ -5,7 +5,7 @@ use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; use yazi_config::popup::InputCfg; use yazi_dds::Pubsub; use yazi_proxy::{CompletionProxy, InputProxy, ManagerProxy, TabProxy}; -use yazi_shared::{event::Cmd, fs::{expand_path, Url}, render, Debounce, InputError}; +use yazi_shared::{event::{Cmd, Data}, fs::{expand_path, Url}, render, Debounce, InputError}; use crate::tab::Tab; @@ -16,12 +16,12 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { - let mut target = Url::from(c.take_first_str().unwrap_or_default()); + let mut target = c.take_first().and_then(Data::into_url).unwrap_or_default(); if target.is_regular() { target.set_path(expand_path(&target)) } - Self { target, interactive: c.get_bool("interactive") } + Self { target, interactive: c.bool("interactive") } } } impl From for Opt { diff --git a/yazi-core/src/tab/commands/filter.rs b/yazi-core/src/tab/commands/filter.rs index 0516a0b6..783b7d85 100644 --- a/yazi-core/src/tab/commands/filter.rs +++ b/yazi-core/src/tab/commands/filter.rs @@ -20,7 +20,7 @@ impl From for Opt { Self { query: c.take_first_str().unwrap_or_default(), case: FilterCase::from(&c), - done: c.get_bool("done"), + done: c.bool("done"), } } } diff --git a/yazi-core/src/tab/commands/find.rs b/yazi-core/src/tab/commands/find.rs index d4b02dc4..3676182b 100644 --- a/yazi-core/src/tab/commands/find.rs +++ b/yazi-core/src/tab/commands/find.rs @@ -16,7 +16,7 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { - Self { query: c.take_first_str(), prev: c.get_bool("previous"), case: FilterCase::from(&c) } + Self { query: c.take_first_str(), prev: c.bool("previous"), case: FilterCase::from(&c) } } } @@ -25,7 +25,7 @@ pub struct ArrowOpt { } impl From for ArrowOpt { - fn from(c: Cmd) -> Self { Self { prev: c.get_bool("previous") } } + fn from(c: Cmd) -> Self { Self { prev: c.bool("previous") } } } impl Tab { diff --git a/yazi-core/src/tab/commands/reveal.rs b/yazi-core/src/tab/commands/reveal.rs index 44f9c38c..ef7f7fec 100644 --- a/yazi-core/src/tab/commands/reveal.rs +++ b/yazi-core/src/tab/commands/reveal.rs @@ -1,5 +1,5 @@ use yazi_proxy::ManagerProxy; -use yazi_shared::{event::Cmd, fs::{expand_path, File, FilesOp, Url}}; +use yazi_shared::{event::{Cmd, Data}, fs::{expand_path, File, FilesOp, Url}}; use crate::tab::Tab; @@ -9,7 +9,7 @@ pub struct Opt { impl From for Opt { fn from(mut c: Cmd) -> Self { - let mut target = Url::from(c.take_first_str().unwrap_or_default()); + let mut target = c.take_first().and_then(Data::into_url).unwrap_or_default(); if target.is_regular() { target.set_path(expand_path(&target)) } diff --git a/yazi-core/src/tab/commands/select.rs b/yazi-core/src/tab/commands/select.rs index 75382923..6de81ccf 100644 --- a/yazi-core/src/tab/commands/select.rs +++ b/yazi-core/src/tab/commands/select.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use yazi_proxy::AppProxy; -use yazi_shared::{event::Cmd, fs::Url, render, render_and}; +use yazi_shared::{event::{Cmd, Data}, fs::Url, render, render_and}; use crate::tab::Tab; @@ -13,7 +13,7 @@ pub struct Opt<'a> { impl<'a> From for Opt<'a> { fn from(mut c: Cmd) -> Self { Self { - url: c.take_str("url").map(|s| Cow::Owned(Url::from(s))), + url: c.take("url").and_then(Data::into_url).map(Cow::Owned), state: match c.take_str("state").as_deref() { Some("true") => Some(true), Some("false") => Some(false), diff --git a/yazi-core/src/tab/commands/shell.rs b/yazi-core/src/tab/commands/shell.rs index a66e525a..4effe5db 100644 --- a/yazi-core/src/tab/commands/shell.rs +++ b/yazi-core/src/tab/commands/shell.rs @@ -17,9 +17,9 @@ impl From for Opt { fn from(mut c: Cmd) -> Self { Self { run: c.take_first_str().unwrap_or_default(), - block: c.get_bool("block"), - orphan: c.get_bool("orphan"), - confirm: c.get_bool("confirm"), + block: c.bool("block"), + orphan: c.bool("orphan"), + confirm: c.bool("confirm"), } } } diff --git a/yazi-core/src/tab/commands/sort.rs b/yazi-core/src/tab/commands/sort.rs index c7aebf77..8b8db845 100644 --- a/yazi-core/src/tab/commands/sort.rs +++ b/yazi-core/src/tab/commands/sort.rs @@ -11,9 +11,9 @@ impl Tab { if let Some(by) = c.take_first_str() { self.conf.sort_by = SortBy::from_str(&by).unwrap_or_default(); } - self.conf.sort_sensitive = c.get_bool("sensitive"); - self.conf.sort_reverse = c.get_bool("reverse"); - self.conf.sort_dir_first = c.get_bool("dir-first"); + self.conf.sort_sensitive = c.bool("sensitive"); + self.conf.sort_reverse = c.bool("reverse"); + self.conf.sort_dir_first = c.bool("dir-first"); self.apply_files_attrs(); ManagerProxy::update_paged(); diff --git a/yazi-core/src/tab/commands/visual_mode.rs b/yazi-core/src/tab/commands/visual_mode.rs index 606a7e62..7e2eb0d2 100644 --- a/yazi-core/src/tab/commands/visual_mode.rs +++ b/yazi-core/src/tab/commands/visual_mode.rs @@ -9,7 +9,7 @@ pub struct Opt { } impl From for Opt { - fn from(c: Cmd) -> Self { Self { unset: c.get_bool("unset") } } + fn from(c: Cmd) -> Self { Self { unset: c.bool("unset") } } } impl Tab { diff --git a/yazi-core/src/tasks/commands/arrow.rs b/yazi-core/src/tasks/commands/arrow.rs index baf1859c..8364bae1 100644 --- a/yazi-core/src/tasks/commands/arrow.rs +++ b/yazi-core/src/tasks/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_shared::{event::Cmd, render}; +use yazi_shared::{event::{Cmd, Data}, render}; use crate::tasks::Tasks; @@ -7,9 +7,7 @@ pub struct Opt { } impl From for Opt { - fn from(mut c: Cmd) -> Self { - Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) } - } + fn from(c: Cmd) -> Self { Self { step: c.first().and_then(Data::as_isize).unwrap_or(0) } } } impl From for Opt { diff --git a/yazi-core/src/which/commands/callback.rs b/yazi-core/src/which/commands/callback.rs index 3970f214..07ae3cfe 100644 --- a/yazi-core/src/which/commands/callback.rs +++ b/yazi-core/src/which/commands/callback.rs @@ -1,6 +1,6 @@ use tokio::sync::mpsc; use tracing::error; -use yazi_shared::event::Cmd; +use yazi_shared::event::{Cmd, Data}; use crate::which::Which; @@ -15,7 +15,7 @@ impl TryFrom for Opt { fn try_from(mut c: Cmd) -> Result { Ok(Self { tx: c.take_any("tx").ok_or(())?, - idx: c.take_first_str().and_then(|s| s.parse().ok()).ok_or(())?, + idx: c.first().and_then(Data::as_usize).ok_or(())?, }) } } diff --git a/yazi-core/src/which/commands/show.rs b/yazi-core/src/which/commands/show.rs index ea68d690..9a5ae428 100644 --- a/yazi-core/src/which/commands/show.rs +++ b/yazi-core/src/which/commands/show.rs @@ -18,7 +18,7 @@ impl TryFrom for Opt { Ok(Self { cands: c.take_any("candidates").unwrap_or_default(), layer: Layer::from_str(&c.take_str("layer").unwrap_or_default())?, - silent: c.get_bool("silent"), + silent: c.bool("silent"), }) } } diff --git a/yazi-dds/src/pubsub.rs b/yazi-dds/src/pubsub.rs index 67291052..1dc4398d 100644 --- a/yazi-dds/src/pubsub.rs +++ b/yazi-dds/src/pubsub.rs @@ -81,8 +81,8 @@ impl Pubsub { } pub fn pub_static(severity: u16, body: Body) { - let (kind, peers) = (body.kind(), PEERS.read()); - if peers.values().any(|c| c.able(kind)) { + let kind = body.kind(); + if REMOTE.read().contains_key(kind) || PEERS.read().values().any(|c| c.able(kind)) { Client::push(body.with_severity(severity)); } } diff --git a/yazi-dds/src/server.rs b/yazi-dds/src/server.rs index 3bb8e19a..b3e51b12 100644 --- a/yazi-dds/src/server.rs +++ b/yazi-dds/src/server.rs @@ -55,8 +55,8 @@ impl Server { let clients = CLIENTS.read(); let clients: Vec<_> = if receiver == 0 { - clients.values().filter(|c| c.id != id && c.able(kind)).collect() - } else if let Some(c) = clients.get(&receiver).filter(|c| c.id != id && c.able(kind)) { + clients.values().filter(|c| c.able(kind)).collect() + } else if let Some(c) = clients.get(&receiver).filter(|c| c.able(kind)) { vec![c] } else { vec![] @@ -72,7 +72,7 @@ impl Server { } line.push('\n'); - clients.into_iter().for_each(|c| _ = c.tx.send(line.clone())); + clients.into_iter().filter(|c| c.id != id).for_each(|c| _ = c.tx.send(line.clone())); } else => break } diff --git a/yazi-fm/src/executor.rs b/yazi-fm/src/executor.rs index b7a056ca..79bc8afa 100644 --- a/yazi-fm/src/executor.rs +++ b/yazi-fm/src/executor.rs @@ -214,7 +214,7 @@ impl<'a> Executor<'a> { on!(forward); if cmd.name.as_str() == "complete" { - return if cmd.get_bool("trigger") { + return if cmd.bool("trigger") { self.app.cx.completion.trigger(cmd) } else { self.app.cx.input.complete(cmd) diff --git a/yazi-plugin/src/opt.rs b/yazi-plugin/src/opt.rs index 87cee4f4..9c6cce32 100644 --- a/yazi-plugin/src/opt.rs +++ b/yazi-plugin/src/opt.rs @@ -20,13 +20,13 @@ impl TryFrom for Opt { bail!("plugin name cannot be empty"); }; - let args = if let Some(s) = c.get_str("args") { + let args = if let Some(s) = c.str("args") { shell_words::split(s)?.into_iter().map(Data::String).collect() } else { c.take_any::>("args").unwrap_or_default() }; - Ok(Self { name, sync: c.get_bool("sync"), args, cb: c.take_any("callback") }) + Ok(Self { name, sync: c.bool("sync"), args, cb: c.take_any("callback") }) } } diff --git a/yazi-shared/src/event/cmd.rs b/yazi-shared/src/event/cmd.rs index 2ccb0890..82685827 100644 --- a/yazi-shared/src/event/cmd.rs +++ b/yazi-shared/src/event/cmd.rs @@ -20,6 +20,7 @@ impl Cmd { } } + // --- With #[inline] pub fn with(mut self, name: impl ToString, value: impl ToString) -> Self { self.args.insert(name.to_string(), Data::String(value.to_string())); @@ -44,22 +45,33 @@ impl Cmd { self } + // --- Get #[inline] - pub fn get_str(&self, name: &str) -> Option<&str> { self.args.get(name).and_then(Data::as_str) } + pub fn get(&self, name: &str) -> Option<&Data> { self.args.get(name) } #[inline] - pub fn get_bool(&self, name: &str) -> bool { + pub fn str(&self, name: &str) -> Option<&str> { self.args.get(name).and_then(Data::as_str) } + + #[inline] + pub fn bool(&self, name: &str) -> bool { self.args.get(name).and_then(Data::as_bool).unwrap_or(false) } #[inline] - pub fn take_data(&mut self, name: &str) -> Option { self.args.remove(name) } + pub fn first(&self) -> Option<&Data> { self.args.get("0") } + + // --- Take + #[inline] + pub fn take(&mut self, name: &str) -> Option { self.args.remove(name) } #[inline] pub fn take_str(&mut self, name: &str) -> Option { if let Some(Data::String(s)) = self.args.remove(name) { Some(s) } else { None } } + #[inline] + pub fn take_first(&mut self) -> Option { self.args.remove("0") } + #[inline] pub fn take_first_str(&mut self) -> Option { if let Some(Data::String(s)) = self.args.remove("0") { Some(s) } else { None } @@ -70,6 +82,7 @@ impl Cmd { self.args.remove(name).and_then(|d| d.into_any()) } + // --- Clone pub fn shallow_clone(&self) -> Self { Self { name: self.name.clone(), diff --git a/yazi-shared/src/event/data.rs b/yazi-shared/src/event/data.rs index f545f24e..927b7567 100644 --- a/yazi-shared/src/event/data.rs +++ b/yazi-shared/src/event/data.rs @@ -14,7 +14,7 @@ pub enum Data { Number(f64), String(String), Table(HashMap), - #[serde(skip)] + #[serde(skip_deserializing)] Url(Url), #[serde(skip)] Any(Box), @@ -53,6 +53,15 @@ impl Data { } } + #[inline] + pub fn into_url(self) -> Option { + match self { + Data::String(s) => Some(Url::from(s)), + Data::Url(u) => Some(u), + _ => None, + } + } + pub fn into_table_string(self) -> HashMap { let Self::Table(table) = self else { return Default::default(); @@ -86,7 +95,7 @@ pub enum DataKey { Integer(i64), Number(OrderedFloat), String(String), - #[serde(skip)] + #[serde(skip_deserializing)] Url(Url), } @@ -94,3 +103,40 @@ impl DataKey { #[inline] pub fn is_numeric(&self) -> bool { matches!(self, Self::Integer(_) | Self::Number(_)) } } + +// --- Macros +macro_rules! impl_integer_as { + ($t:ty, $name:ident) => { + impl Data { + #[inline] + pub fn $name(&self) -> Option<$t> { + match self { + Data::Integer(i) => <$t>::try_from(*i).ok(), + Data::String(s) => s.parse().ok(), + _ => None, + } + } + } + }; +} + +macro_rules! impl_number_as { + ($t:ty, $name:ident) => { + impl Data { + #[inline] + pub fn $name(&self) -> Option<$t> { + match self { + Data::Number(n) => <$t>::try_from(*n).ok(), + Data::String(s) => s.parse().ok(), + _ => None, + } + } + } + }; +} + +impl_integer_as!(usize, as_usize); +impl_integer_as!(isize, as_isize); +impl_integer_as!(i16, as_i16); + +impl_number_as!(f64, as_f64);