ThatCoolCoder
ThatCoolCoder

Reputation: 288

Linux - Open terminal for input/output

I'm coding a Rust app and since it's fairly small and there don't appear to be any stable UI frameworks, I've made it run in the console using println! and whatnot for input/output. However since this program is intended to be used by people directly after downloading from the internet (due to its use case), they're likely to just double click on it instead of navigating to their downloads directory in a terminal and running it from there.

This is a problem because on Linux, it runs in the background waiting for input and looks like it's not working. On Windows, Rust programs do open in CMD by default. (and in fact many of the search results for my question were about disabling this behavior - the exact opposite of what I want!).

So is it possible to somehow make my application open in the system's default terminal? My preferred way would be to somehow embed in the executable to open in terminal (similar to the -mconsole compiler flag on MinGW). Otherwise would it be possible to detect it's in the background and fork it into a terminal? If that's not possible then is it at least possible to detect that the app is not running in a terminal and throw up a message box telling the user to run in a terminal?

My app is cross-platform but I'm OK with writing code conditionally compiled on one OS.

Upvotes: 0

Views: 1037

Answers (1)

battlmonstr
battlmonstr

Reputation: 6300

One typical way would be to distribute a program.sh along with your executable. If .sh extension is bound to opening a terminal in their window manager of choice, it would open automatically. If not - it is enough of a hint for running it from the shell.

Without this file you could:

  1. Detect if the program is already running inside a terminal can be done with isatty(). There's a crate for it.
  2. If not, spawn the terminal app process (see process::Command) and relaunch the program with it by passing its path to the terminal command line options. As @Caesar mentioned there's a bunch of popular terminals that you might want to check for presence on Linux. If nothing is found, xterm could sometimes be a fallback.

Upvotes: 1

Related Questions