Add support for termux-battery-status

This commit is contained in:
Freed-Wu 2021-03-07 03:22:46 +08:00
parent 3920940ea8
commit 2194d96789
1 changed files with 60 additions and 40 deletions

View File

@ -1268,6 +1268,9 @@ _p9k_prompt_battery_init() {
_p9k__async_segments_compute+='_p9k_worker_invoke battery _p9k_prompt_battery_compute' _p9k__async_segments_compute+='_p9k_worker_invoke battery _p9k_prompt_battery_compute'
return return
fi fi
if [[ -x `which termux-battery-status` ]] && [[ -x `which jq` ]]; then
return
fi
if [[ $_p9k_os != (Linux|Android) || if [[ $_p9k_os != (Linux|Android) ||
-z /sys/class/power_supply/(CMB*|BAT*|battery)/(energy_full|charge_full|charge_counter)(#qN) ]]; then -z /sys/class/power_supply/(CMB*|BAT*|battery)/(energy_full|charge_full|charge_counter)(#qN) ]]; then
typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}' typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}'
@ -1328,52 +1331,69 @@ _p9k_prompt_battery_set_args() {
Linux|Android) Linux|Android)
# See https://sourceforge.net/projects/acpiclient. # See https://sourceforge.net/projects/acpiclient.
local -a bats=( /sys/class/power_supply/(CMB*|BAT*|battery)/(FN) ) local -a bats=( /sys/class/power_supply/(CMB*|BAT*|battery)/(FN) )
(( $#bats )) || return if (( $#bats )); then
local -i energy_now energy_full power_now
local -i is_full=1 is_calculating is_charching
local dir
for dir in $bats; do
local -i pow=0 full=0
if _p9k_read_file $dir/(energy_full|charge_full|charge_counter)(N); then
(( energy_full += ${full::=_p9k__ret} ))
fi
if _p9k_read_file $dir/(power|current)_now(N) && (( $#_p9k__ret < 9 )); then
(( power_now += ${pow::=$_p9k__ret} ))
fi
if _p9k_read_file $dir/(energy|charge)_now(N); then
(( energy_now += _p9k__ret ))
elif _p9k_read_file $dir/capacity(N); then
(( energy_now += _p9k__ret * full / 100. + 0.5 ))
fi
_p9k_read_file $dir/status(N) && local bat_status=$_p9k__ret || continue
[[ $bat_status != Full ]] && is_full=0
[[ $bat_status == Charging ]] && is_charching=1
[[ $bat_status == (Charging|Discharging) && $pow == 0 ]] && is_calculating=1
done
local -i energy_now energy_full power_now (( energy_full )) || return
local -i is_full=1 is_calculating is_charching
local dir
for dir in $bats; do
local -i pow=0 full=0
if _p9k_read_file $dir/(energy_full|charge_full|charge_counter)(N); then
(( energy_full += ${full::=_p9k__ret} ))
fi
if _p9k_read_file $dir/(power|current)_now(N) && (( $#_p9k__ret < 9 )); then
(( power_now += ${pow::=$_p9k__ret} ))
fi
if _p9k_read_file $dir/(energy|charge)_now(N); then
(( energy_now += _p9k__ret ))
elif _p9k_read_file $dir/capacity(N); then
(( energy_now += _p9k__ret * full / 100. + 0.5 ))
fi
_p9k_read_file $dir/status(N) && local bat_status=$_p9k__ret || continue
[[ $bat_status != Full ]] && is_full=0
[[ $bat_status == Charging ]] && is_charching=1
[[ $bat_status == (Charging|Discharging) && $pow == 0 ]] && is_calculating=1
done
(( energy_full )) || return bat_percent=$(( 100. * energy_now / energy_full + 0.5 ))
(( bat_percent > 100 )) && bat_percent=100
bat_percent=$(( 100. * energy_now / energy_full + 0.5 )) if (( is_full || (bat_percent == 100 && is_charching) )); then
(( bat_percent > 100 )) && bat_percent=100 state=CHARGED
if (( is_full || (bat_percent == 100 && is_charching) )); then
state=CHARGED
else
if (( is_charching )); then
state=CHARGING
elif (( bat_percent < _POWERLEVEL9K_BATTERY_LOW_THRESHOLD )); then
state=LOW
else else
state=DISCONNECTED if (( is_charching )); then
state=CHARGING
elif (( bat_percent < _POWERLEVEL9K_BATTERY_LOW_THRESHOLD )); then
state=LOW
else
state=DISCONNECTED
fi
if (( power_now > 0 )); then
(( is_charching )) && local -i e=$((energy_full - energy_now)) || local -i e=energy_now
local -i minutes=$(( 60 * e / power_now ))
(( minutes > 0 )) && remain=$((minutes/60)):${(l#2##0#)$((minutes%60))}
elif (( is_calculating )); then
remain="..."
fi
fi fi
if (( power_now > 0 )); then else
(( is_charching )) && local -i e=$((energy_full - energy_now)) || local -i e=energy_now # See https://wiki.termux.com/wiki/Termux-battery-status
local -i minutes=$(( 60 * e / power_now )) if [[ -x `which termux-battery-status` ]] && [[ -x `which jq` ]]; then
(( minutes > 0 )) && remain=$((minutes/60)):${(l#2##0#)$((minutes%60))} (( bat_percent = `termux-battery-status|jq -r .percentage` ))
elif (( is_calculating )); then state=`termux-battery-status|jq -r .status`
remain="..." case $state in
NOT_CHARGING)
(( bat_percent < _POWERLEVEL9K_BATTERY_LOW_THRESHOLD )) && state=LOW || state=DISCONNECTED
;;
CHARGING)
(( bat_percent == 100 )) && state=CHARGED
;;
esac
else
return
fi fi
fi fi
;; ;;