Skip to content

Commit 27b5a19

Browse files
committed
[Applications] Reworked terminal configuration to allow for more terminal possibilities
1 parent 6bdba43 commit 27b5a19

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

plugins/applications/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Config(
1818
max_entries: 5,
1919
// The terminal used for running terminal based desktop entries, if left as `None` a static list of terminals is used
2020
// to determine what terminal to use.
21-
terminal: Some("alacritty"),
21+
terminal: Some(Terminal(
22+
// The main terminal command
23+
command: "alacritty",
24+
// What arguments should be passed to the terminal process to run the command correctly
25+
// {} is replaced with the command in the desktop entry
26+
args: "-e {}",
27+
)),
2228
)
23-
```
29+
```

plugins/applications/src/lib.rs

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ use std::{env, fs, process::Command};
99
pub struct Config {
1010
desktop_actions: bool,
1111
max_entries: usize,
12-
terminal: Option<String>,
12+
terminal: Option<Terminal>,
13+
}
14+
15+
#[derive(Deserialize)]
16+
pub struct Terminal {
17+
command: String,
18+
args: String,
1319
}
1420

1521
impl Default for Config {
@@ -29,8 +35,6 @@ pub struct State {
2935

3036
mod scrubber;
3137

32-
const SENSIBLE_TERMINALS: &[&str] = &["alacritty", "foot", "kitty", "wezterm", "wterm"];
33-
3438
#[handler]
3539
pub fn handler(selection: Match, state: &State) -> HandleResult {
3640
let entry = state
@@ -48,18 +52,58 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
4852
if entry.term {
4953
match &state.config.terminal {
5054
Some(term) => {
51-
if let Err(why) = Command::new(term).arg("-e").arg(&entry.exec).spawn() {
55+
if let Err(why) = Command::new("sh")
56+
.arg("-c")
57+
.arg(format!(
58+
"{} {}",
59+
term.command,
60+
term.args.replace("{}", &entry.exec)
61+
))
62+
.spawn()
63+
{
5264
eprintln!("Error running desktop entry: {}", why);
5365
}
5466
}
5567
None => {
56-
for term in SENSIBLE_TERMINALS {
57-
if Command::new(term)
58-
.arg("-e")
59-
.arg(&entry.exec)
60-
.spawn()
61-
.is_ok()
68+
let sensible_terminals = &[
69+
Terminal {
70+
command: "alacritty".to_string(),
71+
args: "-e {}".to_string(),
72+
},
73+
Terminal {
74+
command: "foot".to_string(),
75+
args: "-e \"{}\"".to_string(),
76+
},
77+
Terminal {
78+
command: "kitty".to_string(),
79+
args: "-e \"{}\"".to_string(),
80+
},
81+
Terminal {
82+
command: "wezterm".to_string(),
83+
args: "-e \"{}\"".to_string(),
84+
},
85+
Terminal {
86+
command: "wterm".to_string(),
87+
args: "-e \"{}\"".to_string(),
88+
},
89+
];
90+
for term in sensible_terminals {
91+
if Command::new("which")
92+
.arg(&term.command)
93+
.output()
94+
.is_ok_and(|output| output.status.success())
6295
{
96+
if let Err(why) = Command::new("sh")
97+
.arg("-c")
98+
.arg(format!(
99+
"{} {}",
100+
term.command,
101+
term.args.replace("{}", &entry.exec)
102+
))
103+
.spawn()
104+
{
105+
eprintln!("Error running desktop entry: {}", why);
106+
}
63107
break;
64108
}
65109
}
@@ -72,13 +116,16 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
72116
.arg("-c")
73117
.arg(&entry.exec)
74118
.current_dir(if let Some(path) = &entry.path {
75-
if path.exists() { path } else { current_dir }
119+
if path.exists() {
120+
path
121+
} else {
122+
current_dir
123+
}
76124
} else {
77125
current_dir
78126
})
79127
.spawn()
80-
}
81-
{
128+
} {
82129
eprintln!("Error running desktop entry: {}", why);
83130
}
84131

0 commit comments

Comments
 (0)