From 9801b09ab620e08cbcbba91c3a1c113cfb403882 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Tue, 21 Dec 2021 22:49:11 +0100 Subject: [PATCH] feat(core): wire up choice extension. #850 --- espanso/src/cli/worker/engine/mod.rs | 8 ++- .../middleware/render/extension/choice.rs | 55 +++++++++++++++++++ .../middleware/render/extension/mod.rs | 1 + 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 espanso/src/cli/worker/engine/process/middleware/render/extension/choice.rs diff --git a/espanso/src/cli/worker/engine/mod.rs b/espanso/src/cli/worker/engine/mod.rs index 513bff5..8581a62 100644 --- a/espanso/src/cli/worker/engine/mod.rs +++ b/espanso/src/cli/worker/engine/mod.rs @@ -49,7 +49,9 @@ use crate::{ }, multiplex::MultiplexAdapter, render::{ - extension::{clipboard::ClipboardAdapter, form::FormProviderAdapter}, + extension::{ + choice::ChoiceSelectorAdapter, clipboard::ClipboardAdapter, form::FormProviderAdapter, + }, RendererAdapter, }, }, @@ -198,6 +200,9 @@ pub fn initialize_and_spawn( let shell_extension = espanso_render::extension::shell::ShellExtension::new(&paths.config); let form_adapter = FormProviderAdapter::new(&modulo_form_ui); let form_extension = espanso_render::extension::form::FormExtension::new(&form_adapter); + let choice_adapter = ChoiceSelectorAdapter::new(&modulo_search_ui); + let choice_extension = + espanso_render::extension::choice::ChoiceExtension::new(&choice_adapter); let renderer = espanso_render::create(vec![ &clipboard_extension, &date_extension, @@ -207,6 +212,7 @@ pub fn initialize_and_spawn( &script_extension, &shell_extension, &form_extension, + &choice_extension, ]); let renderer_adapter = RendererAdapter::new(&match_cache, &config_manager, &renderer); let path_provider = PathProviderAdapter::new(&paths); diff --git a/espanso/src/cli/worker/engine/process/middleware/render/extension/choice.rs b/espanso/src/cli/worker/engine/process/middleware/render/extension/choice.rs new file mode 100644 index 0000000..d2f245d --- /dev/null +++ b/espanso/src/cli/worker/engine/process/middleware/render/extension/choice.rs @@ -0,0 +1,55 @@ +/* + * 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_render::extension::choice::{ChoiceSelector, ChoiceSelectorResult}; + +use crate::gui::{SearchItem, SearchUI}; + +pub struct ChoiceSelectorAdapter<'a> { + search_ui: &'a dyn SearchUI, +} + +impl<'a> ChoiceSelectorAdapter<'a> { + pub fn new(search_ui: &'a dyn SearchUI) -> Self { + Self { search_ui } + } +} + +impl<'a> ChoiceSelector for ChoiceSelectorAdapter<'a> { + fn show(&self, choices: &[espanso_render::extension::choice::Choice]) -> ChoiceSelectorResult { + let items = convert_items(choices); + match self.search_ui.show(&items, None) { + Ok(Some(choice)) => ChoiceSelectorResult::Success(choice), + Ok(None) => ChoiceSelectorResult::Aborted, + Err(err) => ChoiceSelectorResult::Error(err), + } + } +} + +fn convert_items(choices: &[espanso_render::extension::choice::Choice]) -> Vec { + choices + .iter() + .map(|choice| SearchItem { + id: choice.id.to_string(), + label: choice.label.to_string(), + tag: None, + is_builtin: false, + }) + .collect() +} diff --git a/espanso/src/cli/worker/engine/process/middleware/render/extension/mod.rs b/espanso/src/cli/worker/engine/process/middleware/render/extension/mod.rs index d7cafac..e2497b5 100644 --- a/espanso/src/cli/worker/engine/process/middleware/render/extension/mod.rs +++ b/espanso/src/cli/worker/engine/process/middleware/render/extension/mod.rs @@ -17,5 +17,6 @@ * along with espanso. If not, see . */ +pub mod choice; pub mod clipboard; pub mod form;