From 21ced33d7d4b85995d6e00399abbb66e99c39d24 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Thu, 19 Aug 2021 20:21:48 +0200 Subject: [PATCH] chore(plugins): Refactoring to share mime_from_path function --- plugins/src/files/mod.rs | 11 ++--------- plugins/src/find/mod.rs | 9 +-------- plugins/src/lib.rs | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/plugins/src/files/mod.rs b/plugins/src/files/mod.rs index 6fbbc78..99af062 100644 --- a/plugins/src/files/mod.rs +++ b/plugins/src/files/mod.rs @@ -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() diff --git a/plugins/src/find/mod.rs b/plugins/src/find/mod.rs index eb565f3..71359a7 100644 --- a/plugins/src/find/mod.rs +++ b/plugins/src/find/mod.rs @@ -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() }); diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index a31d8d1..cbbeefd 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -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(tx: &mut W, response: PluginResponse) { if let Ok(mut bytes) = serde_json::to_string(&response) { @@ -17,7 +17,18 @@ pub async fn send(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>(file: S) { let _ = smol::process::Command::new("xdg-open").arg(file).spawn(); -} +} \ No newline at end of file