$_ BSDHowTo.ch
How To... Why not..? Scripts Patches RSS logo

How to create a simple status bar

Last update: 2018-08-26

Screenshot lemonbar

Introduction

I create a simple status bar that shows useful information like display brightness, network status and battery status. A shell script uses tools from the OpenBSD base to collect the information and write it to stdout. The tool lemonbar from ports will read the information from stdin and display it formatted in a customizable bar.

Install software

Install lemonbar as usual:

$ doas pkg_add -i lemonbar

If you want to display the current network speed you have to install ifstat too:

$ doas pkg_add -i ifstat

Structure of the script

The script implements a function for each piece of information that is displayed by lemonbar. This makes it easy to add or remove functions to/from the script. And it eases the parsing of the output from external tools that get the values. Each function prints its value as a string without newline. The main loop of the script collects the outputs into a single string that is printed to stdout.

Colors in the output

The lemonbar supports the formatting of the text by embedding codes into the string it reads from stdin. My lemonbar.sh uses the colors as indicators for different states. When the power adapter is connected the word AC: is green, else it is red. The same is true for the word Vol: where red indicates that the sound is muted while green indicates the opposite.

Running lemonbar using the script

Most of the time you want to start lemonbar when you log into X(7) . Add something like the following lines to ~/.xsession:

lbfont=-xos4-terminus-*-*-*-*-14-*-*-*-*-*-iso10646-1
~/bin/lemonbar.sh | lemonbar -g 2560x15 -b -n "lemonbar" -f $lbfont &

Use xfontsel(1) to generate a valid value for $lbfont on your system. The parameter -g defines the size of lemonbar as width x height in pixel.

The complete script

Beside ifstat mentioned above the script uses tools from OpenBSD base. In the function Wlan() you should adapt the name of the WLAN interface to your adapter. You can download the complete script.