feat(config): add force_mode parameter

This commit is contained in:
Federico Terzi 2021-06-06 21:51:14 +02:00
parent 394aed1b1d
commit 030763cab5
3 changed files with 43 additions and 10 deletions

View File

@ -17,7 +17,14 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 anyhow::Result;
use log::{error, warn}; use log::{error, warn};
use parse::YAMLMatchGroup; use parse::YAMLMatchGroup;
@ -137,17 +144,30 @@ impl TryFrom<YAMLMatch> for Match {
.unwrap_or(TriggerCause::default().propagate_case), .unwrap_or(TriggerCause::default().propagate_case),
uppercase_style, uppercase_style,
}) })
} else if let Some(regex) = yaml_match.regex { // TODO: add test case } else if let Some(regex) = yaml_match.regex {
MatchCause::Regex(RegexCause { // TODO: add test case
regex, MatchCause::Regex(RegexCause { regex })
})
} else { } else {
MatchCause::None 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 = let effect =
if yaml_match.replace.is_some() || yaml_match.markdown.is_some() || yaml_match.html.is_some() 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 { let (replace, format) = if let Some(plain) = yaml_match.replace {
(plain, TextFormat::Plain) (plain, TextFormat::Plain)
} else if let Some(markdown) = yaml_match.markdown { } else if let Some(markdown) = yaml_match.markdown {
@ -164,10 +184,12 @@ impl TryFrom<YAMLMatch> for Match {
.into_iter() .into_iter()
.map(|var| var.try_into()) .map(|var| var.try_into())
.collect(); .collect();
MatchEffect::Text(TextEffect { MatchEffect::Text(TextEffect {
replace, replace,
vars: vars?, vars: vars?,
format, format,
force_mode,
}) })
} else if let Some(form_layout) = yaml_match.form { } else if let Some(form_layout) = yaml_match.form {
// TODO: test form case // TODO: test form case
@ -201,11 +223,11 @@ impl TryFrom<YAMLMatch> for Match {
replace: resolved_layout, replace: resolved_layout,
vars, vars,
format: TextFormat::Plain, format: TextFormat::Plain,
force_mode,
}) })
} else if let Some(image_path) = yaml_match.image_path { // TODO: test image case } else if let Some(image_path) = yaml_match.image_path {
MatchEffect::Image(ImageEffect { // TODO: test image case
path: image_path, MatchEffect::Image(ImageEffect { path: image_path })
})
} else { } else {
MatchEffect::None MatchEffect::None
}; };

View File

@ -100,6 +100,9 @@ pub struct YAMLMatch {
#[serde(default)] #[serde(default)]
pub force_clipboard: Option<bool>, pub force_clipboard: Option<bool>,
#[serde(default)]
pub force_mode: Option<String>,
#[serde(default)] #[serde(default)]
pub markdown: Option<String>, pub markdown: Option<String>,

View File

@ -141,6 +141,7 @@ pub struct TextEffect {
pub replace: String, pub replace: String,
pub vars: Vec<Variable>, pub vars: Vec<Variable>,
pub format: TextFormat, pub format: TextFormat,
pub force_mode: Option<TextInjectMode>,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -150,12 +151,19 @@ pub enum TextFormat {
Html, Html,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TextInjectMode {
Keys,
Clipboard,
}
impl Default for TextEffect { impl Default for TextEffect {
fn default() -> Self { fn default() -> Self {
Self { Self {
replace: String::new(), replace: String::new(),
vars: Vec::new(), vars: Vec::new(),
format: TextFormat::Plain, format: TextFormat::Plain,
force_mode: None,
} }
} }
} }