67 lines
1.9 KiB
Bash
Executable File
67 lines
1.9 KiB
Bash
Executable File
#!/bin/bash
|
|
# Battery + power profile for waybar (JSON).
|
|
# Usage: bat-pp.sh refresh — print battery state as JSON
|
|
# bat-pp.sh toggle — cycle CPU governor (powersave <-> performance)
|
|
|
|
BAT=$(ls -d /sys/class/power_supply/BAT* 2>/dev/null | head -1)
|
|
|
|
get_governor() {
|
|
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null || echo "n/a"
|
|
}
|
|
|
|
case "$1" in
|
|
refresh)
|
|
if [ -z "$BAT" ]; then
|
|
# Desktop — no battery, show AC + governor
|
|
GOV=$(get_governor)
|
|
echo "{\"text\": \" AC\", \"alt\": \"No battery (desktop)\nGovernor: $GOV\", \"class\": \"\", \"percentage\": 100}"
|
|
exit 0
|
|
fi
|
|
|
|
CAP=$(cat "$BAT/capacity")
|
|
STATUS=$(cat "$BAT/status")
|
|
GOV=$(get_governor)
|
|
|
|
CLASS=""
|
|
ICON=""
|
|
case "$STATUS" in
|
|
Charging)
|
|
CLASS="charging"
|
|
ICON=""
|
|
;;
|
|
Full)
|
|
CLASS="full"
|
|
ICON=""
|
|
;;
|
|
*)
|
|
if [ "$CAP" -le 10 ]; then CLASS="critical"; ICON=""
|
|
elif [ "$CAP" -le 25 ]; then CLASS="warning"; ICON=""
|
|
elif [ "$CAP" -le 50 ]; then ICON=""
|
|
elif [ "$CAP" -le 75 ]; then ICON=""
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
echo "{\"text\": \"$ICON $CAP\", \"alt\": \"Battery: $CAP%\nStatus: $STATUS\nGovernor: $GOV\", \"class\": \"$CLASS\", \"percentage\": $CAP}"
|
|
;;
|
|
|
|
toggle)
|
|
# Cycle CPU governor: powersave <-> performance
|
|
GOV=$(get_governor)
|
|
if [ "$GOV" = "powersave" ]; then
|
|
NEW="performance"
|
|
else
|
|
NEW="powersave"
|
|
fi
|
|
# Needs root — add to /etc/doas.conf:
|
|
# permit nopass :wheel cmd /usr/bin/cpupower
|
|
if command -v cpupower > /dev/null 2>&1; then
|
|
doas cpupower frequency-set -g "$NEW" > /dev/null 2>&1
|
|
else
|
|
for CPU in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
|
|
echo "$NEW" | doas tee "$CPU" > /dev/null 2>&1
|
|
done
|
|
fi
|
|
;;
|
|
esac
|