diff --git a/src/extension/mod.rs b/src/extension/mod.rs index e90f62b..139dea2 100644 --- a/src/extension/mod.rs +++ b/src/extension/mod.rs @@ -1,6 +1,7 @@ use serde_yaml::Mapping; mod date; +mod shell; pub trait Extension { fn name(&self) -> String; @@ -10,5 +11,6 @@ pub trait Extension { pub fn get_extensions() -> Vec> { vec![ Box::new(date::DateExtension::new()), + Box::new(shell::ShellExtension::new()), ] } \ No newline at end of file diff --git a/src/extension/shell.rs b/src/extension/shell.rs new file mode 100644 index 0000000..6b54b81 --- /dev/null +++ b/src/extension/shell.rs @@ -0,0 +1,49 @@ +use serde_yaml::{Mapping, Value}; +use std::process::Command; +use log::{warn, error}; + +pub struct ShellExtension {} + +impl ShellExtension { + pub fn new() -> ShellExtension { + ShellExtension{} + } +} + +impl super::Extension for ShellExtension { + fn name(&self) -> String { + String::from("shell") + } + + fn calculate(&self, params: &Mapping) -> Option { + let cmd = params.get(&Value::from("cmd")); + if cmd.is_none() { + warn!("No 'cmd' parameter specified for shell variable"); + return None + } + let cmd = cmd.unwrap().as_str().unwrap(); + + let output = if cfg!(target_os = "windows") { + Command::new("cmd") + .args(&["/C", cmd]) + .output() + } else { + Command::new("sh") + .arg("-c") + .arg(cmd) + .output() + }; + + match output { + Ok(output) => { + let output_str = String::from_utf8_lossy(output.stdout.as_slice()); + + Some(output_str.into_owned()) + }, + Err(e) => { + error!("Could not execute cmd '{}', error: {}", cmd, e); + None + }, + } + } +} \ No newline at end of file diff --git a/src/res/config.yaml b/src/res/config.yaml index 2e5676f..906bbd9 100644 --- a/src/res/config.yaml +++ b/src/res/config.yaml @@ -23,7 +23,7 @@ matches: # Accented letters - trigger: "e'" replace: "è" - - trigger: "e/" + - trigger: "e//" replace: "é" - trigger: "a'" replace: "à"