diff options
| Commit | dce4fccf08255afaaa218e2e21e53e45e7e1c582 (patch) | |
|---|---|---|
| Author | 2025-03-28 19:44:31 +0200 | |
| Committer | 2025-03-28 19:44:31 +0200 | |
| Files | 8514b9290d3c44b728b287f574b64116a111b1ca /.scripts/battery-status | |
| .files-dce4fccf08255afaaa218e2e21e53e45e7e1c582.tar .files-dce4fccf08255afaaa218e2e21e53e45e7e1c582.tar.gz .files-dce4fccf08255afaaa218e2e21e53e45e7e1c582.zip | ||
| Parent | 6b71263670e0924294610b537f0ac17292ce2f19 (diff) | |
feat: Add scripts
1 files changed, 65 insertions, 0 deletions
| -rwxr-xr-x | .scripts/battery-status | 65 |
diff --git a/.scripts/battery-status b/.scripts/battery-status new file mode 100755 index 0000000..6e51bcb --- /dev/null +++ b/.scripts/battery-status @@ -0,0 +1,65 @@ +#!/bin/bash +exit + +# Check for dependencies +for comm in upower awk; do + command -v upower >/dev/null 2>&1 || (echo "Error, missing command dependencies: $comm" && exit) +done + +# Prepare battery informatoin +for bat in BAT0 BAT1; do + upower -i "/org/freedesktop/UPower/devices/battery_$bat" > "/tmp/$bat" + [ "$(awk '/native-path/{print $2}' "/tmp/$bat")" = '(null)' ] && continue + + bat_state="$(awk '/state/{print substr($2, 1, 1)}' "/tmp/$bat")" + if [ $bat_state = 'c' ]; then bat_power_ind='' + elif [ $bat_state = 'd' ]; then bat_power_ind='' + else bat_power_ind='' + fi + + bat_per="$(awk '/percentage/{print strtonum($2)}' "/tmp/$bat")" + # bat_per=$((bat_per * 5 / 4 - 25)) # compensation for failing battery + + [ $bat_state = 'd' ] \ + && bat_time="$(awk '/time to empty/{print $4 substr($5, 1, 1)}' "/tmp/$bat")" \ + || bat_time="$(awk '/time to full/ {print $4 substr($5, 1, 1)}' "/tmp/$bat")" + + echo -n " $bat_power_ind ${bat_per}% $bat_time" + + # + # Notifications + # + + command -v notify-send >/dev/null 2>&1 || exit + [ ! -e "/tmp/bat_notif$bat" ] && touch "/tmp/bat_notif$bat" + bat_notif="$(cat "/tmp/bat_notif$bat")" + + + # Notify when battery is charged to 80% + if [ $bat_state = 'c' ]; then + if [ "$bat_notif" != 80 ] && [ $bat_per -ge 80 ]; then + notify-send -u normal -i battery-charging -t 999999 "$bat at 80%" + echo 80 > "/tmp/bat_notif$bat" + fi + + # Notify when battery discharges to 5%, 10%, 15% and 20% + elif [ $bat_state = 'd' ]; then + for per in 5 10 15 20; do + if [ "$bat_notif" = $per ]; then + break + elif [ $bat_per -le $per ]; then + notify-send -u critical -i battery-low -t 999999 "$bat at ${per}%" + echo $per > "/tmp/bat_notif$bat" + break + fi + done + + # Notify when battery is full + else + if [ "$bat_notif" != 100 ]; then + notify-send -u normal -i battery-full -t 999999 "$bat fully charged!" + echo 100 > "/tmp/bat_notif$bat" + fi + fi +done +echo |
