Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expand the types supported by the event system #923

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions yazi-config/src/keymap/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ pub struct Control {

impl Control {
#[inline]
pub fn to_seq(&self) -> VecDeque<Cmd> {
self.run.iter().map(|e| e.clone_without_data()).collect()
}
pub fn to_seq(&self) -> VecDeque<Cmd> { self.run.iter().map(|c| c.shallow_clone()).collect() }
}

impl Control {
Expand Down
35 changes: 21 additions & 14 deletions yazi-config/src/keymap/run.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
use std::fmt;
use std::{fmt, mem};

use anyhow::{bail, Result};
use serde::{de::{self, Visitor}, Deserializer};
use yazi_shared::event::Cmd;
use yazi_shared::event::{Cmd, Data};

pub(super) fn run_deserialize<'de, D>(deserializer: D) -> Result<Vec<Cmd>, D::Error>
where
D: Deserializer<'de>,
{
struct RunVisitor;

#[allow(clippy::explicit_counter_loop)]
fn parse(s: &str) -> Result<Cmd> {
let s = shell_words::split(s)?;
if s.is_empty() {
bail!("`run` cannot be empty");
}
let mut args = shell_words::split(s)?;
let mut cmd = Cmd { name: mem::take(&mut args[0]), ..Default::default() };

let mut i = 0usize;
for arg in args.into_iter().skip(1) {
let Some(arg) = arg.strip_prefix("--") else {
cmd.args.insert(i.to_string(), Data::String(arg));
i += 1;
continue;
};

let mut parts = arg.splitn(2, '=');
let Some(key) = parts.next().map(|s| s.to_owned()) else {
bail!("invalid argument: {arg}");
};

let mut cmd = Cmd { name: s[0].clone(), ..Default::default() };
for arg in s.into_iter().skip(1) {
if arg.starts_with("--") {
let mut arg = arg.splitn(2, '=');
let key = arg.next().unwrap().trim_start_matches('-');
let val = arg.next().unwrap_or("").to_string();
cmd.named.insert(key.to_string(), val);
if let Some(val) = parts.next() {
cmd.args.insert(key, Data::String(val.to_owned()));
} else {
cmd.args.push(arg);
cmd.args.insert(key, Data::Boolean(true));
}
}
Ok(cmd)
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/completion/commands/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Opt {

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self { step: c.take_first().and_then(|s| s.parse().ok()).unwrap_or(0) }
Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/completion/commands/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { submit: c.named.contains_key("submit") } }
fn from(c: Cmd) -> Self { Self { submit: c.get_bool("submit") } }
}

impl Completion {
Expand Down
10 changes: 5 additions & 5 deletions yazi-core/src/completion/commands/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub struct Opt {
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
cache: mem::take(&mut c.args),
cache_name: c.take_name("cache-name").unwrap_or_default(),
word: c.take_name("word").unwrap_or_default(),
ticket: c.take_name("ticket").and_then(|v| v.parse().ok()).unwrap_or(0),
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),
}
}
}
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Completion {
}

if !opt.cache.is_empty() {
self.caches.insert(opt.cache_name.to_owned(), opt.cache.clone());
self.caches.insert(opt.cache_name.to_owned(), opt.cache);
}
let Some(cache) = self.caches.get(&opt.cache_name) else {
return;
Expand Down
7 changes: 4 additions & 3 deletions yazi-core/src/completion/commands/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub struct Opt {
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
word: c.take_first().unwrap_or_default(),
ticket: c.take_name("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
word: c.take_first_str().unwrap_or_default(),
ticket: c.take_str("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
}
}
}
Expand Down Expand Up @@ -57,7 +57,8 @@ impl Completion {

if !cache.is_empty() {
emit!(Call(
Cmd::args("show", cache)
Cmd::new("show")
.with_any("cache", cache)
.with("cache-name", parent)
.with("word", child)
.with("ticket", ticket),
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/folder/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum FilterCase {

impl From<&Cmd> for FilterCase {
fn from(c: &Cmd) -> Self {
match (c.named.contains_key("smart"), c.named.contains_key("insensitive")) {
match (c.get_bool("smart"), c.get_bool("insensitive")) {
(true, _) => Self::Smart,
(_, false) => Self::Sensitive,
(_, true) => Self::Insensitive,
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/help/commands/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Opt {

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self { step: c.take_first().and_then(|s| s.parse().ok()).unwrap_or(0) }
Self { step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) }
}
}
impl From<isize> for Opt {
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/backspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { under: c.named.contains_key("under") } }
fn from(c: Cmd) -> Self { Self { under: c.get_bool("under") } }
}
impl From<bool> for Opt {
fn from(under: bool) -> Self { Self { under } }
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { submit: c.named.contains_key("submit") } }
fn from(c: Cmd) -> Self { Self { submit: c.get_bool("submit") } }
}
impl From<bool> for Opt {
fn from(submit: bool) -> Self { Self { submit } }
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct Opt {
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
word: c.take_first().unwrap_or_default(),
ticket: c.take_name("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
word: c.take_first_str().unwrap_or_default(),
ticket: c.take_str("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions yazi-core/src/input/commands/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self {
Self { cut: c.named.contains_key("cut"), insert: c.named.contains_key("insert") }
}
fn from(c: Cmd) -> Self { Self { cut: c.get_bool("cut"), insert: c.get_bool("insert") } }
}

impl Input {
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { end_of_word: c.named.contains_key("end-of-word") } }
fn from(c: Cmd) -> Self { Self { end_of_word: c.get_bool("end-of-word") } }
}

impl Input {
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { append: c.named.contains_key("append") } }
fn from(c: Cmd) -> Self { Self { append: c.get_bool("append") } }
}
impl From<bool> for Opt {
fn from(append: bool) -> Self { Self { append } }
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self { Self { kind: c.take_first().unwrap_or_default() } }
fn from(mut c: Cmd) -> Self { Self { kind: c.take_first_str().unwrap_or_default() } }
}

impl Input {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/move_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct Opt {
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
step: c.take_first().and_then(|s| s.parse().ok()).unwrap_or(0),
in_operating: c.named.contains_key("in-operating"),
step: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0),
in_operating: c.get_bool("in-operating"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { before: c.named.contains_key("before") } }
fn from(c: Cmd) -> Self { Self { before: c.get_bool("before") } }
}

impl Input {
Expand Down
20 changes: 17 additions & 3 deletions yazi-core/src/input/commands/show.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use yazi_proxy::options::InputOpt;
use yazi_shared::render;
use tokio::sync::mpsc;
use yazi_config::popup::InputCfg;
use yazi_shared::{event::Cmd, render, InputError};

use crate::input::Input;

pub struct Opt {
cfg: InputCfg,
tx: mpsc::UnboundedSender<Result<String, InputError>>,
}

impl TryFrom<Cmd> for Opt {
type Error = ();

fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
Ok(Self { cfg: c.take_any("cfg").ok_or(())?, tx: c.take_any("tx").ok_or(())? })
}
}

impl Input {
pub fn show(&mut self, opt: impl TryInto<InputOpt>) {
pub fn show(&mut self, opt: impl TryInto<Opt>) {
let Ok(opt) = opt.try_into() else { return };

self.close(false);
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { force: c.named.contains_key("force") } }
fn from(c: Cmd) -> Self { Self { force: c.get_bool("force") } }
}

impl Manager {
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self { Self { url: c.take_first().map(Url::from) } }
fn from(mut c: Cmd) -> Self { Self { url: c.take_first_str().map(Url::from) } }
}
impl From<Option<Url>> for Opt {
fn from(url: Option<Url>) -> Self { Self { url } }
Expand Down
4 changes: 1 addition & 3 deletions yazi-core/src/manager/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self {
Self { relative: c.named.contains_key("relative"), force: c.named.contains_key("force") }
}
fn from(c: Cmd) -> Self { Self { relative: c.get_bool("relative"), force: c.get_bool("force") } }
}

impl Manager {
Expand Down
5 changes: 1 addition & 4 deletions yazi-core/src/manager/commands/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ pub struct Opt {

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self {
Self {
interactive: c.named.contains_key("interactive"),
hovered: c.named.contains_key("hovered"),
}
Self { interactive: c.get_bool("interactive"), hovered: c.get_bool("hovered") }
}
}

Expand Down
4 changes: 1 addition & 3 deletions yazi-core/src/manager/commands/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub struct Opt {
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self {
Self { force: c.named.contains_key("force"), follow: c.named.contains_key("follow") }
}
fn from(c: Cmd) -> Self { Self { force: c.get_bool("force"), follow: c.get_bool("follow") } }
}

impl Manager {
Expand Down
8 changes: 4 additions & 4 deletions yazi-core/src/manager/commands/peek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub struct Opt {
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
skip: c.take_first().and_then(|s| s.parse().ok()),
force: c.named.contains_key("force"),
only_if: c.take_name("only-if").map(Url::from),
upper_bound: c.named.contains_key("upper-bound"),
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"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/quit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl From<()> for Opt {
fn from(_: ()) -> Self { Self::default() }
}
impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { no_cwd_file: c.named.contains_key("no-cwd-file") } }
fn from(c: Cmd) -> Self { Self { no_cwd_file: c.get_bool("no-cwd-file") } }
}

impl Manager {
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/manager/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub struct Opt {
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
force: c.named.contains_key("force"),
permanently: c.named.contains_key("permanently"),
targets: c.take_data().unwrap_or_default(),
force: c.get_bool("force"),
permanently: c.get_bool("permanently"),
targets: c.take_any("targets").unwrap_or_default(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/manager/commands/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub struct Opt {
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
force: c.named.contains_key("force"),
empty: c.take_name("empty").unwrap_or_default(),
cursor: c.take_name("cursor").unwrap_or_default(),
force: c.get_bool("force"),
empty: c.take_str("empty").unwrap_or_default(),
cursor: c.take_str("cursor").unwrap_or_default(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Opt {

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self { units: c.take_first().and_then(|s| s.parse().ok()).unwrap_or(0) }
Self { units: c.take_first_str().and_then(|s| s.parse().ok()).unwrap_or(0) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/tab_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Opt {

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self { idx: c.take_first().and_then(|i| i.parse().ok()).unwrap_or(0) }
Self { idx: c.take_first_str().and_then(|i| i.parse().ok()).unwrap_or(0) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/manager/commands/tab_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub struct Opt {

impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
if c.named.contains_key("current") {
if c.get_bool("current") {
Self { url: Default::default(), current: true }
} else {
Self {
url: c.take_first().map_or_else(|| Url::from(&BOOT.cwd), Url::from),
url: c.take_first_str().map_or_else(|| Url::from(&BOOT.cwd), Url::from),
current: false,
}
}
Expand Down
Loading