A simple battery status indicator for Tint2

I was having this hilarious love/hate relationship with the battery status indicator lately and finally decided to take matters into my own hands. Below is the Shell Script code for a simple battery status indicator:

#!/bin/bash

# Personalize icons/glyphs.
# You can copy/paste those from, say, https://www.nerdfonts.com/cheat-sheet
# and search for: nf-md-battery*
NoBatteryIcon="󱉝"
DischargingIcons="󰂎󱊡󱊢󱊣󰁹"
ChargingIcons="󰢟󱊤󱊥󱊦󰂅"

if BatteryInfo="$(upower -i $(upower -e | grep 'BAT'))"; then
   read -r State Percentage <<< "$(awk -F: '/state|percentage/ {gsub(" ","",$2); printf $2" "}' <<< "$BatteryInfo")"
   case ${Percentage%?} in
      9[0-9]|100   ) Index=4 ;; # 90-100
      [7-8]*       ) Index=3 ;; # 70-89%  
      [4-6]*       ) Index=2 ;; # 40-69%
      1[6-9]|[23]* ) Index=1 ;; # 16-39%
      *            ) Index=0 ;; # <=15
   esac
   [[ "$State" = 'discharging' ]] && IconSet="$DischargingIcons" || IconSet="$ChargingIcons"
   Icon=${IconSet:"$Index":1}
else
   Icon="$NoBatteryIcon"
fi
echo "${Icon} $Percentage"

All you have to do is save it in your favorite scripts folder and give it execution permission. Then, add another Executor in your Tint2 bar and set the script in the Command dialog.

Enjoy. :grin:

2 Likes

That’s a really nice little script. I particularly like your use of icons.
Thank you. :grinning: