From a5fff067f78dacde89e961a858f9185d866e74b3 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Wed, 21 Apr 2021 20:49:46 +0200 Subject: [PATCH] feat(core): wire up extensions --- espanso/Cargo.toml | 3 +- .../src/cli/worker/engine/render/clipboard.rs | 39 +++++++++++++++++++ .../engine/{render.rs => render/mod.rs} | 4 +- espanso/src/cli/worker/mod.rs | 29 +++++++++++--- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 espanso/src/cli/worker/engine/render/clipboard.rs rename espanso/src/cli/worker/engine/{render.rs => render/mod.rs} (99%) diff --git a/espanso/Cargo.toml b/espanso/Cargo.toml index 26f1e60..1ea8f74 100644 --- a/espanso/Cargo.toml +++ b/espanso/Cargo.toml @@ -31,4 +31,5 @@ thiserror = "1.0.23" clap = "2.33.3" lazy_static = "1.4.0" crossbeam = "0.8.0" -enum-as-inner = "0.3.3" \ No newline at end of file +enum-as-inner = "0.3.3" +dirs = "3.0.1" \ No newline at end of file diff --git a/espanso/src/cli/worker/engine/render/clipboard.rs b/espanso/src/cli/worker/engine/render/clipboard.rs new file mode 100644 index 0000000..dfbe441 --- /dev/null +++ b/espanso/src/cli/worker/engine/render/clipboard.rs @@ -0,0 +1,39 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2021 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +use espanso_clipboard::Clipboard; +use espanso_render::extension::clipboard::ClipboardProvider; + +pub struct ClipboardAdapter<'a> { + clipboard: &'a dyn Clipboard, +} + +impl <'a> ClipboardAdapter<'a> { + pub fn new(clipboard: &'a dyn Clipboard) -> Self { + Self { + clipboard + } + } +} + +impl<'a> ClipboardProvider for ClipboardAdapter<'a> { + fn get_text(&self) -> Option { + self.clipboard.get_text() + } +} diff --git a/espanso/src/cli/worker/engine/render.rs b/espanso/src/cli/worker/engine/render/mod.rs similarity index 99% rename from espanso/src/cli/worker/engine/render.rs rename to espanso/src/cli/worker/engine/render/mod.rs index 7a3c763..f2a48a5 100644 --- a/espanso/src/cli/worker/engine/render.rs +++ b/espanso/src/cli/worker/engine/render/mod.rs @@ -17,7 +17,9 @@ * along with espanso. If not, see . */ -use std::{cell::RefCell, collections::HashMap, convert::TryInto}; +use std::{cell::RefCell, collections::HashMap}; + +pub mod clipboard; use espanso_config::{ config::Config, diff --git a/espanso/src/cli/worker/mod.rs b/espanso/src/cli/worker/mod.rs index 4b7f43d..ca9691b 100644 --- a/espanso/src/cli/worker/mod.rs +++ b/espanso/src/cli/worker/mod.rs @@ -46,6 +46,8 @@ fn worker_main(args: CliModuleArgs) { let match_store = args .match_store .expect("missing match store in worker main"); + + let paths = args.paths.expect("missing paths in worker main"); let app_info_provider = espanso_info::get_provider().expect("unable to initialize app info provider"); @@ -66,8 +68,27 @@ fn worker_main(args: CliModuleArgs) { let selector = MatchSelectorAdapter::new(); let multiplexer = engine::multiplex::MultiplexAdapter::new(&match_cache); - // TODO: add extensions - let renderer = espanso_render::create(Vec::new()); + let injector = + espanso_inject::get_injector(Default::default()).expect("failed to initialize injector module"); // TODO: handle the options + let clipboard = espanso_clipboard::get_clipboard(Default::default()) + .expect("failed to initialize clipboard module"); // TODO: handle options + + let clipboard_adapter = engine::render::clipboard::ClipboardAdapter::new(&*clipboard); + let clipboard_extension = espanso_render::extension::clipboard::ClipboardExtension::new(&clipboard_adapter); + let date_extension = espanso_render::extension::date::DateExtension::new(); + let echo_extension = espanso_render::extension::echo::EchoExtension::new(); + let random_extension = espanso_render::extension::random::RandomExtension::new(); + let home_path = dirs::home_dir().expect("unable to obtain home dir path"); + let script_extension = espanso_render::extension::script::ScriptExtension::new(&paths.config, &home_path, &paths.packages); + let shell_extension = espanso_render::extension::shell::ShellExtension::new(&paths.config); + let renderer = espanso_render::create(vec![ + &clipboard_extension, + &date_extension, + &echo_extension, + &random_extension, + &script_extension, + &shell_extension, + ]); let renderer_adapter = engine::render::RendererAdapter::new(&match_cache, &config_manager, &renderer); @@ -80,10 +101,6 @@ fn worker_main(args: CliModuleArgs) { &match_cache, ); - let injector = - espanso_inject::get_injector(Default::default()).expect("failed to initialize injector module"); // TODO: handle the options - let clipboard = espanso_clipboard::get_clipboard(Default::default()) - .expect("failed to initialize clipboard module"); // TODO: handle options let event_injector = engine::executor::event_injector::EventInjectorAdapter::new(&*injector); let clipboard_injector =