Just like last week, let’s add some more information to our tmux status bar: this time, the current battery level.
I’ve got the inspiration from the “Showing your laptop battery status in tmux” blog post. The Cute Battery project on GitLab contains the scripts I currently use to display the battery level on my terminal. I’ve tested them on Linux, FreeBSD, and macOS (Darwin).
# ...
battery_status() {
case $(uname -s) in
"Linux")
linux_get_bat
;;
"FreeBSD")
freebsd_get_bat
;;
"Darwin")
darwin_get_bat
;;
esac
}
BATTERY_STATUS=$(battery_status "$1")
[ -z "$BATTERY_STATUS" ] && exit
echo "$BATTERY_STATUS"
The battery.sh script returns the current level of the battery in percentage (0-100). A negative value means that the battery is discharging, while a positive value means that the battery is charging.
The cutinate.sh script takes a numerical value, either from stdin or as argument, and displays a series of bullets. If the value passed to the script is positive, then a lightning bolt emoji is shown on the left.
Very simple to use:
$ battery.sh | cutinate.sh
⚡︎●●●○○○○○○○
You can also specify the maximum number of icons to display:
$ battery.sh | NUM_ICONS=30 cutinate.sh
⚡︎●●●●●●●●●●●●○○○○○○○○○○○○○○○○○○
The cutinate.sh script can be used with anything returning an integer between -100 and 100:
$ echo "-32" | cutinate.sh
●●●○○○○○○○
$ cutinate.sh 56
⚡︎●●●●●●○○○○
You can display this on tmux adding this line to your .tmux.conf file:
set -g status-right "| #(battery.sh | cutinate.sh) | %a %b %e, %H:%M "
Enjoy!