diff --git a/src/config/mod.rs b/src/config/mod.rs index 11661ff..3fba57e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -44,11 +44,9 @@ use crate::modules::workspaces::WorkspacesModule; use crate::modules::{AnyModuleFactory, ModuleFactory, ModuleInfo}; use cfg_if::cfg_if; -use color_eyre::{Help, Report, Result}; +use color_eyre::Result; use serde::Deserialize; use std::collections::HashMap; -use std::process::{Command, Stdio}; -use tracing::error; #[cfg(feature = "schema")] use schemars::JsonSchema; @@ -441,21 +439,3 @@ pub const fn default_true() -> bool { pub fn default_launch_command() -> String { String::from("gtk-launch {app_name}") } - -pub fn launch_command(file_name: &str, str: &str) { - let expanded = str.replace("{app_name}", file_name); - let launch_command_parts: Vec<&str> = expanded.split_whitespace().collect(); - if let Err(err) = Command::new(&launch_command_parts[0]) - .args(&launch_command_parts[1..]) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - { - error!( - "{:?}", - Report::new(err) - .wrap_err("Failed to run launch command.") - .suggestion("Perhaps the applications file is invalid?") - ); - } -} diff --git a/src/desktop_file.rs b/src/desktop_file.rs index 3fdf346..8b0a509 100644 --- a/src/desktop_file.rs +++ b/src/desktop_file.rs @@ -1,12 +1,13 @@ use crate::spawn; -use color_eyre::Result; +use color_eyre::{Help, Report, Result}; use std::collections::HashMap; use std::env; use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; use std::sync::Arc; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::sync::Mutex; -use tracing::debug; +use tracing::{debug, error}; use walkdir::{DirEntry, WalkDir}; #[derive(Debug, Clone)] @@ -323,6 +324,25 @@ fn files(dir: &Path) -> Vec { .collect() } +/// Starts a `.desktop` file with the provided formatted command. +pub fn open_program(file_name: &str, str: &str) { + let expanded = str.replace("{app_name}", file_name); + let launch_command_parts: Vec<&str> = expanded.split_whitespace().collect(); + if let Err(err) = Command::new(&launch_command_parts[0]) + .args(&launch_command_parts[1..]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + { + error!( + "{:?}", + Report::new(err) + .wrap_err("Failed to run launch command.") + .suggestion("Perhaps the applications file is invalid?") + ); + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/modules/launcher/mod.rs b/src/modules/launcher/mod.rs index 5b6694e..0bdfbb2 100644 --- a/src/modules/launcher/mod.rs +++ b/src/modules/launcher/mod.rs @@ -8,8 +8,9 @@ use super::{Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, Wid use crate::channels::{AsyncSenderExt, BroadcastReceiverExt}; use crate::clients::wayland::{self, ToplevelEvent}; use crate::config::{ - CommonConfig, EllipsizeMode, LayoutConfig, TruncateMode, default_launch_command, launch_command, + CommonConfig, EllipsizeMode, LayoutConfig, TruncateMode, default_launch_command, }; +use crate::desktop_file::open_program; use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt}; use crate::modules::launcher::item::ImageTextButton; use crate::modules::launcher::pagination::{IconContext, Pagination}; @@ -387,7 +388,7 @@ impl Module for LauncherModule { if let ItemEvent::OpenItem(app_id) = event { match desktop_files.find(&app_id).await { Ok(Some(file)) => { - launch_command(&file.file_name, &launch_command_str); + open_program(&file.file_name, &launch_command_str); } Ok(None) => warn!("Could not find applications file for {}", app_id), Err(err) => error!("Failed to find parse file for {}: {}", app_id, err), diff --git a/src/modules/menu/ui.rs b/src/modules/menu/ui.rs index e311910..7c7cb1e 100644 --- a/src/modules/menu/ui.rs +++ b/src/modules/menu/ui.rs @@ -1,7 +1,7 @@ use super::MenuEntry; use crate::channels::AsyncSenderExt; use crate::config::TruncateMode; -use crate::config::launch_command; +use crate::desktop_file::open_program; use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt}; use crate::modules::ModuleUpdateEvent; use crate::script::Script; @@ -100,7 +100,7 @@ where let tx = tx.clone(); button.connect_clicked(move |_button| { - launch_command(&file_name, &command); + open_program(&file_name, &command); sub_menu.hide(); tx.send_spawn(ModuleUpdateEvent::ClosePopup);