ark/users/modules/eww/scripts/battery
2022-10-22 23:54:44 +03:00

67 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
icons=("󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹")
icons_charging=("󰂆" "󰂇" "󰂈" "󰂉" "󰂅")
# declare -A baticon=([10]="󰁺" [20]="󰁻" [30]="󰁼" [40]="󰁽" [50]="󰁾" [60]="󰁿" [70]="󰂀" [80]="󰂁" [90]="󰂂" [100]="󰁹")
geticon() {
if [ "$STATE" = "Charging" ]; then
level=$(awk -v n="$CAPACITY" 'BEGIN{print int((n-1)/20)}')
echo "${icons_charging[$level]}"
else
level=$(awk -v n="$CAPACITY" 'BEGIN{print int((n-1)/10)}')
echo "${icons[$level]}"
fi
}
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 '{ "icon": "'"$(geticon)"'", "percentage": '"$CAPACITY"', "wattage": "'"$(wattage)"'", "status": "'"$(status)"'", "color": "'"$(color)"'" }'
sleep 3
done