round() { awk -v n=$1 -v d=$2 'BEGIN{print int((n+d/2)/d) * d}'; } # e.g. 7h est_time=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/time to empty:/ {print $4substr($5, 1, 1)}') # charging / discharging / pending-charge / ??? chg_state=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/state:/ {print $2}') # e.g. 69.420% percentage_raw=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/percentage:/ {print $2}') # rounded to integer percentage_int=$(printf "%.0f%" "${percentage_raw%%%}") # when battery is low, highlight the widget if [[ $percentage_int -le "10" ]]; then printf "#[bg=red,fg=brightwhite]"; elif [[ $percentage_int -le "15" ]]; then printf "#[bg=brightyellow]"; fi printf " "; # left padding # battery icon if [[ $chg_state == "charging" ]]; then printf "󰂄" else case "$(round "$percentage_int" 10)" in "10") printf "󰁺";; "20") printf "󰁻";; "30") printf "󰁼";; "40") printf "󰁽";; "50") printf "󰁾";; "60") printf "󰁿";; "70") printf "󰂀";; "80") printf "󰂁";; "90") printf "󰂂";; "100") printf "󰁹";; esac fi printf " "; #bat-percent gap printf "%s%%" "$percentage_int"; # this is only shown when on battery if [[ $est_time ]]; then printf " "; #percent-estimage gap printf " $est_time" fi printf " "; # right padding printf "#[default]"; # reset style