This commit is contained in:
sxyazi 2023-11-27 15:10:36 +08:00
parent e62b324be1
commit 0a594302a4
No known key found for this signature in database
5 changed files with 26 additions and 16 deletions

View file

@ -4,7 +4,7 @@ use serde::Deserialize;
use super::{Exec, Key};
#[derive(Clone, Debug, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct Control {
pub on: Vec<Key>,
#[serde(deserialize_with = "Exec::deserialize")]
@ -13,8 +13,18 @@ pub struct Control {
}
impl Control {
#[inline]
pub fn to_call(&self) -> Vec<Exec> { self.exec.clone() }
pub fn to_call(&self) -> Vec<Exec> {
self
.exec
.iter()
.map(|e| Exec {
cmd: e.cmd.clone(),
args: e.args.clone(),
named: e.named.clone(),
data: None,
})
.collect()
}
}
impl Control {

View file

@ -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<String>,
pub named: BTreeMap<String, String>,
pub data: Option<Box<dyn Any + Send>>,
}
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<String>) -> 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<String, String>) -> Self {
Exec { cmd: cwd.to_owned(), args: Default::default(), named }
Exec { cmd: cwd.to_owned(), named, ..Default::default() }
}
#[inline]

View file

@ -10,7 +10,7 @@ use crate::input::Input;
pub struct Help {
pub visible: bool,
pub layer: Layer,
pub(super) bindings: Vec<Control>,
pub(super) bindings: Vec<&'static Control>,
// Filter
keyword: Option<String>,
@ -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]
}

View file

@ -8,7 +8,7 @@ use crate::emit;
pub struct Which {
layer: Layer,
pub times: usize,
pub cands: Vec<Control>,
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
}

View file

@ -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),