2019-08-31 14:07:45 +00:00
|
|
|
use crate::matcher::{Match, MatchReceiver};
|
|
|
|
use crate::keyboard::KeyboardSender;
|
2019-09-05 18:54:19 +00:00
|
|
|
use crate::config::Configs;
|
2019-09-06 22:38:13 +00:00
|
|
|
use crate::clipboard::ClipboardManager;
|
|
|
|
use std::sync::Arc;
|
2019-08-31 14:07:45 +00:00
|
|
|
|
2019-09-06 22:38:13 +00:00
|
|
|
pub struct Engine<S, C> where S: KeyboardSender, C: ClipboardManager {
|
2019-09-05 18:54:19 +00:00
|
|
|
sender: S,
|
2019-09-06 22:38:13 +00:00
|
|
|
clipboard_manager: Arc<C>,
|
2019-09-05 18:54:19 +00:00
|
|
|
configs: Configs,
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 22:38:13 +00:00
|
|
|
impl <S, C> Engine<S, C> where S: KeyboardSender, C: ClipboardManager{
|
|
|
|
pub fn new(sender: S, clipboard_manager: Arc<C>, configs: Configs) -> Engine<S, C> where S: KeyboardSender, C: ClipboardManager {
|
|
|
|
Engine{sender, clipboard_manager, configs }
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 22:38:13 +00:00
|
|
|
impl <S, C> MatchReceiver for Engine<S, C> where S: KeyboardSender, C: ClipboardManager{
|
2019-08-31 15:00:23 +00:00
|
|
|
fn on_match(&self, m: &Match) {
|
2019-08-31 14:07:45 +00:00
|
|
|
self.sender.delete_string(m.trigger.len() as i32);
|
2019-09-06 08:21:33 +00:00
|
|
|
|
2019-09-06 20:19:28 +00:00
|
|
|
// Send the expected string. On linux, newlines are managed automatically
|
|
|
|
// while on windows and macos, we need to emulate a Enter key press.
|
2019-09-06 08:21:33 +00:00
|
|
|
|
2019-09-06 20:19:28 +00:00
|
|
|
if cfg!(target_os = "linux") {
|
2019-09-06 22:38:13 +00:00
|
|
|
self.clipboard_manager.set_clipboard(m.replace.as_str());
|
|
|
|
self.sender.trigger_paste();
|
|
|
|
//self.sender.send_string(m.replace.as_str());
|
2019-09-06 20:19:28 +00:00
|
|
|
}else{
|
|
|
|
// To handle newlines, substitute each "\n" char with an Enter key press.
|
|
|
|
let splits = m.replace.lines();
|
|
|
|
|
|
|
|
for (i, split) in splits.enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
self.sender.send_enter();
|
|
|
|
}
|
2019-09-06 08:21:33 +00:00
|
|
|
|
2019-09-06 20:19:28 +00:00
|
|
|
self.sender.send_string(split);
|
|
|
|
}
|
2019-09-06 08:21:33 +00:00
|
|
|
}
|
2019-08-31 14:07:45 +00:00
|
|
|
}
|
|
|
|
}
|