skip to Main Content

I have this piece of code part of my desktop app that I am building using Rust+Tauri(A docker GUI client). I want to allow the user to click on the button and it should spwan a terminal app that basically runs the exec command.

The Problem:
I have figured out how to do it for my system(running Linux/Pop!os) but how do I make it work on all the various linux distros (for now, cross-os support will be later). I know I have "gnome-terminal" app that I can hardcoded it & it is working.

let mut terminal_app: String = String::from("gnome-terminal"); // How do I get this dynamically?

let docker_command = format!("docker exec -it {} sh", container_name);

let mut command = std::process::Command::new(terminal_app);
let args = ["--", "bash", "-c", &docker_command];;

command.args(&args);
match command.spawn() {
    Ok(_) => Ok("Exec command executed".to_string()),
    Err(err) => Err(format!("Cannot run exec command: {}", err.to_string())),
}

I will be grateful for your help…

2

Answers


  1. In you app settings ask for the name of the terminal and command line to execute and external command, probably using placeholders.

    There’s also xdg-terminal-exec but not commonly installed.

    Finally, you can also rely on environment vars like TERMINAL or something more specific for. your app.

    Login or Signup to reply.
  2. You can create a list of popular terminal emulators (like gnome-terminal, xterm, konsole, xfce4-terminal, etc.) and check which one is installed on the system. This can be done using the which command to see if the terminal is available in the user’s PATH, something like this

    Function detect_terminal():
        List of common_terminals = ["gnome-terminal", "konsole", "xfce4-terminal", "xterm", "alacritty"]
    
        For each terminal in common_terminals:
            If terminal exists on system (use the 'which' command):
                Return terminal name
    
        Return default terminal (e.g., "gnome-terminal") if none found
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search