A Go terminal UI for displaying network adapters in real time
Practical Box
The layout algorithm in termui
arranges the widgets passed to it in a 12x12 grid. The AddRows()
function in line 33 accepts layout rows created with NewRow()
as arguments; their arguments in turn are columns created with NewCol()
. The latter function takes the width of the column as the first argument; in Listing 2, this means all 12 of a total of 12 grid squares. The second parameter is an offset as a spacer, which remains unused here at
.
The Align()
function called in line 40 builds the widget grid internally; the following Render()
puts the whole thing on the screen. Now the only thing left is to intercept any events that occur – for example, if the user increases or decreases the size of the terminal window. In this case, the raster engine kicks in thanks to the /sys/wnd/resize
handler in line 45. The code first uses TermWidth()
to determine the new terminal width and then calls the Align()
function to distribute the widgets across the available space once again. The following call to Render()
refreshes the display in a smooth, single action.
Now the list box with the displayed network interfaces is originally empty, because Items
– a "field" (attribute) type in Go, as part of a struct
(mixed data structure) – wasn't initialized in the beginning. Line 54 now takes care of this with the handler called once per second for the /timer/1s
event. It calls the AsStrings()
function from the ifconfig
package previously shown in Listing 1 and assigns to the list box a pre-formatted slice of strings with the network adapters and their IPs. For the refreshed list to appear on the screen, the graphic manager's Render()
function must be called again.
All's Well That Ends Well
To allow the user to exit the program gracefully without the terminal getting stuck in graphics mode and becoming unusable, the handlers /sys/kbd/C-c
and /sys/kbd/q
in lines 59 and 62 intercept the Ctrl+C and Q keystrokes and use StopLoop()
to stop the event loop, which in turn lets the blocking Loop()
function in line 66 return before main()
finishes.
But wait, before that happens, the defer t.Close()
command, which was called previously in line 18, quickly calls the graphic manager's cleanup function, which kindly cleans up everything to leave the terminal as it was when the program started.
You can compile the program by typing:
$ go build iftop.go
If the ifconfig
package from Listing 1 is correctly installed, all code will be wrapped up in a binary that clocks in at about 3MB. It contains everything, so you can just copy it to another machine with a similar operating system and launch it there if so desired.
When you finally launch the program by typing ./iftop
at the command line, you will see a tidy screen with the elements shown in Figure 2: the initially empty list box at the top, and the text field at the bottom. After one second, when the timer expires for the first time, a list of the network interfaces with their IP addresses appears in the list box.
If you want to test the dynamic behavior, you can now plug a network cable into your laptop (e.g., via USB), connect it to your router, and see a new entry appear in the list box almost immediately. Conversely, an existing entry should disappear as soon as a network interface goes away (e.g., when the laptop's wireless signal is turned off).
The termui
project offers a generous helping of eye candy in the terminal, from progress bars to bar and pie charts (Figure 3), and termui
is not the only terminal UI framework in Go: gocui
, clui
, wm
, tui-go
, and others also vie for the developer's favor. A blog post online [4] provides an overview of the advantages and disadvantages of the individual releases. It would be quite possible to build a sophisticated terminal application like lazygit
[5] with one of these frameworks or a kind of Norton Commander (also known as Midnight Commander on Linux) for retro-looking file manipulation – state-of-the-art again 35 years after MS-DOS!
Infos
- "Using the Electron Framework to Weed Out Images" by Mike Schilli, Linux Pro Magazine, issue 216, November 218, p. 46.
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/218/
- termui on GitHub: https://github.com/gizak/termui
- "Text-Based User Interfaces" by Christoph Berger: https://appliedgo.net/tui/
- lazygit: https://github.com/jesseduffield/lazygit
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.