[Applications] Reworked terminal configuration to allow for more terminal possibilities

This commit is contained in:
Kirottu
2024-12-26 15:35:33 +02:00
parent 6bdba4377b
commit 27b5a1904a
2 changed files with 68 additions and 15 deletions

View File

@@ -18,6 +18,12 @@ Config(
max_entries: 5, max_entries: 5,
// The terminal used for running terminal based desktop entries, if left as `None` a static list of terminals is used // 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. // 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 {}",
)),
) )
``` ```

View File

@@ -9,7 +9,13 @@ use std::{env, fs, process::Command};
pub struct Config { pub struct Config {
desktop_actions: bool, desktop_actions: bool,
max_entries: usize, max_entries: usize,
terminal: Option<String>, terminal: Option<Terminal>,
}
#[derive(Deserialize)]
pub struct Terminal {
command: String,
args: String,
} }
impl Default for Config { impl Default for Config {
@@ -29,8 +35,6 @@ pub struct State {
mod scrubber; mod scrubber;
const SENSIBLE_TERMINALS: &[&str] = &["alacritty", "foot", "kitty", "wezterm", "wterm"];
#[handler] #[handler]
pub fn handler(selection: Match, state: &State) -> HandleResult { pub fn handler(selection: Match, state: &State) -> HandleResult {
let entry = state let entry = state
@@ -48,18 +52,58 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
if entry.term { if entry.term {
match &state.config.terminal { match &state.config.terminal {
Some(term) => { 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); eprintln!("Error running desktop entry: {}", why);
} }
} }
None => { None => {
for term in SENSIBLE_TERMINALS { let sensible_terminals = &[
if Command::new(term) Terminal {
.arg("-e") command: "alacritty".to_string(),
.arg(&entry.exec) args: "-e {}".to_string(),
.spawn() },
.is_ok() 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; break;
} }
} }
@@ -72,13 +116,16 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
.arg("-c") .arg("-c")
.arg(&entry.exec) .arg(&entry.exec)
.current_dir(if let Some(path) = &entry.path { .current_dir(if let Some(path) = &entry.path {
if path.exists() { path } else { current_dir } if path.exists() {
path
} else {
current_dir
}
} else { } else {
current_dir current_dir
}) })
.spawn() .spawn()
} } {
{
eprintln!("Error running desktop entry: {}", why); eprintln!("Error running desktop entry: {}", why);
} }