Published: July 16, 2020
Note from the future (Dec 2023)
Looking back, I had way too much time in highschool : )
In order to practice Rust development, and to rice my desktop to infinity, I decided to write some custom polybar modules in Rust.
Polybar is one of the most popular(and expandable) status bars for unix desktops. It is often used with a window manager, because desktop environments tend to have their own status bar. See: difference between WM and DE.
Because it’s so popular you’d think that there are plenty of modules(a module is basically a widget, for example the clock or the current playing song) freely available. Well, you’d be wrong. I managed to find surprisingly few modules on the internet, and I think that it is because making one is super simple.
Here is my current bar(you have to click on it and ZOOM if you want to see anything, having a 21:9 monitor is hard ok):
Short answer: any program that outputs text to stdout. Or, in simpler terms: any program that would normally output text to the terminal.
Polybar simply takes that text and formats it nicely, then it displays it in your bar. Because of this, you could write a polybar module that is very specific to your configuration. For instance, I have a module which tells me when my kitchen door is open(since I always leave it open). I achieved this with a WIFI enabled MCU.
With a simple bash script I managed to get this string to pop up in my bar whenever the door is open for more than 1 minute:
NOTE:
The white lines are an artifact of my screenshotting software, scrot I don’t actually see them in the bar.
When I close the door it simply goes away.
Another cool one that I wrote uses a python script to display the current value of the BAT token, a relatively new cryptocurrency, so that when it will crash, and I’ll lose all my money, I can instantly cry. Here is what this one looks like:
Although this scripts are extremely useful, a skilled programmer could spot a major flaw in them in a split second: they aren’t written in pure, sacred Rust.
Now, here is some eye candy to recover from those cursed, interpreted, slow scripts.
I know that I promised you only perfect, memory safe, rust code, but you might want to take a look at this other modules too.
[module/uptime]
type = custom/script
exec = awk '{printf("%dd : %02dh : %02dm\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}' /proc/uptime
format-underline = ${colors.alert}
interval = 60
This will update the time every minute, and print it in a fancy format
[module/kernel]
type = custom/script
exec = "uname -r"
format-underline = #c47d83
interval = 18000
[module/myMemory]
type = custom/script
exec = "vmstat -s | sed -n '2p' | sed 's/[[:space:]]//g' | sed 's/K*//' | sed -E 's/(Kusedmemory)+$//' | cut -c 1-4"
format-underline = #4bffdc
interval = 2