chore(plugins): Refactoring to share mime_from_path function

This commit is contained in:
Michael Aaron Murphy 2021-08-19 20:21:48 +02:00
parent 8f25fcaad1
commit 21ced33d7d
3 changed files with 16 additions and 19 deletions

View file

@ -1,7 +1,7 @@
use futures_lite::prelude::*;
use pop_launcher::*;
use smol::Unblock;
use std::{borrow::Cow, collections::BTreeMap, io, path::PathBuf};
use std::{collections::BTreeMap, io, path::PathBuf};
#[derive(Clone)]
struct Item {
@ -108,14 +108,7 @@ impl App {
let path = entry.path();
if let Some(name) = path.file_name().and_then(|x| x.to_str()) {
items.push(Item {
icon: IconSource::Mime(if path.is_dir() {
Cow::Borrowed("inode/directory")
} else if let Some(guess) = new_mime_guess::from_path(&path).first()
{
Cow::Owned(guess.essence_str().to_owned())
} else {
Cow::Borrowed("text/plain")
}),
icon: IconSource::Mime(crate::mime_from_path(&path)),
name: name.to_owned(),
description: path
.metadata()

View file

@ -3,7 +3,6 @@ use pop_launcher::*;
use postage::mpsc;
use postage::prelude::{Sink, Stream};
use smol::process::{Child, ChildStdout, Command, Stdio};
use std::borrow::Cow;
use std::cell::Cell;
use std::io;
use std::path::PathBuf;
@ -130,13 +129,7 @@ impl SearchContext {
id,
description,
name,
icon: Some(IconSource::Mime(if path.is_dir() {
Cow::Borrowed("inode/directory")
} else if let Some(guess) = new_mime_guess::from_path(&path).first() {
Cow::Owned(guess.essence_str().to_owned())
} else {
Cow::Borrowed("text/plain")
})),
icon: Some(IconSource::Mime(crate::mime_from_path(&path))),
..Default::default()
});

View file

@ -8,7 +8,7 @@ pub mod web;
use futures_lite::{AsyncWrite, AsyncWriteExt};
use pop_launcher::PluginResponse;
use std::ffi::OsStr;
use std::{borrow::Cow, ffi::OsStr, path::Path};
pub async fn send<W: AsyncWrite + Unpin>(tx: &mut W, response: PluginResponse) {
if let Ok(mut bytes) = serde_json::to_string(&response) {
@ -17,7 +17,18 @@ pub async fn send<W: AsyncWrite + Unpin>(tx: &mut W, response: PluginResponse) {
}
}
/// Fetch the mime for a given path
pub fn mime_from_path(path: &Path) -> Cow<'static, str> {
if path.is_dir() {
Cow::Borrowed("inode/directory")
} else if let Some(guess) = new_mime_guess::from_path(&path).first() {
Cow::Owned(guess.essence_str().to_owned())
} else {
Cow::Borrowed("text/plain")
}
}
/// Launches a file with its default appplication via `xdg-open`.
pub fn xdg_open<S: AsRef<OsStr>>(file: S) {
let _ = smol::process::Command::new("xdg-open").arg(file).spawn();
}
}