conky/battery_estimte: select icon based on battery percentage
This commit is contained in:
@@ -11,8 +11,8 @@ usage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# these icons come from sxmo; they only render in nerdfonts
|
# these icons come from sxmo; they only render in nerdfonts
|
||||||
bat_dis=""
|
icon_bat_chg=("" "" "" "")
|
||||||
bat_chg=""
|
icon_bat_dis=("" "" "" "")
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
if [ "$BATTERY_ESTIMATE_DEBUG" = "1" ]; then
|
if [ "$BATTERY_ESTIMATE_DEBUG" = "1" ]; then
|
||||||
@@ -21,14 +21,29 @@ log() {
|
|||||||
fi
|
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() {
|
try_path() {
|
||||||
# assigns output variables:
|
# assigns output variables:
|
||||||
# - perc, perc_left (0-100)
|
# - perc, perc_from_full (0-100)
|
||||||
# - full, rate (pos means charging)
|
# - full, rate (pos means charging)
|
||||||
if [ -f "$1/capacity" ]; then
|
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=$(cat "$1/capacity")
|
||||||
perc_left=$((100 - $perc))
|
perc_from_full=$((100 - $perc))
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$1/charge_full_design" ] && [ -f "$1/current_now" ]; then
|
if [ -f "$1/charge_full_design" ] && [ -f "$1/current_now" ]; then
|
||||||
@@ -53,12 +68,17 @@ try_path() {
|
|||||||
try_all_paths() {
|
try_all_paths() {
|
||||||
try_path "/sys/class/power_supply/axp20x-battery" # Pinephone
|
try_path "/sys/class/power_supply/axp20x-battery" # Pinephone
|
||||||
try_path "/sys/class/power_supply/BAT0" # Thinkpad
|
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 "full: %f, rate: %f" "$full" "$rate"
|
||||||
log " rate > 0 means charging, else discharging"
|
log " rate > 0 means charging, else discharging"
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt_minutes() {
|
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"
|
log "charge/discharge time: %f min" "$3"
|
||||||
# args: <battery symbol> <text if ludicrous estimate> <estimated minutes to full/empty>
|
# args: <battery symbol> <text if ludicrous estimate> <estimated minutes to full/empty>
|
||||||
if [[ $3 -gt 1440 ]]; then
|
if [[ $3 -gt 1440 ]]; then
|
||||||
@@ -75,10 +95,12 @@ fmt_minutes() {
|
|||||||
pretty_output() {
|
pretty_output() {
|
||||||
if [[ $rate -lt 0 ]]; then
|
if [[ $rate -lt 0 ]]; then
|
||||||
log "discharging"
|
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
|
elif [[ $rate -gt 0 ]]; then
|
||||||
log "charging"
|
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
|
elif [[ "$perc" != "" ]]; then
|
||||||
log "neither charging nor discharging"
|
log "neither charging nor discharging"
|
||||||
echo "$bat_dis $perc%"
|
echo "$bat_dis $perc%"
|
||||||
|
Reference in New Issue
Block a user