ark/users/modules/eww/scripts/battery

52 lines
1.0 KiB
Plaintext
Raw Normal View History

2022-09-14 15:35:52 +03:00
#!/usr/bin/env bash
status() {
if [ "$STATE" = "Charging" ]; then
echo -n "charging"
if [ "$RATE" -gt 0 ]; then
echo ", $(gettime) left"
else
echo ""
fi
elif [ "$STATE" = "Discharging" ]; then
echo "$(gettime)h left"
else
echo "fully charged"
fi
}
color() {
if [ "$CAPACITY" -le 20 ]; then
echo '#f38ba8'
else
echo '#a6e3a1'
fi
}
wattage() {
echo "$(bc -l <<< "scale=1; $RATE / 1000000") W"
}
gettime() {
FULL=$(cat /sys/class/power_supply/BAT0/energy_full)
NOW=$(cat /sys/class/power_supply/BAT0/energy_now)
if [ "$RATE" -gt 0 ]; then
if [ "$STATE" = "Discharging" ]; then
EX="$NOW / $RATE"
else
EX="($FULL - $NOW) / $RATE"
fi
date -u -d@$(bc -l <<< "$EX * 3600") +%H:%M
fi
}
while true; do
RATE=$(cat /sys/class/power_supply/BAT0/power_now)
CAPACITY=$(cat /sys/class/power_supply/BAT0/capacity)
STATE=$(cat /sys/class/power_supply/BAT0/status)
echo '{ "percentage": '"$CAPACITY"', "wattage": "'"$(wattage)"'", "status": "'"$(status)"'", "color": "'"$(color)"'" }'
2022-09-14 17:58:08 +03:00
sleep 60
2022-09-14 15:35:52 +03:00
done