Skip to content

Commit 786f539

Browse files
authored
plugins/applications: add preprocess_exec_script option (#201)
1 parent 416bc4a commit 786f539

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

plugins/applications/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ Simply search for the application you wish to launch.
1515
Config(
1616
// Also show the Desktop Actions defined in the desktop files, e.g. "New Window" from LibreWolf
1717
desktop_actions: true,
18-
max_entries: 5,
18+
19+
max_entries: 5,
20+
21+
// A command to preprocess the command from the desktop file. The commands should take arguments in this order:
22+
// command_name <term|no-term> <command>
23+
preprocess_exec_script: Some("/home/user/.local/share/anyrun/preprocess_application_command.sh")
24+
1925
// The terminal used for running terminal based desktop entries, if left as `None` a static list of terminals is used
2026
// to determine what terminal to use.
2127
terminal: Some(Terminal(

plugins/applications/src/lib.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use anyrun_plugin::{anyrun_interface::HandleResult, *};
33
use fuzzy_matcher::FuzzyMatcher;
44
use scrubber::DesktopEntry;
55
use serde::Deserialize;
6-
use std::{env, fs, process::Command};
6+
use std::{env, fs, path::PathBuf, process::Command};
77

88
#[derive(Deserialize)]
99
pub struct Config {
1010
desktop_actions: bool,
1111
max_entries: usize,
1212
terminal: Option<Terminal>,
13+
preprocess_exec_script: Option<PathBuf>,
1314
}
1415

1516
#[derive(Deserialize)]
@@ -23,6 +24,7 @@ impl Default for Config {
2324
Self {
2425
desktop_actions: false,
2526
max_entries: 5,
27+
preprocess_exec_script: None,
2628
terminal: None,
2729
}
2830
}
@@ -49,6 +51,26 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
4951
})
5052
.unwrap();
5153

54+
let exec = if let Some(script) = &state.config.preprocess_exec_script {
55+
let output = Command::new("sh")
56+
.arg("-c")
57+
.arg(format!(
58+
"{} {} {}",
59+
script.display(),
60+
if entry.term { "term" } else { "no-term" },
61+
&entry.exec
62+
))
63+
.output()
64+
.unwrap_or_else(|why| {
65+
eprintln!("Error running preprocess script: {}", why);
66+
std::process::exit(1);
67+
});
68+
69+
String::from_utf8_lossy(&output.stdout).trim().to_string()
70+
} else {
71+
entry.exec.clone()
72+
};
73+
5274
if entry.term {
5375
match &state.config.terminal {
5476
Some(term) => {
@@ -57,7 +79,7 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
5779
.arg(format!(
5880
"{} {}",
5981
term.command,
60-
term.args.replace("{}", &entry.exec)
82+
term.args.replace("{}", &exec)
6183
))
6284
.spawn()
6385
{
@@ -98,7 +120,7 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
98120
.arg(format!(
99121
"{} {}",
100122
term.command,
101-
term.args.replace("{}", &entry.exec)
123+
term.args.replace("{}", &exec)
102124
))
103125
.spawn()
104126
{
@@ -114,7 +136,7 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
114136

115137
Command::new("sh")
116138
.arg("-c")
117-
.arg(&entry.exec)
139+
.arg(&exec)
118140
.current_dir(if let Some(path) = &entry.path {
119141
if path.exists() {
120142
path

0 commit comments

Comments
 (0)