From 65c1742a889dcb9db2d65f2f7548e8f09d628165 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 16 May 2024 10:24:11 -0400 Subject: [PATCH] feat: prefer cosmic-term in terminal plugin --- plugins/src/terminal/mod.rs | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/plugins/src/terminal/mod.rs b/plugins/src/terminal/mod.rs index 15df6ec..51bc1b4 100644 --- a/plugins/src/terminal/mod.rs +++ b/plugins/src/terminal/mod.rs @@ -116,8 +116,47 @@ impl App { } fn detect_terminal() -> (PathBuf, &'static str) { + use freedesktop_desktop_entry::DesktopEntry; use std::fs::read_link; + let terminal_apps: Vec<_> = + freedesktop_desktop_entry::Iter::new(freedesktop_desktop_entry::default_paths()) + .filter_map(|path| { + std::fs::read_to_string(&path).ok().and_then(|input| { + DesktopEntry::decode(&path, &input).ok().and_then(|de| { + if de.no_display() + || de + .categories() + .map(|c| c.split_terminator(';').all(|c| c != "TerminalEmulator")) + .unwrap_or(true) + || de.exec().is_none() + { + return None; + } + + Some((de.id().to_owned(), de.exec().unwrap().to_owned())) + }) + }) + }) + .collect(); + + for id in ["com.system76.CosmicTerm"] { + for (terminal_id, exec) in &terminal_apps { + if terminal_id.as_str() == id { + return (PathBuf::from(exec), "-e"); + } + } + } + + if let Some((id, exec)) = terminal_apps.first() { + let arg = if id == "org.gnome.Terminal" { + "--" + } else { + "-e" + }; + return (PathBuf::from(exec), arg); + } + const SYMLINK: &str = "/usr/bin/x-terminal-emulator"; if let Ok(found) = read_link(SYMLINK) {