Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/applications/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ anyrun-plugin = { path = "../../anyrun-plugin" }
fuzzy-matcher = "0.3.7"
ron = "0.8.0"
serde = { features = [ "derive" ], version = "1.0.228" }
shell-words = "1.1.1"
sublime_fuzzy = "0.7.0"
89 changes: 52 additions & 37 deletions plugins/applications/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use abi_stable::std_types::{ROption, RString, RVec};
use anyrun_plugin::{anyrun_interface::HandleResult, *};
use fuzzy_matcher::FuzzyMatcher;
use scrubber::DesktopEntry;
use scrubber::{DesktopEntry, lower_exec};
use serde::Deserialize;
use std::{env, fs, path::PathBuf, process::Command};
use shell_words;

#[derive(Deserialize)]
pub struct Config {
Expand Down Expand Up @@ -54,36 +55,54 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
})
.unwrap();

let exec = if let Some(script) = &state.config.preprocess_exec_script {
let output = Command::new("sh")
.arg("-c")
.arg(format!(
"{} {} {}",
script.display(),
if entry.term { "term" } else { "no-term" },
&entry.exec
))
let (command, argv) = match lower_exec(&entry) {
Ok((command, argv)) => (command, argv),
Err(error) => {
eprintln!("[applications] Unable to parse the exec key `{}`: {}", &entry.exec, error.0);
return HandleResult::Close
}
};

let (command, argv) = if let Some(script) = &state.config.preprocess_exec_script {
let output = match Command::new(script.as_os_str())
.arg(if entry.term { "term" } else { "no-term" })
.arg(command)
.args(argv)
.output()
.unwrap_or_else(|why| {
{
Ok(output) => output,
Err(why) => {
eprintln!("[applications] Error running preprocess script: {}", why);
std::process::exit(1);
});
return HandleResult::Close
}
};

let args = match shell_words::split(String::from_utf8_lossy(&output.stdout).trim()) {
Ok(args) => args,
Err(error) => {
eprintln!("[applications] Unable to parse the output of the preprocessing script: {}", error);
return HandleResult::Close
}
};

String::from_utf8_lossy(&output.stdout).trim().to_string()
let mut it = args.into_iter();
let Some(command) = it.next() else {
eprintln!("[applications] Empty output of preprocessing script.");
return HandleResult::Close
};
let argv = it.collect();
(command, argv)
} else {
entry.exec.clone()
(command, argv)
};

if entry.term {
match &state.config.terminal {
Some(term) => {
if let Err(why) = Command::new("sh")
.arg("-c")
.arg(format!(
"{} {}",
term.command,
term.args.replace("{}", &exec)
))
if let Err(why) = Command::new(&term.command)
.arg(&term.args)
.arg(command)
.args(argv)
.spawn()
{
eprintln!("[applications] Error running desktop entry: {}", why);
Expand All @@ -93,23 +112,23 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
let sensible_terminals = &[
Terminal {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all the terminals actually accept the commands in this way?

command: "alacritty".to_string(),
args: "-e {}".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "foot".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "kitty".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "wezterm".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "wterm".to_string(),
args: "-e \"{}\"".to_string(),
args: "-e".to_string(),
},
Terminal {
command: "ghostty".to_string(),
Expand All @@ -122,13 +141,10 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
.output()
.is_ok_and(|output| output.status.success())
{
if let Err(why) = Command::new("sh")
.arg("-c")
.arg(format!(
"{} {}",
term.command,
term.args.replace("{}", &exec)
))
if let Err(why) = Command::new(&term.command)
.arg(&term.args)
.arg(command)
.args(argv)
.spawn()
{
eprintln!("Error running desktop entry: {}", why);
Expand All @@ -141,9 +157,8 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
} else if let Err(why) = {
let current_dir = &env::current_dir().unwrap();

Command::new("sh")
.arg("-c")
.arg(&exec)
Command::new(command)
.args(argv)
.current_dir(match &entry.path {
Some(path) if path.exists() => path,
_ => current_dir,
Expand Down
Loading