programming (and other) musings
08 Aug 2020

xmobar: a battery trick

i've been maintaining xmobar for more than a decade now, and i still use it daily and tweak it almost as often. With more than a hundred contributors besides myself, and many bugs to solve, i am always learning new things. The latest one, that font awesome thing everyone seems so fond of.

i just decided to add a bit more icons to my configuration, and, in particular, to have a battery monitor that displays graphically an approximation of time left.

For that, you specify the new font as one of the additional fonts:

additionalFonts = ["xft:FontAwesome-9"]

Then, find a list of glyphs that represent a charging battery (the ones between 0xf240 and 0xf244 work well), and assign that to the -f flag (the foreground of horizontal bars) with -W (the display width) set to 0, and use it in your battery monitor template, which should include somewhere <leftbar>; for instance:

BatteryN ["BAT0"]
         ["-t", "<acstatus>"
         , "-S", "Off", "-d", "0", "-m", "3"
         , "-L", "10", "-H", "90", "-p", "3"
         , "-W", "0"
         , "-f", "\xf244\xf243\xf243\xf243\xf242\xf242\xf242\xf241\xf241\xf240"
         , "--"
         , "-P"
         , "-a", "notify-send -u critical 'Battery running out!!!!!!'"
         , "-A", "5"
         , "-i", "<fn=1>\xf1e6</fn>"
         , "-O", "<fn=1><leftbar>  \xf1e6</fn> <timeleft>"
         , "-o", "<fn=1><leftbar></fn1> <timeleft>"
         , "-H", "10", "-L", "7"
         ] 50 "batt0"

where 0xf1e6 displays a little plug. here's a peek at the corner of my desktop with a charging battery:

xmobar-battery.png

The only thing we needed to enable this little feature was to implement indexing on the foreground bar string when the special value 0 is passed, a really small change codewise. Moroever, the feature is generic and applies to any horizontal bar in any monitor, so one can play similar tricks in many places.

This feature will be available in the forthcoming xmobar 0.36 release, but you can of course just grab it from its master branch.

Tags: programming
Creative Commons License
jao.io by jao is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.