feat(core): implement builtin to dump app info

This commit is contained in:
Federico Terzi 2021-07-31 22:56:29 +02:00
parent 7f3c70c8df
commit d23de2e3c9
6 changed files with 55 additions and 10 deletions

View File

@ -17,7 +17,10 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::{cli::worker::builtin::generate_next_builtin_id, engine::event::{EventType, effect::{TextInjectRequest}}};
use crate::{
cli::worker::builtin::generate_next_builtin_id,
engine::event::{effect::TextInjectRequest, EventType},
};
use super::BuiltInMatch;
@ -38,3 +41,26 @@ pub fn create_match_paste_active_config_info() -> BuiltInMatch {
},
}
}
pub fn create_match_paste_active_app_info() -> BuiltInMatch {
BuiltInMatch {
id: generate_next_builtin_id(),
label: "Paste active application information (detect)",
triggers: vec!["#detect#".to_string()],
action: |context| {
let info = context.get_active_app_info();
let dump = format!(
"title: '{}'\nexec: '{}'\nclass: '{}'",
info.title.unwrap_or_default(),
info.exec.unwrap_or_default(),
info.class.unwrap_or_default()
);
EventType::TextInject(TextInjectRequest {
text: dump,
force_mode: None,
})
},
}
}

View File

@ -37,6 +37,7 @@ pub struct BuiltInMatch {
pub fn get_builtin_matches() -> Vec<BuiltInMatch> {
vec![
debug::create_match_paste_active_config_info(),
debug::create_match_paste_active_app_info(),
]
}

View File

@ -17,16 +17,25 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use espanso_info::AppInfoProvider;
use super::*;
use crate::cli::worker::config::ConfigManager;
pub struct DefaultContext<'a> {
config_manager: &'a ConfigManager<'a>,
app_info_provider: &'a dyn AppInfoProvider,
}
impl<'a> DefaultContext<'a> {
pub fn new(config_manager: &'a ConfigManager<'a>) -> Self {
Self { config_manager }
pub fn new(
config_manager: &'a ConfigManager<'a>,
app_info_provider: &'a dyn AppInfoProvider,
) -> Self {
Self {
config_manager,
app_info_provider,
}
}
}
@ -41,3 +50,9 @@ impl<'a> ConfigContext for DefaultContext<'a> {
self.config_manager.active()
}
}
impl<'a> AppInfoContext for DefaultContext<'a> {
fn get_active_app_info(&self) -> AppInfo {
self.app_info_provider.get_info()
}
}

View File

@ -23,10 +23,15 @@ use espanso_config::config::Config;
mod default;
pub use default::DefaultContext;
use espanso_info::{AppInfo};
pub trait Context: ConfigContext {}
pub trait Context: ConfigContext + AppInfoContext {}
pub trait ConfigContext {
fn get_default_config(&self) -> Arc<dyn Config>;
fn get_active_config(&self) -> Arc<dyn Config>;
}
pub trait AppInfoContext {
fn get_active_app_info(&self) -> AppInfo;
}

View File

@ -85,7 +85,7 @@ pub fn initialize_and_spawn(
let modulo_form_ui = crate::gui::modulo::form::ModuloFormUI::new(&modulo_manager);
let modulo_search_ui = crate::gui::modulo::search::ModuloSearchUI::new(&modulo_manager);
let context: Box<dyn Context> = Box::new(super::context::DefaultContext::new(&config_manager));
let context: Box<dyn Context> = Box::new(super::context::DefaultContext::new(&config_manager, &*app_info_provider));
let builtin_matches = super::builtin::get_builtin_matches();
let combined_match_cache = CombinedMatchCache::load(&match_cache, &builtin_matches);

View File

@ -46,7 +46,7 @@ pub struct MultiplexAdapter<'a> {
}
impl<'a> MultiplexAdapter<'a> {
pub fn new(provider: &'a dyn MatchProvider<'a>, context: &'a dyn Context) -> Self {
pub fn new(provider: &'a dyn MatchProvider<'a>, context: &'a Context) -> Self {
Self { provider, context }
}
}
@ -69,9 +69,7 @@ impl<'a> Multiplexer for MultiplexAdapter<'a> {
})),
MatchEffect::None => None,
},
MatchResult::Builtin(m) => {
Some((m.action)(self.context))
}
MatchResult::Builtin(m) => Some((m.action)(self.context)),
}
}
}