yazi/config/src/keymap/control.rs
三咲雅 · Misaki Masa 7ec7e36a53
feat: find (#104)
2023-09-11 18:49:26 +08:00

40 lines
884 B
Rust

use std::borrow::Cow;
use serde::Deserialize;
use super::{Exec, Key};
#[derive(Clone, Debug, Deserialize)]
pub struct Control {
pub on: Vec<Key>,
#[serde(deserialize_with = "Exec::deserialize")]
pub exec: Vec<Exec>,
pub desc: Option<String>,
}
impl Control {
#[inline]
pub fn to_call(&self) -> Vec<Exec> { self.exec.clone() }
}
impl Control {
#[inline]
pub fn on(&self) -> String { self.on.iter().map(ToString::to_string).collect() }
#[inline]
pub fn exec(&self) -> String {
self.exec.iter().map(|e| e.to_string()).collect::<Vec<_>>().join("; ")
}
#[inline]
pub fn desc_or_exec(&self) -> Cow<str> {
if let Some(ref s) = self.desc { Cow::Borrowed(s) } else { self.exec().into() }
}
#[inline]
pub fn contains(&self, s: &str) -> bool {
self.desc.as_ref().map(|d| d.contains(s)) == Some(true)
|| self.exec().contains(s)
|| self.on().contains(s)
}
}