Add backend configuration option. Fix #10

This commit is contained in:
Federico Terzi 2019-09-07 10:43:23 +02:00
parent 30c127786d
commit 9c680445f0
2 changed files with 36 additions and 15 deletions

View File

@ -32,9 +32,23 @@ pub struct Configs {
#[serde(default = "default_backspace_limit")]
pub backspace_limit: i32,
#[serde(default)]
pub backend: BackendType,
pub matches: Vec<Match>
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BackendType {
Inject,
Clipboard
}
impl Default for BackendType {
fn default() -> Self {
BackendType::Inject
}
}
impl Configs {
pub fn load(path: &Path) -> Configs {
let file_res = File::open(path);

View File

@ -1,6 +1,7 @@
use crate::matcher::{Match, MatchReceiver};
use crate::keyboard::KeyboardSender;
use crate::config::Configs;
use crate::config::BackendType;
use crate::clipboard::ClipboardManager;
use std::sync::Arc;
@ -20,13 +21,13 @@ impl <S, C> MatchReceiver for Engine<S, C> where S: KeyboardSender, C: Clipboard
fn on_match(&self, m: &Match) {
self.sender.delete_string(m.trigger.len() as i32);
match self.configs.backend {
BackendType::Inject => {
// Send the expected string. On linux, newlines are managed automatically
// while on windows and macos, we need to emulate a Enter key press.
if cfg!(target_os = "linux") {
self.clipboard_manager.set_clipboard(m.replace.as_str());
self.sender.trigger_paste();
//self.sender.send_string(m.replace.as_str());
self.sender.send_string(m.replace.as_str());
}else{
// To handle newlines, substitute each "\n" char with an Enter key press.
let splits = m.replace.lines();
@ -39,5 +40,11 @@ impl <S, C> MatchReceiver for Engine<S, C> where S: KeyboardSender, C: Clipboard
self.sender.send_string(split);
}
}
},
BackendType::Clipboard => {
self.clipboard_manager.set_clipboard(m.replace.as_str());
self.sender.trigger_paste();
},
}
}
}