From dee464d2614192478e513ae4775d6d83105171a1 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 27 Nov 2023 21:05:29 +0800 Subject: [PATCH] .. --- yazi-config/src/keymap/control.rs | 5 +- yazi-config/src/keymap/exec.rs | 122 +++++-------------- yazi-config/src/keymap/mod.rs | 1 + yazi-core/src/completion/commands/arrow.rs | 2 +- yazi-core/src/completion/commands/close.rs | 3 +- yazi-core/src/completion/commands/show.rs | 2 +- yazi-core/src/completion/commands/trigger.rs | 3 +- yazi-core/src/event.rs | 4 +- yazi-core/src/help/commands/arrow.rs | 2 +- yazi-core/src/help/commands/escape.rs | 2 +- yazi-core/src/help/commands/filter.rs | 3 +- yazi-core/src/input/commands/backspace.rs | 2 +- yazi-core/src/input/commands/backward.rs | 3 +- yazi-core/src/input/commands/close.rs | 3 +- yazi-core/src/input/commands/complete.rs | 3 +- yazi-core/src/input/commands/delete.rs | 2 +- yazi-core/src/input/commands/escape.rs | 2 +- yazi-core/src/input/commands/forward.rs | 3 +- yazi-core/src/input/commands/insert.rs | 2 +- yazi-core/src/input/commands/kill.rs | 3 +- yazi-core/src/input/commands/move_.rs | 2 +- yazi-core/src/input/commands/paste.rs | 2 +- yazi-core/src/input/commands/redo.rs | 2 +- yazi-core/src/input/commands/type_.rs | 3 +- yazi-core/src/input/commands/undo.rs | 2 +- yazi-core/src/input/commands/visual.rs | 2 +- yazi-core/src/input/commands/yank.rs | 2 +- yazi-core/src/manager/commands/close.rs | 2 +- yazi-core/src/manager/commands/create.rs | 4 +- yazi-core/src/manager/commands/hover.rs | 2 +- yazi-core/src/manager/commands/link.rs | 2 +- yazi-core/src/manager/commands/open.rs | 4 +- yazi-core/src/manager/commands/paste.rs | 2 +- yazi-core/src/manager/commands/peek.rs | 4 +- yazi-core/src/manager/commands/quit.rs | 3 +- yazi-core/src/manager/commands/refresh.rs | 2 +- yazi-core/src/manager/commands/remove.rs | 2 +- yazi-core/src/manager/commands/rename.rs | 4 +- yazi-core/src/manager/commands/suspend.rs | 2 +- yazi-core/src/manager/commands/tab_close.rs | 2 +- yazi-core/src/manager/commands/tab_create.rs | 2 +- yazi-core/src/manager/commands/tab_swap.rs | 2 +- yazi-core/src/manager/commands/tab_switch.rs | 2 +- yazi-core/src/manager/commands/yank.rs | 2 +- yazi-core/src/select/commands/arrow.rs | 2 +- yazi-core/src/select/commands/close.rs | 2 +- yazi-core/src/tab/commands/arrow.rs | 2 +- yazi-core/src/tab/commands/backstack.rs | 2 +- yazi-core/src/tab/commands/cd.rs | 4 +- yazi-core/src/tab/commands/copy.rs | 2 +- yazi-core/src/tab/commands/enter.rs | 2 +- yazi-core/src/tab/commands/escape.rs | 2 +- yazi-core/src/tab/commands/find.rs | 4 +- yazi-core/src/tab/commands/hidden.rs | 2 +- yazi-core/src/tab/commands/jump.rs | 2 +- yazi-core/src/tab/commands/leave.rs | 2 +- yazi-core/src/tab/commands/linemode.rs | 2 +- yazi-core/src/tab/commands/reveal.rs | 2 +- yazi-core/src/tab/commands/search.rs | 3 +- yazi-core/src/tab/commands/select.rs | 2 +- yazi-core/src/tab/commands/shell.rs | 3 +- yazi-core/src/tab/commands/sort.rs | 3 +- yazi-core/src/tab/commands/visual_mode.rs | 2 +- yazi-core/src/tasks/commands/arrow.rs | 2 +- yazi-core/src/tasks/commands/cancel.rs | 2 +- yazi-core/src/tasks/commands/inspect.rs | 2 +- yazi-core/src/tasks/commands/toggle.rs | 2 +- yazi-fm/src/app.rs | 4 +- yazi-fm/src/executor.rs | 4 +- yazi-shared/src/event.rs | 54 ++++++++ yazi-shared/src/lib.rs | 2 + 71 files changed, 173 insertions(+), 174 deletions(-) create mode 100644 yazi-shared/src/event.rs diff --git a/yazi-config/src/keymap/control.rs b/yazi-config/src/keymap/control.rs index 45992ec0..dad67651 100644 --- a/yazi-config/src/keymap/control.rs +++ b/yazi-config/src/keymap/control.rs @@ -1,13 +1,14 @@ use std::borrow::Cow; use serde::Deserialize; +use yazi_shared::Exec; -use super::{Exec, Key}; +use super::Key; #[derive(Debug, Deserialize)] pub struct Control { pub on: Vec, - #[serde(deserialize_with = "Exec::deserialize")] + #[serde(deserialize_with = "super::exec_deserialize")] pub exec: Vec, pub desc: Option, } diff --git a/yazi-config/src/keymap/exec.rs b/yazi-config/src/keymap/exec.rs index f83d6bb6..3d3d7cfd 100644 --- a/yazi-config/src/keymap/exec.rs +++ b/yazi-config/src/keymap/exec.rs @@ -1,26 +1,22 @@ -use std::{any::Any, collections::BTreeMap, fmt::{self, Debug, Display}}; +use std::fmt; -use anyhow::bail; +use anyhow::{bail, Result}; use serde::{de::{self, Visitor}, Deserializer}; +use yazi_shared::Exec; -#[derive(Debug, Default)] -pub struct Exec { - pub cmd: String, - pub args: Vec, - pub named: BTreeMap, - pub data: Option>, -} +pub(super) fn exec_deserialize<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + struct ExecVisitor; -impl TryFrom<&str> for Exec { - type Error = anyhow::Error; - - fn try_from(s: &str) -> Result { + fn parse(s: &str) -> Result { let s = shell_words::split(s)?; if s.is_empty() { bail!("`exec` cannot be empty"); } - let mut exec = Self { cmd: s[0].clone(), ..Default::default() }; + let mut exec = Exec { cmd: s[0].clone(), ..Default::default() }; for arg in s.into_iter().skip(1) { if arg.starts_with("--") { let mut arg = arg.splitn(2, '='); @@ -33,86 +29,32 @@ impl TryFrom<&str> for Exec { } Ok(exec) } -} -impl Display for Exec { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.cmd)?; - if !self.args.is_empty() { - write!(f, " {}", self.args.join(" "))?; - } - for (k, v) in &self.named { - write!(f, " --{k}")?; - if !v.is_empty() { - write!(f, "={v}")?; - } - } - Ok(()) - } -} + impl<'de> Visitor<'de> for ExecVisitor { + type Value = Vec; -impl Exec { - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - struct ExecVisitor; - - impl<'de> Visitor<'de> for ExecVisitor { - type Value = Vec; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a exec string, e.g. tab_switch 0") - } - - fn visit_seq(self, mut seq: A) -> Result - where - A: de::SeqAccess<'de>, - { - let mut execs = Vec::new(); - while let Some(value) = &seq.next_element::()? { - execs.push(Exec::try_from(value.as_str()).map_err(de::Error::custom)?); - } - Ok(execs) - } - - fn visit_str(self, value: &str) -> Result - where - E: de::Error, - { - Ok(vec![Exec::try_from(value).map_err(de::Error::custom)?]) - } + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("a exec string, e.g. `tab_switch 0`") } - deserializer.deserialize_any(ExecVisitor) - } -} - -impl Exec { - #[inline] - pub fn call(cwd: &str, args: Vec) -> Self { - Exec { cmd: cwd.to_owned(), args, ..Default::default() } - } - - #[inline] - pub fn call_named(cwd: &str, named: BTreeMap) -> Self { - Exec { cmd: cwd.to_owned(), named, ..Default::default() } - } - - #[inline] - pub fn vec(self) -> Vec { vec![self] } - - #[inline] - pub fn with(mut self, name: impl ToString, value: impl ToString) -> Self { - self.named.insert(name.to_string(), value.to_string()); - self - } - - #[inline] - pub fn with_bool(mut self, name: impl ToString, state: bool) -> Self { - if state { - self.named.insert(name.to_string(), Default::default()); + fn visit_seq(self, mut seq: A) -> Result + where + A: de::SeqAccess<'de>, + { + let mut execs = Vec::new(); + while let Some(value) = &seq.next_element::()? { + execs.push(parse(value).map_err(de::Error::custom)?); + } + Ok(execs) + } + + fn visit_str(self, value: &str) -> Result + where + E: de::Error, + { + Ok(vec![parse(value).map_err(de::Error::custom)?]) } - self } + + deserializer.deserialize_any(ExecVisitor) } diff --git a/yazi-config/src/keymap/mod.rs b/yazi-config/src/keymap/mod.rs index 4ca907b5..c531882a 100644 --- a/yazi-config/src/keymap/mod.rs +++ b/yazi-config/src/keymap/mod.rs @@ -4,6 +4,7 @@ mod key; mod keymap; pub use control::*; +#[allow(unused_imports)] pub use exec::*; pub use key::*; pub use keymap::*; diff --git a/yazi-core/src/completion/commands/arrow.rs b/yazi-core/src/completion/commands/arrow.rs index c5296585..cdf32ccf 100644 --- a/yazi-core/src/completion/commands/arrow.rs +++ b/yazi-core/src/completion/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::completion::Completion; diff --git a/yazi-core/src/completion/commands/close.rs b/yazi-core/src/completion/commands/close.rs index 929dae1c..5bc8d2cd 100644 --- a/yazi-core/src/completion/commands/close.rs +++ b/yazi-core/src/completion/commands/close.rs @@ -1,5 +1,4 @@ -use yazi_config::keymap::Exec; -use yazi_shared::Layer; +use yazi_shared::{Exec, Layer}; use crate::{completion::Completion, emit, input::Input}; diff --git a/yazi-core/src/completion/commands/show.rs b/yazi-core/src/completion/commands/show.rs index 102df4bf..ea2ef2f8 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_config::keymap::Exec; +use yazi_shared::Exec; use crate::completion::Completion; diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/completion/commands/trigger.rs index 82fa9fbe..8536cb02 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/completion/commands/trigger.rs @@ -1,8 +1,7 @@ use std::{mem, path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR}}; use tokio::fs; -use yazi_config::keymap::Exec; -use yazi_shared::Layer; +use yazi_shared::{Exec, Layer}; use crate::{completion::Completion, emit}; diff --git a/yazi-core/src/event.rs b/yazi-core/src/event.rs index fa17ef2d..e8b88e5c 100644 --- a/yazi-core/src/event.rs +++ b/yazi-core/src/event.rs @@ -3,8 +3,8 @@ use std::{collections::BTreeMap, ffi::OsString}; use anyhow::Result; use crossterm::event::KeyEvent; use tokio::sync::{mpsc::{self, UnboundedSender}, oneshot}; -use yazi_config::{keymap::Exec, open::Opener, popup::{InputOpt, SelectOpt}}; -use yazi_shared::{fs::Url, InputError, Layer, RoCell}; +use yazi_config::{open::Opener, popup::{InputOpt, SelectOpt}}; +use yazi_shared::{fs::Url, Exec, InputError, Layer, RoCell}; use super::files::FilesOp; use crate::{preview::PreviewLock, tasks::TasksProgress}; diff --git a/yazi-core/src/help/commands/arrow.rs b/yazi-core/src/help/commands/arrow.rs index af4ca625..b95fb81a 100644 --- a/yazi-core/src/help/commands/arrow.rs +++ b/yazi-core/src/help/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::help::Help; diff --git a/yazi-core/src/help/commands/escape.rs b/yazi-core/src/help/commands/escape.rs index e27cbec3..be90b6ed 100644 --- a/yazi-core/src/help/commands/escape.rs +++ b/yazi-core/src/help/commands/escape.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::help::Help; diff --git a/yazi-core/src/help/commands/filter.rs b/yazi-core/src/help/commands/filter.rs index 0721bd43..df5e7934 100644 --- a/yazi-core/src/help/commands/filter.rs +++ b/yazi-core/src/help/commands/filter.rs @@ -1,4 +1,5 @@ -use yazi_config::{keymap::Exec, popup::{Offset, Origin, Position}}; +use yazi_config::popup::{Offset, Origin, Position}; +use yazi_shared::Exec; use crate::{help::Help, input::Input}; diff --git a/yazi-core/src/input/commands/backspace.rs b/yazi-core/src/input/commands/backspace.rs index 8828796a..521d622e 100644 --- a/yazi-core/src/input/commands/backspace.rs +++ b/yazi-core/src/input/commands/backspace.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::Input; diff --git a/yazi-core/src/input/commands/backward.rs b/yazi-core/src/input/commands/backward.rs index 4105e414..93fed9d1 100644 --- a/yazi-core/src/input/commands/backward.rs +++ b/yazi-core/src/input/commands/backward.rs @@ -1,5 +1,4 @@ -use yazi_config::keymap::Exec; -use yazi_shared::CharKind; +use yazi_shared::{CharKind, Exec}; use crate::input::Input; diff --git a/yazi-core/src/input/commands/close.rs b/yazi-core/src/input/commands/close.rs index 563d2630..861a16a2 100644 --- a/yazi-core/src/input/commands/close.rs +++ b/yazi-core/src/input/commands/close.rs @@ -1,5 +1,4 @@ -use yazi_config::keymap::Exec; -use yazi_shared::InputError; +use yazi_shared::{Exec, InputError}; use crate::{completion::Completion, input::Input}; diff --git a/yazi-core/src/input/commands/complete.rs b/yazi-core/src/input/commands/complete.rs index 612f18d1..a3cc7257 100644 --- a/yazi-core/src/input/commands/complete.rs +++ b/yazi-core/src/input/commands/complete.rs @@ -1,7 +1,6 @@ use std::path::MAIN_SEPARATOR; -use yazi_config::keymap::Exec; -use yazi_shared::Layer; +use yazi_shared::{Exec, Layer}; use crate::{emit, input::Input}; diff --git a/yazi-core/src/input/commands/delete.rs b/yazi-core/src/input/commands/delete.rs index b36e6905..d87cd43b 100644 --- a/yazi-core/src/input/commands/delete.rs +++ b/yazi-core/src/input/commands/delete.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::{op::InputOp, Input}; diff --git a/yazi-core/src/input/commands/escape.rs b/yazi-core/src/input/commands/escape.rs index 392dd179..747f23ab 100644 --- a/yazi-core/src/input/commands/escape.rs +++ b/yazi-core/src/input/commands/escape.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{completion::Completion, input::{op::InputOp, Input, InputMode}}; diff --git a/yazi-core/src/input/commands/forward.rs b/yazi-core/src/input/commands/forward.rs index 523f1f63..3d85ffb6 100644 --- a/yazi-core/src/input/commands/forward.rs +++ b/yazi-core/src/input/commands/forward.rs @@ -1,5 +1,4 @@ -use yazi_config::keymap::Exec; -use yazi_shared::CharKind; +use yazi_shared::{CharKind, Exec}; use crate::input::{op::InputOp, Input}; diff --git a/yazi-core/src/input/commands/insert.rs b/yazi-core/src/input/commands/insert.rs index ba7b87f0..207ce5de 100644 --- a/yazi-core/src/input/commands/insert.rs +++ b/yazi-core/src/input/commands/insert.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::{op::InputOp, Input, InputMode}; diff --git a/yazi-core/src/input/commands/kill.rs b/yazi-core/src/input/commands/kill.rs index 918943a9..262703eb 100644 --- a/yazi-core/src/input/commands/kill.rs +++ b/yazi-core/src/input/commands/kill.rs @@ -1,7 +1,6 @@ use std::ops::RangeBounds; -use yazi_config::keymap::Exec; -use yazi_shared::CharKind; +use yazi_shared::{CharKind, Exec}; use crate::input::Input; diff --git a/yazi-core/src/input/commands/move_.rs b/yazi-core/src/input/commands/move_.rs index 4bccecd4..1cb527c1 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_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::{op::InputOp, snap::InputSnap, Input}; diff --git a/yazi-core/src/input/commands/paste.rs b/yazi-core/src/input/commands/paste.rs index 5145845d..b53b8e16 100644 --- a/yazi-core/src/input/commands/paste.rs +++ b/yazi-core/src/input/commands/paste.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{external, input::{op::InputOp, Input}}; diff --git a/yazi-core/src/input/commands/redo.rs b/yazi-core/src/input/commands/redo.rs index 1722d664..685bbb2b 100644 --- a/yazi-core/src/input/commands/redo.rs +++ b/yazi-core/src/input/commands/redo.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::Input; diff --git a/yazi-core/src/input/commands/type_.rs b/yazi-core/src/input/commands/type_.rs index db9296df..08162935 100644 --- a/yazi-core/src/input/commands/type_.rs +++ b/yazi-core/src/input/commands/type_.rs @@ -1,4 +1,5 @@ -use yazi_config::keymap::{Exec, Key}; +use yazi_config::keymap::Key; +use yazi_shared::Exec; use crate::input::{Input, InputMode}; diff --git a/yazi-core/src/input/commands/undo.rs b/yazi-core/src/input/commands/undo.rs index 6ef8d81e..bc6a7f68 100644 --- a/yazi-core/src/input/commands/undo.rs +++ b/yazi-core/src/input/commands/undo.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::{Input, InputMode}; diff --git a/yazi-core/src/input/commands/visual.rs b/yazi-core/src/input/commands/visual.rs index 81d1d7c0..1a1a3c17 100644 --- a/yazi-core/src/input/commands/visual.rs +++ b/yazi-core/src/input/commands/visual.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::{op::InputOp, Input, InputMode}; diff --git a/yazi-core/src/input/commands/yank.rs b/yazi-core/src/input/commands/yank.rs index e8c32c6c..76dbf6b2 100644 --- a/yazi-core/src/input/commands/yank.rs +++ b/yazi-core/src/input/commands/yank.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::input::{op::InputOp, Input}; diff --git a/yazi-core/src/manager/commands/close.rs b/yazi-core/src/manager/commands/close.rs index 45eaa08c..ced8761c 100644 --- a/yazi-core/src/manager/commands/close.rs +++ b/yazi-core/src/manager/commands/close.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tasks::Tasks}; diff --git a/yazi-core/src/manager/commands/create.rs b/yazi-core/src/manager/commands/create.rs index 0eb94393..664a5b15 100644 --- a/yazi-core/src/manager/commands/create.rs +++ b/yazi-core/src/manager/commands/create.rs @@ -1,8 +1,8 @@ use std::path::{PathBuf, MAIN_SEPARATOR}; use tokio::fs; -use yazi_config::{keymap::Exec, popup::InputOpt}; -use yazi_shared::fs::Url; +use yazi_config::popup::InputOpt; +use yazi_shared::{fs::Url, Exec}; use crate::{emit, files::{File, FilesOp}, manager::Manager}; diff --git a/yazi-core/src/manager/commands/hover.rs b/yazi-core/src/manager/commands/hover.rs index 42214f39..f52e80d8 100644 --- a/yazi-core/src/manager/commands/hover.rs +++ b/yazi-core/src/manager/commands/hover.rs @@ -1,6 +1,6 @@ use std::collections::BTreeSet; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use yazi_shared::{fs::Url, Layer}; use crate::{emit, manager::Manager}; diff --git a/yazi-core/src/manager/commands/link.rs b/yazi-core/src/manager/commands/link.rs index 74be120e..a555df8d 100644 --- a/yazi-core/src/manager/commands/link.rs +++ b/yazi-core/src/manager/commands/link.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tasks::Tasks}; diff --git a/yazi-core/src/manager/commands/open.rs b/yazi-core/src/manager/commands/open.rs index d436c3f6..b2e601d8 100644 --- a/yazi-core/src/manager/commands/open.rs +++ b/yazi-core/src/manager/commands/open.rs @@ -1,7 +1,7 @@ use std::ffi::OsString; -use yazi_config::{keymap::Exec, popup::SelectOpt, OPEN}; -use yazi_shared::MIME_DIR; +use yazi_config::{popup::SelectOpt, OPEN}; +use yazi_shared::{Exec, MIME_DIR}; use crate::{emit, external, manager::Manager}; diff --git a/yazi-core/src/manager/commands/paste.rs b/yazi-core/src/manager/commands/paste.rs index 31877e3a..4c51d942 100644 --- a/yazi-core/src/manager/commands/paste.rs +++ b/yazi-core/src/manager/commands/paste.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tasks::Tasks}; diff --git a/yazi-core/src/manager/commands/peek.rs b/yazi-core/src/manager/commands/peek.rs index 3d77b874..a101e959 100644 --- a/yazi-core/src/manager/commands/peek.rs +++ b/yazi-core/src/manager/commands/peek.rs @@ -1,5 +1,5 @@ -use yazi_config::{keymap::Exec, MANAGER}; -use yazi_shared::{fs::Url, Layer, MIME_DIR}; +use yazi_config::MANAGER; +use yazi_shared::{fs::Url, Exec, Layer, MIME_DIR}; use crate::{emit, manager::Manager}; diff --git a/yazi-core/src/manager/commands/quit.rs b/yazi-core/src/manager/commands/quit.rs index e4f70fc0..4745953b 100644 --- a/yazi-core/src/manager/commands/quit.rs +++ b/yazi-core/src/manager/commands/quit.rs @@ -1,4 +1,5 @@ -use yazi_config::{keymap::Exec, popup::InputOpt}; +use yazi_config::popup::InputOpt; +use yazi_shared::Exec; use crate::{emit, manager::Manager, tasks::Tasks}; diff --git a/yazi-core/src/manager/commands/refresh.rs b/yazi-core/src/manager/commands/refresh.rs index 560e9ee5..a2b7c712 100644 --- a/yazi-core/src/manager/commands/refresh.rs +++ b/yazi-core/src/manager/commands/refresh.rs @@ -1,6 +1,6 @@ use std::env; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use yazi_shared::Layer; use crate::{emit, manager::Manager}; diff --git a/yazi-core/src/manager/commands/remove.rs b/yazi-core/src/manager/commands/remove.rs index 7f9c4e6b..d9fee10e 100644 --- a/yazi-core/src/manager/commands/remove.rs +++ b/yazi-core/src/manager/commands/remove.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tasks::Tasks}; diff --git a/yazi-core/src/manager/commands/rename.rs b/yazi-core/src/manager/commands/rename.rs index 482dd9aa..dcc0faa6 100644 --- a/yazi-core/src/manager/commands/rename.rs +++ b/yazi-core/src/manager/commands/rename.rs @@ -2,8 +2,8 @@ use std::{collections::BTreeMap, ffi::OsStr, io::{stdout, BufWriter, Write}, pat use anyhow::{anyhow, bail, Result}; use tokio::{fs::{self, OpenOptions}, io::{stdin, AsyncReadExt, AsyncWriteExt}}; -use yazi_config::{keymap::Exec, popup::InputOpt, OPEN, PREVIEW}; -use yazi_shared::{fs::{max_common_root, Url}, term::Term, Defer}; +use yazi_config::{popup::InputOpt, OPEN, PREVIEW}; +use yazi_shared::{fs::{max_common_root, Url}, term::Term, Defer, Exec}; use crate::{emit, external::{self, ShellOpt}, files::{File, FilesOp}, manager::Manager, Event, BLOCKER}; diff --git a/yazi-core/src/manager/commands/suspend.rs b/yazi-core/src/manager/commands/suspend.rs index f88bed06..d7fafb93 100644 --- a/yazi-core/src/manager/commands/suspend.rs +++ b/yazi-core/src/manager/commands/suspend.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::manager::Manager; diff --git a/yazi-core/src/manager/commands/tab_close.rs b/yazi-core/src/manager/commands/tab_close.rs index addd3e7c..e6d8e350 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_config::keymap::Exec; +use yazi_shared::Exec; use crate::manager::Tabs; diff --git a/yazi-core/src/manager/commands/tab_create.rs b/yazi-core/src/manager/commands/tab_create.rs index 2dd072f7..593c514d 100644 --- a/yazi-core/src/manager/commands/tab_create.rs +++ b/yazi-core/src/manager/commands/tab_create.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use yazi_shared::fs::Url; use crate::{manager::Tabs, tab::Tab}; diff --git a/yazi-core/src/manager/commands/tab_swap.rs b/yazi-core/src/manager/commands/tab_swap.rs index 00a52207..e575b160 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_config::keymap::Exec; +use yazi_shared::Exec; use crate::manager::Tabs; diff --git a/yazi-core/src/manager/commands/tab_switch.rs b/yazi-core/src/manager/commands/tab_switch.rs index 27c7bafa..a0bff3cd 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_config::keymap::Exec; +use yazi_shared::Exec; use crate::manager::Tabs; diff --git a/yazi-core/src/manager/commands/yank.rs b/yazi-core/src/manager/commands/yank.rs index bd43646a..715f9157 100644 --- a/yazi-core/src/manager/commands/yank.rs +++ b/yazi-core/src/manager/commands/yank.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::manager::Manager; diff --git a/yazi-core/src/select/commands/arrow.rs b/yazi-core/src/select/commands/arrow.rs index e06a3ddc..4b4c4495 100644 --- a/yazi-core/src/select/commands/arrow.rs +++ b/yazi-core/src/select/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::select::Select; diff --git a/yazi-core/src/select/commands/close.rs b/yazi-core/src/select/commands/close.rs index edc575df..70f575c8 100644 --- a/yazi-core/src/select/commands/close.rs +++ b/yazi-core/src/select/commands/close.rs @@ -1,5 +1,5 @@ use anyhow::anyhow; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::select::Select; diff --git a/yazi-core/src/tab/commands/arrow.rs b/yazi-core/src/tab/commands/arrow.rs index 6119f067..e110bb89 100644 --- a/yazi-core/src/tab/commands/arrow.rs +++ b/yazi-core/src/tab/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tab::Tab, Step}; diff --git a/yazi-core/src/tab/commands/backstack.rs b/yazi-core/src/tab/commands/backstack.rs index c565ca51..bc1215e8 100644 --- a/yazi-core/src/tab/commands/backstack.rs +++ b/yazi-core/src/tab/commands/backstack.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tab::Tab; diff --git a/yazi-core/src/tab/commands/cd.rs b/yazi-core/src/tab/commands/cd.rs index d63cf0a9..7592aa8c 100644 --- a/yazi-core/src/tab/commands/cd.rs +++ b/yazi-core/src/tab/commands/cd.rs @@ -2,8 +2,8 @@ use std::{mem, time::Duration}; use tokio::{fs, pin}; use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; -use yazi_config::{keymap::Exec, popup::InputOpt}; -use yazi_shared::{fs::{expand_path, Url}, Debounce, InputError, Layer}; +use yazi_config::popup::InputOpt; +use yazi_shared::{fs::{expand_path, Url}, Debounce, Exec, InputError, Layer}; use crate::{completion::Completion, emit, manager::Manager, tab::Tab}; diff --git a/yazi-core/src/tab/commands/copy.rs b/yazi-core/src/tab/commands/copy.rs index bc864585..27d54ce7 100644 --- a/yazi-core/src/tab/commands/copy.rs +++ b/yazi-core/src/tab/commands/copy.rs @@ -1,6 +1,6 @@ use std::ffi::{OsStr, OsString}; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{external, tab::Tab}; diff --git a/yazi-core/src/tab/commands/enter.rs b/yazi-core/src/tab/commands/enter.rs index 65f9ce24..e275a8f4 100644 --- a/yazi-core/src/tab/commands/enter.rs +++ b/yazi-core/src/tab/commands/enter.rs @@ -1,6 +1,6 @@ use std::mem; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tab::Tab}; diff --git a/yazi-core/src/tab/commands/escape.rs b/yazi-core/src/tab/commands/escape.rs index e8e7eca5..997656dc 100644 --- a/yazi-core/src/tab/commands/escape.rs +++ b/yazi-core/src/tab/commands/escape.rs @@ -1,5 +1,5 @@ use bitflags::bitflags; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tab::{Mode, Tab}; diff --git a/yazi-core/src/tab/commands/find.rs b/yazi-core/src/tab/commands/find.rs index 553f177f..9e425c24 100644 --- a/yazi-core/src/tab/commands/find.rs +++ b/yazi-core/src/tab/commands/find.rs @@ -2,8 +2,8 @@ use std::time::Duration; use tokio::pin; use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; -use yazi_config::{keymap::Exec, popup::InputOpt}; -use yazi_shared::{Debounce, InputError, Layer}; +use yazi_config::popup::InputOpt; +use yazi_shared::{Debounce, Exec, InputError, Layer}; use crate::{emit, tab::{Finder, FinderCase, Tab}}; diff --git a/yazi-core/src/tab/commands/hidden.rs b/yazi-core/src/tab/commands/hidden.rs index f66b3cba..4b919b76 100644 --- a/yazi-core/src/tab/commands/hidden.rs +++ b/yazi-core/src/tab/commands/hidden.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tab::Tab}; diff --git a/yazi-core/src/tab/commands/jump.rs b/yazi-core/src/tab/commands/jump.rs index 66b309f4..cbb9f20d 100644 --- a/yazi-core/src/tab/commands/jump.rs +++ b/yazi-core/src/tab/commands/jump.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use yazi_shared::{fs::ends_with_slash, Defer}; use crate::{emit, external::{self, FzfOpt, ZoxideOpt}, tab::Tab, Event, BLOCKER}; diff --git a/yazi-core/src/tab/commands/leave.rs b/yazi-core/src/tab/commands/leave.rs index f07480eb..7c1ec497 100644 --- a/yazi-core/src/tab/commands/leave.rs +++ b/yazi-core/src/tab/commands/leave.rs @@ -1,6 +1,6 @@ use std::mem; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::{manager::Manager, tab::Tab}; diff --git a/yazi-core/src/tab/commands/linemode.rs b/yazi-core/src/tab/commands/linemode.rs index 36feab95..47ca8c16 100644 --- a/yazi-core/src/tab/commands/linemode.rs +++ b/yazi-core/src/tab/commands/linemode.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tab::Tab; diff --git a/yazi-core/src/tab/commands/reveal.rs b/yazi-core/src/tab/commands/reveal.rs index b33161d1..631e3a56 100644 --- a/yazi-core/src/tab/commands/reveal.rs +++ b/yazi-core/src/tab/commands/reveal.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use yazi_shared::{fs::{expand_path, Url}, Layer}; use crate::{emit, files::{File, FilesOp}, manager::Manager, tab::Tab}; diff --git a/yazi-core/src/tab/commands/search.rs b/yazi-core/src/tab/commands/search.rs index 6d60aafb..c6f70298 100644 --- a/yazi-core/src/tab/commands/search.rs +++ b/yazi-core/src/tab/commands/search.rs @@ -3,7 +3,8 @@ use std::{mem, time::Duration}; use anyhow::bail; use tokio::pin; use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; -use yazi_config::{keymap::Exec, popup::InputOpt}; +use yazi_config::popup::InputOpt; +use yazi_shared::Exec; use crate::{emit, external, files::FilesOp, manager::Manager, tab::Tab}; diff --git a/yazi-core/src/tab/commands/select.rs b/yazi-core/src/tab/commands/select.rs index 5d7cac87..bd034714 100644 --- a/yazi-core/src/tab/commands/select.rs +++ b/yazi-core/src/tab/commands/select.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tab::Tab; diff --git a/yazi-core/src/tab/commands/shell.rs b/yazi-core/src/tab/commands/shell.rs index fb309e72..6cf21b04 100644 --- a/yazi-core/src/tab/commands/shell.rs +++ b/yazi-core/src/tab/commands/shell.rs @@ -1,4 +1,5 @@ -use yazi_config::{keymap::Exec, open::Opener, popup::InputOpt}; +use yazi_config::{open::Opener, popup::InputOpt}; +use yazi_shared::Exec; use crate::{emit, tab::Tab}; diff --git a/yazi-core/src/tab/commands/sort.rs b/yazi-core/src/tab/commands/sort.rs index 73793607..1413fd09 100644 --- a/yazi-core/src/tab/commands/sort.rs +++ b/yazi-core/src/tab/commands/sort.rs @@ -1,6 +1,7 @@ use std::str::FromStr; -use yazi_config::{keymap::Exec, manager::SortBy}; +use yazi_config::manager::SortBy; +use yazi_shared::Exec; use crate::tab::Tab; diff --git a/yazi-core/src/tab/commands/visual_mode.rs b/yazi-core/src/tab/commands/visual_mode.rs index 15ac386b..34f096c9 100644 --- a/yazi-core/src/tab/commands/visual_mode.rs +++ b/yazi-core/src/tab/commands/visual_mode.rs @@ -1,6 +1,6 @@ use std::collections::BTreeSet; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tab::{Mode, Tab}; diff --git a/yazi-core/src/tasks/commands/arrow.rs b/yazi-core/src/tasks/commands/arrow.rs index 22333208..f734c78b 100644 --- a/yazi-core/src/tasks/commands/arrow.rs +++ b/yazi-core/src/tasks/commands/arrow.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tasks::Tasks; diff --git a/yazi-core/src/tasks/commands/cancel.rs b/yazi-core/src/tasks/commands/cancel.rs index ea028859..b449345c 100644 --- a/yazi-core/src/tasks/commands/cancel.rs +++ b/yazi-core/src/tasks/commands/cancel.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tasks::Tasks; diff --git a/yazi-core/src/tasks/commands/inspect.rs b/yazi-core/src/tasks/commands/inspect.rs index f27c8295..b79255ad 100644 --- a/yazi-core/src/tasks/commands/inspect.rs +++ b/yazi-core/src/tasks/commands/inspect.rs @@ -2,7 +2,7 @@ use std::io::{stdout, Write}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use tokio::{io::{stdin, AsyncReadExt}, select, sync::mpsc, time}; -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use yazi_shared::{term::Term, Defer}; use crate::{emit, tasks::Tasks, Event, BLOCKER}; diff --git a/yazi-core/src/tasks/commands/toggle.rs b/yazi-core/src/tasks/commands/toggle.rs index ad7a4cda..43b72325 100644 --- a/yazi-core/src/tasks/commands/toggle.rs +++ b/yazi-core/src/tasks/commands/toggle.rs @@ -1,4 +1,4 @@ -use yazi_config::keymap::Exec; +use yazi_shared::Exec; use crate::tasks::Tasks; diff --git a/yazi-fm/src/app.rs b/yazi-fm/src/app.rs index 6d94c5f2..1239e2c9 100644 --- a/yazi-fm/src/app.rs +++ b/yazi-fm/src/app.rs @@ -4,9 +4,9 @@ use anyhow::{Ok, Result}; use crossterm::event::KeyEvent; use ratatui::{backend::Backend, prelude::Rect}; use tokio::sync::oneshot; -use yazi_config::{keymap::{Exec, Key}, BOOT}; +use yazi_config::{keymap::Key, BOOT}; use yazi_core::{emit, files::FilesOp, input::InputMode, manager::Manager, preview::COLLISION, Ctx, Event}; -use yazi_shared::{term::Term, Layer}; +use yazi_shared::{term::Term, Exec, Layer}; use crate::{Executor, Logs, Panic, Root, Signals}; diff --git a/yazi-fm/src/executor.rs b/yazi-fm/src/executor.rs index bc88feb3..17b57d13 100644 --- a/yazi-fm/src/executor.rs +++ b/yazi-fm/src/executor.rs @@ -1,6 +1,6 @@ -use yazi_config::{keymap::{Control, Exec, Key}, KEYMAP}; +use yazi_config::{keymap::{Control, Key}, KEYMAP}; use yazi_core::{input::InputMode, Ctx}; -use yazi_shared::Layer; +use yazi_shared::{Exec, Layer}; pub(super) struct Executor<'a> { cx: &'a mut Ctx, diff --git a/yazi-shared/src/event.rs b/yazi-shared/src/event.rs new file mode 100644 index 00000000..1706e0ff --- /dev/null +++ b/yazi-shared/src/event.rs @@ -0,0 +1,54 @@ +use std::{any::Any, collections::BTreeMap, fmt::{self, Display}}; + +#[derive(Debug, Default)] +pub struct Exec { + pub cmd: String, + pub args: Vec, + pub named: BTreeMap, + pub data: Option>, +} + +impl Exec { + #[inline] + pub fn call(cwd: &str, args: Vec) -> Self { + Exec { cmd: cwd.to_owned(), args, ..Default::default() } + } + + #[inline] + pub fn call_named(cwd: &str, named: BTreeMap) -> Self { + Exec { cmd: cwd.to_owned(), named, ..Default::default() } + } + + #[inline] + pub fn vec(self) -> Vec { vec![self] } + + #[inline] + pub fn with(mut self, name: impl ToString, value: impl ToString) -> Self { + self.named.insert(name.to_string(), value.to_string()); + self + } + + #[inline] + pub fn with_bool(mut self, name: impl ToString, state: bool) -> Self { + if state { + self.named.insert(name.to_string(), Default::default()); + } + self + } +} + +impl Display for Exec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.cmd)?; + if !self.args.is_empty() { + write!(f, " {}", self.args.join(" "))?; + } + for (k, v) in &self.named { + write!(f, " --{k}")?; + if !v.is_empty() { + write!(f, "={v}")?; + } + } + Ok(()) + } +} diff --git a/yazi-shared/src/lib.rs b/yazi-shared/src/lib.rs index 1f5617a4..9f87b4f1 100644 --- a/yazi-shared/src/lib.rs +++ b/yazi-shared/src/lib.rs @@ -5,6 +5,7 @@ mod debounce; mod defer; mod env; mod errors; +mod event; pub mod fs; mod layer; mod mime; @@ -19,6 +20,7 @@ pub use debounce::*; pub use defer::*; pub use env::*; pub use errors::*; +pub use event::*; pub use layer::*; pub use mime::*; pub use natsort::*;