gravel_provider_kill/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! gravel's process killer
//! Lists running processes on your system and will allow you to kill them.

use gravel_ffi::prelude::*;
use implementation::Pid;

#[cfg_attr(target_os = "linux", path = "linux.rs")]
#[cfg_attr(windows, path = "windows.rs")]
mod implementation;

struct KillProvider;

#[gravel_provider("kill")]
impl Provider for KillProvider {
	fn new(_config: &PluginConfigAdapter<'_>) -> Self {
		Self {}
	}

	fn query(&self, _query: &str) -> ProviderResult {
		let hits = match implementation::query() {
			Ok(hits) => hits,
			Err(err) => {
				log::error!("unable to query running processes: {err}");
				return ProviderResult::empty();
			}
		};

		ProviderResult::new(hits)
	}
}

fn get_hit(name: &str, pid: Pid, cmdline: &str) -> SimpleHit {
	let title = format!("{name} - {pid}");

	SimpleHit::new(title, cmdline, move |_, ctx| do_kill(pid, ctx))
}

fn do_kill(pid: Pid, context: RefDynHitActionContext<'_>) {
	log::debug!("attempting to kill PID {pid}");

	implementation::kill_process(pid)
		.inspect_err(|e| log::error!("unable to kill PID {pid}: {e}"))
		.ok();

	context.refresh_frontend();
}