mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.23k stars 243 forks source link

Unable to figure out how to close an application - explanations missing in the docs #523

Open notsodistantworlds opened 3 years ago

notsodistantworlds commented 3 years ago

The docs are a bit of a stub in general, but the following 2 things in the docs (or the lack of them, should i say) stand out:

I thought i could use these emulated terminals to have a new window that i could close to stop the program.

I know the docs suggest creating a button element and adding a runnable to it which would stop the program, but that seems hacky and unnecessary. Every terminal already has an obvious way of doing that, CTRL+C. And if not that, then creating a terminal emulator window and then closing it should work well enough as a substitute, at least for my use case. But, again, that doesn't work, either.

avl42 commented 3 years ago

I believe to spot a few misunderstandings here...

Not all Terminal-applications are terminated by Ctrl-C. There are mostly two categories of terminal applications: plain stdin/-out/-err apps (which almost all terminate on Ctrl-C) and so called "text GUI" apps, like e.g. editors, mail-apps, irc-clients, nntp-clients, which only rarely quit on Ctrl-C. Lanterna is a library to create the second type of applications.

AWT and Swing terminals can be mostly thought of as a "workaround" for platforms without proper native terminals, although there are also legit reasons why one would use them on Linux,Unix,Mac...

It is not necessary to use buttons - I know because my current lanterna-using application just makes use of the screen-layer, not the TextGui layer.

notsodistantworlds commented 3 years ago

Well alright, but still, is it not possible to make this configurable? Quitting on CTRL+C sounds reasonable enough and should at least be an option.

Could you provide some example code for starting a Swing terminal? Or a link to the docs, perhaps i missed it. So far i've not been able to start an emulated terminal.

mabe02 commented 3 years ago

You can set a custom CTRL+C behavior for "real" text terminal through UnixLikeTerminal.CtrlCBehaviour, which determines if the signal should be trapped and interpreted by your application on passed on to the JVM (which effectively kills the app).

There is plenty of example code for creating a SwingTerminal/AWTTerminal if you check under the test directory of the source. Although to avoid repetition we have a utility class that contains most of the initialization logic for most of the tests. If you want to display a simple terminal emulator window, just create a SwingTerminalFrame like you would create any JFrame and set it to visible.