use std::env;
use std::path::Path;
use std::process::Command;
use crate::prelude::command_exists_native;
pub fn open_terminal_by_desktop(function_command: &str) {
if function_command.is_empty() {
return;
}
if Path::new("/mnt/c/Users").exists() {
let wsl_cmd = format!("cmd.exe /C start cmd.exe /K \"wsl.exe {}\"", function_command);
let _ = Command::new("sh").arg("-c").arg(wsl_cmd).spawn();
return;
}
spawn_terminal(function_command);
}
#[cfg(target_os = "windows")]
fn spawn_terminal(func_cmd: &str) {
let cmd = format!("start cmd.exe /K \"{}\"", func_cmd);
let _ = Command::new("cmd").arg("/C").arg(cmd).spawn();
}
#[cfg(target_os = "macos")]
fn spawn_terminal(func_cmd: &str) {
let script = format!("tell application \"Terminal\" to do script \"{}\"", func_cmd);
let _ = Command::new("osascript").arg("-e").arg(script).spawn();
}
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
fn spawn_terminal(func_cmd: &str) {
let desktop = env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().to_uppercase();
let (term, args) = if desktop.contains("GNOME") || desktop.contains("MATE") || desktop.contains("X-CINNAMON") {
(if desktop.contains("MATE") {
"mate-terminal"
}
else {
"gnome-terminal"
}, "--")
}
else if desktop.contains("KDE") {
("konsole", "-e")
}
else if desktop.contains("LXDE") || desktop.contains("LXQT") {
("lxterminal", "-e")
}
else {
let fallbacks = vec![
("gnome-terminal", "--"),
("x-terminal-emulator", "-e"),
("konsole", "-e"),
("xfce4-terminal", "-e"),
("xterm", "-e"),
];
let mut found = None;
for (t, a) in fallbacks {
if command_exists_native(t) {
found = Some((t, a));
break;
}
}
match found {
Some(f) => f,
None => {
eprintln!("[ERROR] No supported terminal found for desktop: {}", desktop);
return;
}
}
};
let full_cmd = format!("{}; exec bash", func_cmd);
let _ = Command::new(term)
.arg(args)
.arg("bash")
.arg("-c")
.arg(full_cmd)
.spawn();
}