From 0a594302a48b30b762331105b25a74c761b70a78 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 27 Nov 2023 15:10:36 +0800 Subject: [PATCH] .. --- yazi-config/src/keymap/control.rs | 16 +++++++++++++--- yazi-config/src/keymap/exec.rs | 11 ++++++----- yazi-core/src/help/help.rs | 8 ++++---- yazi-core/src/which/which.rs | 5 ++--- yazi-fm/src/which/layout.rs | 2 +- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/yazi-config/src/keymap/control.rs b/yazi-config/src/keymap/control.rs index 30789317..45992ec0 100644 --- a/yazi-config/src/keymap/control.rs +++ b/yazi-config/src/keymap/control.rs @@ -4,7 +4,7 @@ use serde::Deserialize; use super::{Exec, Key}; -#[derive(Clone, Debug, Deserialize)] +#[derive(Debug, Deserialize)] pub struct Control { pub on: Vec, #[serde(deserialize_with = "Exec::deserialize")] @@ -13,8 +13,18 @@ pub struct Control { } impl Control { - #[inline] - pub fn to_call(&self) -> Vec { self.exec.clone() } + pub fn to_call(&self) -> Vec { + self + .exec + .iter() + .map(|e| Exec { + cmd: e.cmd.clone(), + args: e.args.clone(), + named: e.named.clone(), + data: None, + }) + .collect() + } } impl Control { diff --git a/yazi-config/src/keymap/exec.rs b/yazi-config/src/keymap/exec.rs index 9548470a..f83d6bb6 100644 --- a/yazi-config/src/keymap/exec.rs +++ b/yazi-config/src/keymap/exec.rs @@ -1,13 +1,14 @@ -use std::{collections::BTreeMap, fmt::{self, Debug, Display}}; +use std::{any::Any, collections::BTreeMap, fmt::{self, Debug, Display}}; use anyhow::bail; use serde::{de::{self, Visitor}, Deserializer}; -#[derive(Clone, Debug)] +#[derive(Debug, Default)] pub struct Exec { pub cmd: String, pub args: Vec, pub named: BTreeMap, + pub data: Option>, } impl TryFrom<&str> for Exec { @@ -19,7 +20,7 @@ impl TryFrom<&str> for Exec { bail!("`exec` cannot be empty"); } - let mut exec = Self { cmd: s[0].clone(), args: Vec::new(), named: BTreeMap::new() }; + let mut exec = Self { cmd: s[0].clone(), ..Default::default() }; for arg in s.into_iter().skip(1) { if arg.starts_with("--") { let mut arg = arg.splitn(2, '='); @@ -90,12 +91,12 @@ impl Exec { impl Exec { #[inline] pub fn call(cwd: &str, args: Vec) -> Self { - Exec { cmd: cwd.to_owned(), args, named: Default::default() } + Exec { cmd: cwd.to_owned(), args, ..Default::default() } } #[inline] pub fn call_named(cwd: &str, named: BTreeMap) -> Self { - Exec { cmd: cwd.to_owned(), args: Default::default(), named } + Exec { cmd: cwd.to_owned(), named, ..Default::default() } } #[inline] diff --git a/yazi-core/src/help/help.rs b/yazi-core/src/help/help.rs index 3a8a404d..455a2882 100644 --- a/yazi-core/src/help/help.rs +++ b/yazi-core/src/help/help.rs @@ -10,7 +10,7 @@ use crate::input::Input; pub struct Help { pub visible: bool, pub layer: Layer, - pub(super) bindings: Vec, + pub(super) bindings: Vec<&'static Control>, // Filter keyword: Option, @@ -44,9 +44,9 @@ impl Help { } if let Some(kw) = kw { - self.bindings = KEYMAP.get(self.layer).iter().filter(|&c| c.contains(kw)).cloned().collect(); + self.bindings = KEYMAP.get(self.layer).iter().filter(|&c| c.contains(kw)).collect(); } else { - self.bindings = KEYMAP.get(self.layer).clone(); + self.bindings = KEYMAP.get(self.layer).iter().collect(); } self.keyword = kw.map(|s| s.to_owned()); @@ -89,7 +89,7 @@ impl Help { // --- Bindings #[inline] - pub fn window(&self) -> &[Control] { + pub fn window(&self) -> &[&Control] { let end = (self.offset + Self::limit()).min(self.bindings.len()); &self.bindings[self.offset..end] } diff --git a/yazi-core/src/which/which.rs b/yazi-core/src/which/which.rs index 1552904b..bdbe0af0 100644 --- a/yazi-core/src/which/which.rs +++ b/yazi-core/src/which/which.rs @@ -8,7 +8,7 @@ use crate::emit; pub struct Which { layer: Layer, pub times: usize, - pub cands: Vec, + pub cands: Vec<&'static Control>, pub visible: bool, } @@ -23,8 +23,7 @@ impl Which { pub fn show(&mut self, key: &Key, layer: Layer) -> bool { self.layer = layer; self.times = 1; - self.cands = - KEYMAP.get(layer).iter().filter(|s| s.on.len() > 1 && &s.on[0] == key).cloned().collect(); + self.cands = KEYMAP.get(layer).iter().filter(|s| s.on.len() > 1 && &s.on[0] == key).collect(); self.visible = true; true } diff --git a/yazi-fm/src/which/layout.rs b/yazi-fm/src/which/layout.rs index d9f5f93f..745570f7 100644 --- a/yazi-fm/src/which/layout.rs +++ b/yazi-fm/src/which/layout.rs @@ -17,7 +17,7 @@ impl Widget for Which<'_> { fn render(self, area: Rect, buf: &mut Buffer) { let which = &self.cx.which; let mut cands: (Vec<_>, Vec<_>, Vec<_>) = Default::default(); - for (i, c) in which.cands.iter().enumerate() { + for (i, &c) in which.cands.iter().enumerate() { match i % 3 { 0 => cands.0.push(c), 1 => cands.1.push(c),