onagre_launcher_plugins/
lib.rs

1// SPDX-License-Identifier: GPL-3.0-only
2// Copyright © 2021 System76
3
4pub mod calc;
5pub mod desktop_entries;
6pub mod files;
7pub mod find;
8pub mod pulse;
9pub mod recent;
10pub mod scripts;
11pub mod terminal;
12pub mod web;
13
14use onagre_launcher::PluginResponse;
15use std::{borrow::Cow, ffi::OsStr, future::Future, path::Path, process::Stdio};
16use tokio::io::{AsyncWrite, AsyncWriteExt};
17
18pub async fn send<W: AsyncWrite + Unpin>(tx: &mut W, response: PluginResponse) {
19    if let Ok(mut bytes) = serde_json::to_string(&response) {
20        bytes.push('\n');
21        let _ = tx.write_all(bytes.as_bytes()).await;
22    }
23}
24
25/// Run both futures and take the output of the first one to finish.
26pub async fn or<T>(future1: impl Future<Output = T>, future2: impl Future<Output = T>) -> T {
27    futures::pin_mut!(future1);
28    futures::pin_mut!(future2);
29
30    futures::future::select(future1, future2)
31        .await
32        .factor_first()
33        .0
34}
35
36/// Fetch the mime for a given path
37pub fn mime_from_path(path: &Path) -> Cow<'static, str> {
38    if path.is_dir() {
39        Cow::Borrowed("inode/directory")
40    } else if let Some(guess) = new_mime_guess::from_path(path).first() {
41        Cow::Owned(guess.essence_str().to_owned())
42    } else {
43        Cow::Borrowed("text/plain")
44    }
45}
46
47/// Launches a file with its default application via `xdg-open`.
48pub fn xdg_open<S: AsRef<OsStr>>(file: S) {
49    let _ = tokio::process::Command::new("xdg-open")
50        .arg(file)
51        .stdout(Stdio::null())
52        .stderr(Stdio::null())
53        .spawn();
54}