Text-Based User Interfaces in Go: Give your console apps a face lift
Originally published at appliedgo.net on April 2, 2017.
Want to equip your command-line application with a nice visual user interface? TUI libraries are here to help.
Console applications usually take some parameters at start, and maybe some more input through basic console I/O. And that’s ok in most cases, though sometimes it would be great to have a visual user interface but without the code size and complexity of a full-blown Web app.
Text-Based User Interface libraries (or TUI libraries) meet this need. They bring panes, input, output, mouse support, graphics, and audio, to your terminal.
OK, so can we do a side-by-side test, please?
Originally, I planned to evaluate three to four TUI libraries for Go side-by-side, using a sample app definition with a specific set of features to be tested. I had a few requirements on the libraries: They should -
- be fairly complete TUI libraries, supporting flexible layouts, standard UI widgets like buttons, menus, text entry, text output, mouse support, etc.
- provide high-level abstractions (as opposed to only providing low-level UI primitives),
- be active projects (that is, the last commit should not date back to months or even years ago),
- be past the alpha stage, and
- have reasonable documentation or sample code. (No, API docs don’t count. I wanted at least a simple how-to-start tutorial that covers the basics.)
Turned out that none of the libraries I found passes all of the requirements.
So that’s it? No single all-in-one, high-level GUI lib with decent documentation?
Well, not if you really want all those nifty UI features at once. However, if you can forego some of the requirements, there is indeed a small selection of capable TUI libraries at your disposal that I’ll briefly introduce below.
Just one thing before we start.
The list is (and cannot) be complete. I used GitHub.com, awesome-go.com, GolangLibs.com, and LibHunt.com to find TUI libraries but still there is no guarantee that I found all relevant projects out there.
And then, of course, the above list of criteria is a very fuzzy filter, and you might have included a library that I ruled out because of this list.
But now let’s move on to the TUI library overview.
Bare bone TUI libraries
If you are fine with building your UI on top of a handful of UI primitives, there are two libraries that look fairly stable and complete:
termbox has a small and straightforward API. It uses a two-dimensional cell buffer to represent the terminal screen, with a few methods like setting cells, clearing the screen, or setting the cursor. All input, whether keyboard, mouse, or resize events, is tracked through a unified Event type.
As simple as the API might seem, it has not prevented others from creating interesting projects on top of
termbox, including higher-level TUI libraries (
termloop`), simple games, godit (a text editor) and hecate (a hex editor), and more.
The README makes no claims about supported platforms. The code includes some
syscall_*.go files, indicating that
termbox-go runs on Linux, macOS, Windows, OpenBSD, NetBSD, FreeBSD, and Dragonfly BSD.
- A fair range of supported platforms
- Popular base TUI layer for a number of TUI projects
According to the author,
tcell was born out of the need for certain features that
termbox-go does not provide, and patching
termbox-go turned out to not be a suitable way to go forward.
tcell claims to have a couple of advantages over
termbox-go, including additional functionality, better portability, better I/O, better support for mouse, Unicode, colors, and more. (I cannot comment on these claims, as I have tested neither
tcell in great depth.)
tcell is the TUI library behind the Micro editor, and is also used by godu.
According to the README,
tcell works on POSIX systems that provide a POSIX termios implementation with
/dev/tty, as well as on Windows.
The “POSIX” requirement includes Linux, macOS, FreeBSD, and Solaris, and certainly more systems that are not explicitly listed in the README.
- A wide range of suppored platforms
- A rich API
termui is one of the high-level TUI’s that are built on top of
termbox-go. It is specialized on displaying information in graphical form - as bar chart, line diagram, spark line, or as a gauge. Text output is also possible of course. The UI is built on the concept of widgets, or blocks, that contain exactly one type of data representation - text, charts, or diagrams.
termui provides a static layout and a grid layout. The grid layout can respond dynamically to resizing of the terminal window.
On the input side,
termui offers a simple event handling system that can react to key presses, window resizes, or timers.
What is missing from
termui to make it a “feature-complete” TUI toolkit is high-level input handling (input boxes, buttons, menus, drop-down lists etc). I also missed some sort of resizeable panes, although the grid layout probably provides a suitable alternative to that.
A couple of projects have already been built with
termui, including ctop.
- Rich set of data visualization widgets
- Flexible, resizable 12-column grid layout
This library almost seems like the opposite of
termui: No fancy widgets anywhere, but instead, building blocks for split views, overlapping views, editable views, and event handling for keyboard and mouse events.
- Text input views
- Resizable layouts via custom redraw handler
I couldn’t resist including the following projects here, although they do not meet some of the inclusion criteria. Still, they look very promising and may offer one or the other feature that you might have missed in this list so far.
From the screenshots, this is perhaps the best-looking TUI library in this list. It also looks fairly advanced already, featuring a rich set of UI widgets like buttons, drop-down lists, gauges, etc. as well as a window manage and theming. However, the lack of documentation (except for the API docs) indicates that this is still a work in progress.
- Many standard UI widgets
- Multi window capability, including resizing and overlapping
Definitely a work in progress,
wm manages overlapping, resizeable, decorated windows with scrollable content.
- Multiple, overlapping windows
The building blocks of
tui-go are widgets and layout boxes. There seems no support for mouse-based resizing of layout boxes, but each layout box can assume one of (currently) two automatic resizing behaviors.
- Easy layout through horizontally and vertically stacked layout boxes
- Layout automatically adjusts to changed terminal size
- Text input boxes
This one is a special kind of TUI.
termloop is not a general TUI library. Rather, it is a specialized toolkit for console-based games. So perhaps you won’t be able to build a “standard” application UI on top of it, but you surely can make your windows make a sound when colliding! 😉
- Support for console games
Code: TUI libs in practice
Let’s try out the two high-level libraries side by side! The code below uses the two libraries
gocui for creating a minimal UI with a simple layout - similar to the layout in the
tui-go screenshot above:
- A list box with a fixed width, positioned at the left side
- A text entry box with a fixed height, positioned at the bottom
- A general-purpose output pane in the remaining area
If the library provides a text entry widget, text entered there shall appear in the output pane.
First, the imports, consts, and vars that we need for the sample code.
Let’s start with
termui organizes its content as “blocks”. Blocks can be positioned using absolute coordinates, or they can be integrated into a 12-column grid layout. The grid layout allows to dynamically adjust the content’s width when the user resizes the terminal window.
Right now, there is no automatic height adjustment available, but an issue has been raised to request this feature.
The test code works around this restriction by manually re-calculating the heights of the list block and the output block before re-aligning and re-rendering the UI.
That wasn’t too difficult, was it? Text entry is missing here, as
termui has no input controls yet. Still, implementing an input box is possible with the available API methods; see the
_example subdirectory in
termui’s repository. It is just too much code for this blog article, so I leave this as an exercise to the reader.
Now let’s see how
gocui solves the same task.
Our main func just needs to read the name from the TUI lib from the command line and execute the respective code.
Conclusions from the code
Both libraries have their pros and cons. The
termui code seems more concise, but this might be due to the lack of an editing feature (that would have added some lines of code), and perhaps also due to the grid layout engine that made the resize handler a two-liner.
gocui layout is certainly more flexible but the tradeoff is increased code size. Text input only needs a few lines of code in
gocui, whereas data visualization is where
I leave the decision to you; maybe you even perfer to go low-level with
tcell, or you might decide to give the other TUI libs a try.
How to get and run the code
go get the code. Note the
-d flag that prevents auto-installing the binary into
go get -d github.com/appliedgo/tui
cd to the source code directory.
Step 3. Run the binary with either “termui” or “gocui” as a parameter.
go run tui.go termui go run tui.go gocui