Author: Adam Evyčędo <git@apiote.xyz>
change bar_battery to upower
.local/bin/bar_battery | 96 ++++++++++++++++++++++++++++++++++++++-----
diff --git a/.local/bin/bar_battery b/.local/bin/bar_battery index c5c46e103b1f2b84e067589086e0e77ec433be62..730c6cdee7497a69303e28ed749cea288965d88c 100755 --- a/.local/bin/bar_battery +++ b/.local/bin/bar_battery @@ -5,22 +5,29 @@ yellow="#db9d3b" red="#892b30" green="#1c9867" -render() { +render_acpi() { acpi=$(acpi | tail -n1) state=$(echo "$acpi" | cut -d ',' -f 1 | cut -d ':' -f 2 | tr -d ' ') load=$(echo "$acpi" | cut -d ',' -f 2 | tr -d ' %') - full=$(( ((load * 12) + 99) / 100 )) - empty=$(( 12 - full )) + energy=$(( ((load * 144) + 99) / 100 )) + render "$(echo "$state" | tr '[:upper:]' '[:lower:]')" $energy +} - if [ "$state" = "Charging" ] +render() { + state="$1" + energy="$2" + full=$(( (energy + 11) / 12 )) + empty=$((12 - full)) + + if [ "$state" = "charging" ] then colour=$yellow - elif [ "$state" = "Full" ] + elif [ "$state" = "full" ] || [ "$state" = 'fully-charged' ] then colour=$green - elif [ $full -le 1 ] + elif [ "$full" -le 1 ] then colour=$red else @@ -33,7 +40,7 @@ do printf " " i=$(( i + 1 )) done - i=0; while [ $i -lt $full ] + i=0; while [ $i -lt "$full" ] do if [ $(( i % 2 )) -eq 0 ] then @@ -48,15 +55,78 @@ printf "" } -while true -do +if [ "$1" = '-1' ] +then if [ -e /tmp/swaybar/pipe ] then - printf "battery|%s\n" "$(render)" >/tmp/swaybar/pipe + printf "battery|%s\n" "$(render_acpi)" >/tmp/swaybar/pipe fi - if [ "$1" = '-1' ] + exit +fi + +battery='' +energy=-1 +energyF=-1 +energyE=-1 +pc=0 +pc_prev=0 +state="" +state_prev="" +last_notify="" +upower --monitor-detail | while read -r line +do + if echo "$line" | grep -qF '[' then - exit + if echo "$line" | grep -q 'device changed' + then + battery=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 3) + else + battery='' + fi fi - sleep 300 + + if [ "$battery" = '/org/freedesktop/UPower/devices/battery_BAT1' ] + then + case $line in + *state:*) + state_prev=$state + state=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2) + if [ "$state_prev" != "$state" ] + then + case $state in + charging) notify-send -i battery-caution-charging-symbolic Battery charging ;; + discharging) notify-send -i battery-good-symbolic Battery discharging ;; + fully-charged) notify-send -i battery-full-charged-symbolic Battery full ;; + esac + last_notif="$state" + fi + ;; + *energy:*) energy=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2);; + *energy-empty:*) energyE=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2);; + *energy-full:*) energyF=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2);; + esac + + if [ "$energy" != '-1' ] && [ "$energyE" != '-1' ] && [ "$energyF" != '-1' ] + then + pc_prev=$pc + pc=$(echo "$energy / ($energyF - $energyE) * 144" | bc -l | jq '.|ceil') + fi + + if [ "$pc_prev" != "$pc" ] + then + if [ "$pc" -le 6 ] && [ "$state" = 'discharging' ] && [ "$last_notify" != '6'] + then + notify-send -u critical -i battery-empty-symbolic Battery 'less than <sub>2</sub>6' + last_notify='6' + elif [ "$pc" -le 12 ] && [ "$state" = 'discharging' ] && [ "$last_notify" != '12'] + then + notify-send -i battery-low-symbolic Battery 'less than <sub>2</sub>10' + last_notify='12' + fi + if [ -e /tmp/swaybar/pipe ] + then + printf "battery|%s\n" "$(render "$state" "$pc")" >/tmp/swaybar/pipe + fi + fi + fi done