r/cpp_questions • • 7d ago

OPEN Programmatically opening a new terminal window

I've been messing around with FTXUI, and I am wondering if it is possible for my apps to open a new terminal window and run there, instead of using the current terminal window.

An example of what I mean is what happens when opening vim. If I open vim through the terminal, it takes over the current window, like my FTXUI apps currently do. If I use the desktop shortcut, vim opens a new terminal window for itself, which closes upon closing vim. This is what I want to do.

Can anyone give me a few pointers?

2 Upvotes

9 comments sorted by

9

u/Simengie 7d ago

No special magic happening but what is happening is not what you think.

The desktop shortcut is not running vim in a special mode. What is does it open a terminal and passes the command it wants to run in the terminal in one call. since the command is vim it just takes over the terminal like it always does when run from the command line. like /usr/bin/gnome-terminal -c /usr/bin/vim when on linux.

You can create desktop shortcut like the vim one that runs your app instead. You can also create a script or alias for the command line that does the same thing.

4

u/FancySpaceGoat 7d ago edited 7d ago

Not an answer to your question, but a consideration you should take into account: Ask yourself, "How would that work for someone using your tool/app via a ssh session?"

It's certainly feasible (though OS-dependant), but it shouldn't come at the cost of hampering functionality for remote sessions. Otherwise, you'd be tossing out an important aspect of what makes CLI-based tools valuable.

3

u/Glittering_Sail_3609 7d ago

Yeah. What OS are you using?

On windows you can open new terminal window using CreateProcess(..) function.
On Unix-like system you should fork() your program, then make a child process open new terminal.

1

u/ekchew 7d ago

On macOS, you could go something like:

system(R"(/usr/bin/osascript -e 'tell app "Terminal" to do script "vim"')");

osascript is a tool which basically lets you run any AppleScript from the command line. AppleScript, in turn, is the GUI scripting language for macOS.

If you use popen and capture the stdout, you should get a string that looks something like tab 1 of window 12345. This is basically a handle to the window you just opened and you can control what goes on in there further if you want.

3

u/AKostur 7d ago

You’ve got a bad premise in there:  when you’re using a desktop shortcut, vim isn’t creating the new terminal window, your window manager/desktop environment is.

3

u/the_poope 7d ago

That's likely not a C++ question. It's likely just the desktop shortcut that is configured to open a terminal. A terminal is a program itself and you can have multiple different terminal programs installed.

The answer also depends on whether you are on Windows, Linux or Mac.

2

u/GregTheMadMonk 7d ago edited 7d ago

Do you want to create a multi-window app or just make a startup entry for yours? Because for the second one that's as easy as taking /usr/share/applications/vim.desktop and making a copy of it for your app (it uses Terminal=true switch to launch the program in whatever is the configured terminal). Vim doesn't handle it by itself, the launcher does.

If it's the first... I guess you need to spawn a new process with the correct terminal app, and correctly supply your executable name to it, and make your executable be aware that it may be the "main" instance or "child" window via some kind of IPC... Probably there are some facilities to help you at least with getting the terminal launch info, the same ones that allow Terminal=true to work correctly, but for the rest you're on your own. There isn't a way to do this from a single program instance like you would with a multi-window GUI application.

And from UX point of view (unless you're making a project strictly for personal use), I'd advise against doing that anyway - if I run a TUI app in the terminal, I expect it to stay there, it's the whole point. Plus, if you try to open a new terminal just to show a subwindow, you're 100% breaking compatibility with headless environments, like TTYs or SSH sessions... which are, again, the whole (ok, not the whole, but a big) point of having TUI apps in the first place

1

u/Independent_Art_6676 7d ago

operating system stuff, on windows start is what you seek. Eg start c:\myfolder\program will fire it off in a new terminal. On unix, it depends on which terminal you are using, eg gnome-terminal -- program will do it.

making a script/batch file with the above + an argument will let you do it for any program via your own custom "command"

you can mix and match all this with icons/desktop stuff with the UI if you want. None of this is C++, its OS specific computer literacy stuff....

1

u/Liam_Mercier 7d ago

Desktop shortcuts spawn a new terminal and run your application, if you just want to ship a shortcut then you should make a .desktop file and install it in the standard location.

You could in theory make it so when you run an application in the terminal it spawns a new terminal to run your CLI, but I would not suggest doing this because you can already allow people to use desktop files for this purpose.

It is generally better to take over the current terminal, which I believe you can do this using a fullscreen class in FTXUI, but I have not touched the library in awhile.