2023-07-13 00:40:52 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#!nix-shell -i bash
|
|
|
|
|
|
|
|
full=$(cat /sys/class/power_supply/axp20x-battery/charge_full_design)
|
|
|
|
rate=$(cat /sys/class/power_supply/axp20x-battery/current_now)
|
|
|
|
perc=$(cat /sys/class/power_supply/axp20x-battery/capacity)
|
|
|
|
perc_left=$((100 - $perc))
|
|
|
|
# these icons come from sxmo; they only render in nerdfonts
|
|
|
|
bat_dis=""
|
|
|
|
bat_chg=""
|
|
|
|
|
|
|
|
fmt_minutes() {
|
2023-07-21 22:51:24 +00:00
|
|
|
# args: <battery symbol> <text if ludicrous estimate> <estimated minutes to full/empty>
|
|
|
|
if [[ $3 -gt 1440 ]]; then
|
|
|
|
printf "%s %s" "$1" "$2" # more than 1d
|
2023-07-13 00:40:52 +00:00
|
|
|
else
|
2023-07-21 22:51:24 +00:00
|
|
|
hr=$(($3 / 60))
|
2023-07-13 00:40:52 +00:00
|
|
|
hr_in_min=$(($hr * 60))
|
2023-07-21 22:51:24 +00:00
|
|
|
min=$(($3 - $hr_in_min))
|
2023-07-13 02:02:25 +00:00
|
|
|
printf "%s %dh%02dm" "$1" "$hr" "$min"
|
2023-07-13 00:40:52 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ $rate -lt 0 ]]; then
|
|
|
|
# discharging
|
2023-07-21 22:51:24 +00:00
|
|
|
fmt_minutes "$bat_dis" '∞' "$(($full * 60 * $perc / (-100 * $rate)))"
|
2023-07-13 00:40:52 +00:00
|
|
|
elif [[ $rate -gt 0 ]]; then
|
|
|
|
# charging
|
2023-07-21 22:51:24 +00:00
|
|
|
fmt_minutes "$bat_chg" '100%' "$(($full * 60 * $perc_left / (100 * $rate)))"
|
2023-07-13 00:40:52 +00:00
|
|
|
else
|
2023-07-21 22:51:24 +00:00
|
|
|
echo "$bat_dis $perc%"
|
2023-07-13 00:40:52 +00:00
|
|
|
fi
|