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.
Without the tips of a couple of redditors in this
wm would perhaps have slipped through.
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, resizeable 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
- Resizeable 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
A newcomer in 2018. In order avoid having to always write UI controls from scratch, the author decided to create a package choke-full of high-level interactive controls like:
- Input forms
- Text views (multi-colored and navigable)
- Tables and lists (navigable and selectable)
- Layout managers (flex and pages)
- and more.
tview is based on
- All-in-one solution, including a comprehensive set of controls (from simple input field to complex table control), support for automatic layout, and an application wrapper.
- Powerful controls
- Intuitive API with optional support for dot-chaining of method calls (if you really want to)
- Detailed documentation with sample code, via godoc.org API doc and GitHub Wiki.
As always, there is some code included to try out at your end. This time, the code uses the two libraries
gocui for creating a minimal UI with a simple layout – similar to the layout in the
- 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.