diff --git a/plugins/applications/README.md b/plugins/applications/README.md index ab77fe4..dbaca8d 100644 --- a/plugins/applications/README.md +++ b/plugins/applications/README.md @@ -18,6 +18,12 @@ Config( max_entries: 5, // The terminal used for running terminal based desktop entries, if left as `None` a static list of terminals is used // to determine what terminal to use. - terminal: Some("alacritty"), + terminal: Some(Terminal( + // The main terminal command + command: "alacritty", + // What arguments should be passed to the terminal process to run the command correctly + // {} is replaced with the command in the desktop entry + args: "-e {}", + )), ) -``` \ No newline at end of file +``` diff --git a/plugins/applications/src/lib.rs b/plugins/applications/src/lib.rs index 3dca60f..61b0af0 100644 --- a/plugins/applications/src/lib.rs +++ b/plugins/applications/src/lib.rs @@ -9,7 +9,13 @@ use std::{env, fs, process::Command}; pub struct Config { desktop_actions: bool, max_entries: usize, - terminal: Option, + terminal: Option, +} + +#[derive(Deserialize)] +pub struct Terminal { + command: String, + args: String, } impl Default for Config { @@ -29,8 +35,6 @@ pub struct State { mod scrubber; -const SENSIBLE_TERMINALS: &[&str] = &["alacritty", "foot", "kitty", "wezterm", "wterm"]; - #[handler] pub fn handler(selection: Match, state: &State) -> HandleResult { let entry = state @@ -48,18 +52,58 @@ pub fn handler(selection: Match, state: &State) -> HandleResult { if entry.term { match &state.config.terminal { Some(term) => { - if let Err(why) = Command::new(term).arg("-e").arg(&entry.exec).spawn() { + if let Err(why) = Command::new("sh") + .arg("-c") + .arg(format!( + "{} {}", + term.command, + term.args.replace("{}", &entry.exec) + )) + .spawn() + { eprintln!("Error running desktop entry: {}", why); } } None => { - for term in SENSIBLE_TERMINALS { - if Command::new(term) - .arg("-e") - .arg(&entry.exec) - .spawn() - .is_ok() + let sensible_terminals = &[ + Terminal { + command: "alacritty".to_string(), + args: "-e {}".to_string(), + }, + Terminal { + command: "foot".to_string(), + args: "-e \"{}\"".to_string(), + }, + Terminal { + command: "kitty".to_string(), + args: "-e \"{}\"".to_string(), + }, + Terminal { + command: "wezterm".to_string(), + args: "-e \"{}\"".to_string(), + }, + Terminal { + command: "wterm".to_string(), + args: "-e \"{}\"".to_string(), + }, + ]; + for term in sensible_terminals { + if Command::new("which") + .arg(&term.command) + .output() + .is_ok_and(|output| output.status.success()) { + if let Err(why) = Command::new("sh") + .arg("-c") + .arg(format!( + "{} {}", + term.command, + term.args.replace("{}", &entry.exec) + )) + .spawn() + { + eprintln!("Error running desktop entry: {}", why); + } break; } } @@ -72,13 +116,16 @@ pub fn handler(selection: Match, state: &State) -> HandleResult { .arg("-c") .arg(&entry.exec) .current_dir(if let Some(path) = &entry.path { - if path.exists() { path } else { current_dir } + if path.exists() { + path + } else { + current_dir + } } else { current_dir }) .spawn() - } - { + } { eprintln!("Error running desktop entry: {}", why); }