gravel_provider_exec/
lib.rs

1//! gravel's command runner
2//!
3//! Always returns a hit with the minimum score that, when selected,
4//! runs the command with the system shell.
5
6use gravel_ffi::prelude::*;
7use serde::Deserialize;
8
9#[cfg_attr(target_os = "linux", path = "linux.rs")]
10#[cfg_attr(windows, path = "windows.rs")]
11mod implementation;
12
13const DEFAULT_CONFIG: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/config.yml"));
14
15struct ExecProvider {
16	config: Config,
17}
18
19#[gravel_provider("exec")]
20impl Provider for ExecProvider {
21	fn new(config: &PluginConfigAdapter<'_>) -> Self {
22		Self {
23			config: config.get(DEFAULT_CONFIG),
24		}
25	}
26
27	fn query(&self, query: &str) -> ProviderResult {
28		let hit = SimpleHit::new(query, &*self.config.subtitle, run_command).with_score(MIN_SCORE);
29
30		ProviderResult::single(hit)
31	}
32}
33
34fn run_command(hit: &SimpleHit, context: RefDynHitActionContext<'_>) {
35	implementation::run_command(hit.title().as_str())
36		.inspect_err(|e| log::error!("{e}"))
37		.ok();
38
39	context.hide_frontend();
40}
41
42#[derive(Deserialize, Debug)]
43struct Config {
44	pub subtitle: String,
45}