gravel_provider_kill/
lib.rs

1//! gravel's process killer
2//! Lists running processes on your system and will allow you to kill them.
3
4use gravel_ffi::prelude::*;
5use implementation::Pid;
6
7#[cfg_attr(target_os = "linux", path = "linux.rs")]
8#[cfg_attr(windows, path = "windows.rs")]
9mod implementation;
10
11struct KillProvider;
12
13#[gravel_provider("kill")]
14impl Provider for KillProvider {
15	fn new(_config: &PluginConfigAdapter<'_>) -> Self {
16		Self {}
17	}
18
19	fn query(&self, _query: &str) -> ProviderResult {
20		let hits = match implementation::query() {
21			Ok(hits) => hits,
22			Err(err) => {
23				log::error!("unable to query running processes: {err}");
24				return ProviderResult::empty();
25			}
26		};
27
28		ProviderResult::new(hits)
29	}
30}
31
32fn get_hit(name: &str, pid: Pid, cmdline: &str) -> SimpleHit {
33	let title = format!("{name} - {pid}");
34
35	SimpleHit::new(title, cmdline, move |_, ctx| do_kill(pid, ctx))
36}
37
38fn do_kill(pid: Pid, context: RefDynHitActionContext<'_>) {
39	log::debug!("attempting to kill PID {pid}");
40
41	implementation::kill_process(pid)
42		.inspect_err(|e| log::error!("unable to kill PID {pid}: {e}"))
43		.ok();
44
45	context.refresh_frontend();
46}