conky/battery_estimte: select icon based on battery percentage

This commit is contained in:
Colin 2023-12-28 01:11:51 +00:00
parent 3846322f12
commit 1df99978bb

View File

@ -11,8 +11,8 @@ usage() {
}
# these icons come from sxmo; they only render in nerdfonts
bat_dis="󱊢"
bat_chg="󱊥"
icon_bat_chg=("󰢟" "󱊤" "󱊥" "󰂅")
icon_bat_dis=("󰂎" "󱊡" "󱊢" "󱊣")
log() {
if [ "$BATTERY_ESTIMATE_DEBUG" = "1" ]; then
@ -21,14 +21,29 @@ log() {
fi
}
render_icon() {
# args:
# 1: "chg" or "dis"
# 2: current battery percentage
level=$(($2 / 25))
level=$(($level > 3 ? 3 : $level))
level=$(($level < 0 ? 0 : $level))
log "icon: %s %d" "$1" "$level"
if [ "$1" = "dis" ]; then
printf "%s" "${icon_bat_dis[$level]}"
elif [ "$1" = "chg" ]; then
printf "%s" "${icon_bat_chg[$level]}"
fi
}
try_path() {
# assigns output variables:
# - perc, perc_left (0-100)
# - perc, perc_from_full (0-100)
# - full, rate (pos means charging)
if [ -f "$1/capacity" ]; then
log "perc, perc_left from %s" "$1/capacity"
log "perc, perc_from_full from %s" "$1/capacity"
perc=$(cat "$1/capacity")
perc_left=$((100 - $perc))
perc_from_full=$((100 - $perc))
fi
if [ -f "$1/charge_full_design" ] && [ -f "$1/current_now" ]; then
@ -53,12 +68,17 @@ try_path() {
try_all_paths() {
try_path "/sys/class/power_supply/axp20x-battery" # Pinephone
try_path "/sys/class/power_supply/BAT0" # Thinkpad
log "perc: %d, left: %d" "$perc" "$perc_left"
log "perc: %d, perc_from_full: %d" "$perc" "$perc_from_full"
log "full: %f, rate: %f" "$full" "$rate"
log " rate > 0 means charging, else discharging"
}
fmt_minutes() {
# args:
# 1: icon to render
# 2: string to show if charge/discharge time is indefinite
# 3: minutes to stable state (i.e. to full charge or full discharge)
# - we work in minutes instead of hours for precision: bash math is integer-only
log "charge/discharge time: %f min" "$3"
# args: <battery symbol> <text if ludicrous estimate> <estimated minutes to full/empty>
if [[ $3 -gt 1440 ]]; then
@ -75,10 +95,12 @@ fmt_minutes() {
pretty_output() {
if [[ $rate -lt 0 ]]; then
log "discharging"
fmt_minutes "$bat_dis" '∞' "$(($full * 60 * $perc / (-100 * $rate)))"
icon="$(render_icon dis $perc)"
fmt_minutes "$icon" '∞' "$(($full * 60 * $perc / (-100 * $rate)))"
elif [[ $rate -gt 0 ]]; then
log "charging"
fmt_minutes "$bat_chg" '100%' "$(($full * 60 * $perc_left / (100 * $rate)))"
icon="$(render_icon chg $perc)"
fmt_minutes "$icon" '100%' "$(($full * 60 * $perc_from_full / (100 * $rate)))"
elif [[ "$perc" != "" ]]; then
log "neither charging nor discharging"
echo "$bat_dis $perc%"