From 030763cab5f833b43e81d4fb987964a0c1bc656d Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 6 Jun 2021 21:51:14 +0200 Subject: [PATCH] feat(config): add force_mode parameter --- .../src/matches/group/loader/yaml/mod.rs | 42 ++++++++++++++----- .../src/matches/group/loader/yaml/parse.rs | 3 ++ espanso-config/src/matches/mod.rs | 8 ++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/espanso-config/src/matches/group/loader/yaml/mod.rs b/espanso-config/src/matches/group/loader/yaml/mod.rs index 0280aa5..fc66d5e 100644 --- a/espanso-config/src/matches/group/loader/yaml/mod.rs +++ b/espanso-config/src/matches/group/loader/yaml/mod.rs @@ -17,7 +17,14 @@ * along with espanso. If not, see . */ -use crate::{counter::next_id, matches::{ImageEffect, Match, Params, RegexCause, TextFormat, UpperCasingStyle, Value, Variable, group::{path::resolve_imports, MatchGroup}}}; +use crate::{ + counter::next_id, + matches::{ + group::{path::resolve_imports, MatchGroup}, + ImageEffect, Match, Params, RegexCause, TextFormat, TextInjectMode, UpperCasingStyle, Value, + Variable, + }, +}; use anyhow::Result; use log::{error, warn}; use parse::YAMLMatchGroup; @@ -137,17 +144,30 @@ impl TryFrom for Match { .unwrap_or(TriggerCause::default().propagate_case), uppercase_style, }) - } else if let Some(regex) = yaml_match.regex { // TODO: add test case - MatchCause::Regex(RegexCause { - regex, - }) + } else if let Some(regex) = yaml_match.regex { + // TODO: add test case + MatchCause::Regex(RegexCause { regex }) } else { MatchCause::None }; + // TODO: test force_mode/force_clipboard + let force_mode = if let Some(true) = yaml_match.force_clipboard { + Some(TextInjectMode::Clipboard) + } else if let Some(mode) = yaml_match.force_mode { + match mode.to_lowercase().as_str() { + "clipboard" => Some(TextInjectMode::Clipboard), + "keys" => Some(TextInjectMode::Keys), + _ => None, + } + } else { + None + }; + let effect = if yaml_match.replace.is_some() || yaml_match.markdown.is_some() || yaml_match.html.is_some() - { // TODO: test markdown and html cases + { + // TODO: test markdown and html cases let (replace, format) = if let Some(plain) = yaml_match.replace { (plain, TextFormat::Plain) } else if let Some(markdown) = yaml_match.markdown { @@ -164,10 +184,12 @@ impl TryFrom for Match { .into_iter() .map(|var| var.try_into()) .collect(); + MatchEffect::Text(TextEffect { replace, vars: vars?, format, + force_mode, }) } else if let Some(form_layout) = yaml_match.form { // TODO: test form case @@ -201,11 +223,11 @@ impl TryFrom for Match { replace: resolved_layout, vars, format: TextFormat::Plain, + force_mode, }) - } else if let Some(image_path) = yaml_match.image_path { // TODO: test image case - MatchEffect::Image(ImageEffect { - path: image_path, - }) + } else if let Some(image_path) = yaml_match.image_path { + // TODO: test image case + MatchEffect::Image(ImageEffect { path: image_path }) } else { MatchEffect::None }; diff --git a/espanso-config/src/matches/group/loader/yaml/parse.rs b/espanso-config/src/matches/group/loader/yaml/parse.rs index c9d7a8f..80c2307 100644 --- a/espanso-config/src/matches/group/loader/yaml/parse.rs +++ b/espanso-config/src/matches/group/loader/yaml/parse.rs @@ -100,6 +100,9 @@ pub struct YAMLMatch { #[serde(default)] pub force_clipboard: Option, + #[serde(default)] + pub force_mode: Option, + #[serde(default)] pub markdown: Option, diff --git a/espanso-config/src/matches/mod.rs b/espanso-config/src/matches/mod.rs index 5783db5..c2b89c8 100644 --- a/espanso-config/src/matches/mod.rs +++ b/espanso-config/src/matches/mod.rs @@ -141,6 +141,7 @@ pub struct TextEffect { pub replace: String, pub vars: Vec, pub format: TextFormat, + pub force_mode: Option, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -150,12 +151,19 @@ pub enum TextFormat { Html, } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum TextInjectMode { + Keys, + Clipboard, +} + impl Default for TextEffect { fn default() -> Self { Self { replace: String::new(), vars: Vec::new(), format: TextFormat::Plain, + force_mode: None, } } }