Fun with text user interfaces



With one week to go, I have one more blog post in my budget for this year. Might as well make it something fun.

Text user interfaces always have a place even with today's advanced computers. They're fast, accessible, and can run over a network without ever being aware of it; a rare example of transparency. And there are several different ways to make one, at least in Linux.

Now if your language of choice has access to the curses API, or another terminal control library, guess you're all set. But it's often just not an option in your target environment. What to do then?

The venerable Dialog utility (based on ncurses, and maintained by the same person for the past couple of decades) is one option: a small program that can handle a variety of common interactions like choosing a file to open, or entering dates, while looking good and being easy enough to use. It's also easy to call from Python thanks to a popular wrapper library, but also from Tcl or shell script for example. Just make sure it's installed where you need it.

Kind of opposite from Dialog sits tput, that always comes with (n)curses, though usually in a separate package with various names. This is a low-level alternative, for when you just want to move the cursor here and there and show colorful text all over the screen. It also needs to run once per operation, and that adds up quickly. To fix that we must dig even deeper. Beware of Balrog.

Yes, I'm talking about sending ANSI escape codes directly to the terminal. It's simple enough for the most common operations; see the tutorial on the Bash Hackers Wiki for example (which also teaches tput), and there are many others. Also it works right away from any programming language, since you're literally just printing out text.

On the minus side, it's less portable, because even terminal emulators have various quirks. But in general it should be reliable enough. Not so much for something as basic as navigating a menu with the arrow keys, as opposed to typing in an option number then Enter. Surprisingly, that requires messy and even less portable code. In shell script it can be done with read -n, or similar, but the syntax and semantics differ from shell to shell. There is no right decision.

Which is my whole point here. What I'm going to choose for my next project depends on many factors. Dialog was famously employed in the Slackware-style installer that Debian also used for a while, so it's pretty damn capable. But it can also be clumsy, and if it just doesn't fit the flow of your script, you'll need an alternative. ANSI escape codes underlie terminal support in the standard libraries of many languages that would rather not depend on curses for various reasons. And tput might be worth using simply for the convenience factor.

Variety is good for the soul. It's also good for the health of an ecosystem, but mostly it's just plain fun to play with different things and see what looks best in your code. Remember when coding used to be fun, just a Basic interpreter between you and the metal?

Next time I'm going to look at the Dialog alternatives for X11. In the mean time, happy holidays, whatever you're celebrating, and stay safe.


Tags: Linux, programming