From 3df0df6fc79e57b9270f3e3ffa9dc1badc0af70f Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Thu, 10 Jun 2021 21:14:12 +0200 Subject: [PATCH] feat(config): add word_separator option --- espanso-config/src/config/mod.rs | 4 +++ espanso-config/src/config/parse/mod.rs | 1 + espanso-config/src/config/parse/yaml.rs | 6 +++++ espanso-config/src/config/resolve.rs | 35 ++++++++++++++++++++++--- espanso-config/src/legacy/mod.rs | 4 +++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/espanso-config/src/config/mod.rs b/espanso-config/src/config/mod.rs index b3841b9..e0c1edf 100644 --- a/espanso-config/src/config/mod.rs +++ b/espanso-config/src/config/mod.rs @@ -91,6 +91,10 @@ pub trait Config: Send { // application is missing some key events. fn key_delay(&self) -> Option; + // Chars that when pressed mark the start and end of a word. + // Examples of this are . or , + fn word_separators(&self) -> Vec; + fn is_match<'a>(&self, app: &AppProperties<'a>) -> bool; } diff --git a/espanso-config/src/config/parse/mod.rs b/espanso-config/src/config/parse/mod.rs index 1e30991..31e9007 100644 --- a/espanso-config/src/config/parse/mod.rs +++ b/espanso-config/src/config/parse/mod.rs @@ -34,6 +34,7 @@ pub(crate) struct ParsedConfig { pub toggle_key: Option, pub paste_shortcut: Option, pub disable_x11_fast_inject: Option, + pub word_separators: Option>, pub pre_paste_delay: Option, pub restore_clipboard_delay: Option, diff --git a/espanso-config/src/config/parse/yaml.rs b/espanso-config/src/config/parse/yaml.rs index b1deee3..96c62d0 100644 --- a/espanso-config/src/config/parse/yaml.rs +++ b/espanso-config/src/config/parse/yaml.rs @@ -69,6 +69,9 @@ pub(crate) struct YAMLConfig { #[serde(default)] pub backspace_delay: Option, + #[serde(default)] + pub word_separators: Option>, + // Include/Exclude #[serde(default)] pub includes: Option>, @@ -127,6 +130,7 @@ impl TryFrom for ParsedConfig { disable_x11_fast_inject: yaml_config.disable_x11_fast_inject, inject_delay: yaml_config.inject_delay, key_delay: yaml_config.key_delay.or(yaml_config.backspace_delay), + word_separators: yaml_config.word_separators, pre_paste_delay: yaml_config.pre_paste_delay, restore_clipboard_delay: yaml_config.restore_clipboard_delay, @@ -169,6 +173,7 @@ mod tests { inject_delay: 10 key_delay: 20 backspace_delay: 30 + word_separators: ["'", "."] use_standard_includes: true includes: ["test1"] @@ -204,6 +209,7 @@ mod tests { pre_paste_delay: Some(300), toggle_key: Some("CTRL".to_string()), + word_separators: Some(vec!["'".to_owned(), ".".to_owned()]), use_standard_includes: Some(true), includes: Some(vec!["test1".to_string()]), diff --git a/espanso-config/src/config/resolve.rs b/espanso-config/src/config/resolve.rs index b3de63b..4019b37 100644 --- a/espanso-config/src/config/resolve.rs +++ b/espanso-config/src/config/resolve.rs @@ -17,7 +17,16 @@ * along with espanso. If not, see . */ -use super::{AppProperties, Backend, Config, ToggleKey, default::{DEFAULT_CLIPBOARD_THRESHOLD, DEFAULT_PRE_PASTE_DELAY, DEFAULT_RESTORE_CLIPBOARD_DELAY, DEFAULT_SHORTCUT_EVENT_DELAY}, parse::ParsedConfig, path::calculate_paths, util::os_matches}; +use super::{ + default::{ + DEFAULT_CLIPBOARD_THRESHOLD, DEFAULT_PRE_PASTE_DELAY, DEFAULT_RESTORE_CLIPBOARD_DELAY, + DEFAULT_SHORTCUT_EVENT_DELAY, + }, + parse::ParsedConfig, + path::calculate_paths, + util::os_matches, + AppProperties, Backend, Config, ToggleKey, +}; use crate::{counter::next_id, merge}; use anyhow::Result; use log::error; @@ -193,11 +202,17 @@ impl Config for ResolvedConfig { } fn restore_clipboard_delay(&self) -> usize { - self.parsed.restore_clipboard_delay.unwrap_or(DEFAULT_RESTORE_CLIPBOARD_DELAY) + self + .parsed + .restore_clipboard_delay + .unwrap_or(DEFAULT_RESTORE_CLIPBOARD_DELAY) } fn paste_shortcut_event_delay(&self) -> usize { - self.parsed.paste_shortcut_event_delay.unwrap_or(DEFAULT_SHORTCUT_EVENT_DELAY) + self + .parsed + .paste_shortcut_event_delay + .unwrap_or(DEFAULT_SHORTCUT_EVENT_DELAY) } fn paste_shortcut(&self) -> Option { @@ -215,6 +230,19 @@ impl Config for ResolvedConfig { fn key_delay(&self) -> Option { self.parsed.key_delay } + + fn word_separators(&self) -> Vec { + self.parsed.word_separators.clone().unwrap_or(vec![ + " ".to_string(), + ",".to_string(), + ".".to_string(), + "?".to_string(), + "!".to_string(), + "\r".to_string(), + "\n".to_string(), + (22u8 as char).to_string(), + ]) + } } impl ResolvedConfig { @@ -283,6 +311,7 @@ impl ResolvedConfig { toggle_key, inject_delay, key_delay, + word_separators, includes, excludes, extra_includes, diff --git a/espanso-config/src/legacy/mod.rs b/espanso-config/src/legacy/mod.rs index 94c3465..1655a45 100644 --- a/espanso-config/src/legacy/mod.rs +++ b/espanso-config/src/legacy/mod.rs @@ -327,6 +327,10 @@ impl Config for LegacyInteropConfig { Some(self.config.backspace_delay.try_into().unwrap()) } } + + fn word_separators(&self) -> Vec { + self.config.word_separators.iter().map(|c| String::from(*c)).collect() + } } struct LegacyMatchGroup {