speed up battery prompt

pull/71/head
romkatv 6 years ago
parent 99457a4ad5
commit 89d7caa16b

@ -456,115 +456,112 @@ set_default -a POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND
set_default POWERLEVEL9K_BATTERY_VERBOSE true set_default POWERLEVEL9K_BATTERY_VERBOSE true
typeset -g POWERLEVEL9K_BATTERY_STAGES typeset -g POWERLEVEL9K_BATTERY_STAGES
prompt_battery() { typeset -gA _P9K_BATTERY_STATES=(
# The battery can have four different states - default to 'unknown'.
local current_state='unknown'
typeset -AH battery_states
battery_states=(
'low' 'red' 'low' 'red'
'charging' 'yellow' 'charging' 'yellow'
'charged' 'green' 'charged' 'green'
'disconnected' "$DEFAULT_COLOR_INVERTED" 'disconnected' "$DEFAULT_COLOR_INVERTED"
) )
local ROOT_PREFIX="${4}"
if [[ $OS =~ OSX && -f "${ROOT_PREFIX}"/usr/bin/pmset && -x "${ROOT_PREFIX}"/usr/bin/pmset ]]; then function _p9k_read_file() {
# obtain battery information from system _P9K_RETVAL=''
local raw_data="$(${ROOT_PREFIX}/usr/bin/pmset -g batt | awk 'FNR==2{print}')" [[ -n $1 ]] && read -r _P9K_RETVAL <$1
# return if there is no battery on system [[ -n $_P9K_RETVAL ]]
[[ -z $(echo $raw_data | grep "InternalBattery") ]] && return }
# Time remaining on battery operation (charging/discharging) prompt_battery() {
local tstring=$(echo $raw_data | awk -F ';' '{print $3}' | awk '{print $1}') local state remain
# If time has not been calculated by system yet local -i bat_percent
[[ $tstring =~ '(\(no|not)' ]] && tstring="..."
case $OS in
# percent of battery charged OSX)
typeset -i 10 bat_percent (( $+commands[pmset] )) || return
bat_percent=$(echo $raw_data | grep -o '[0-9]*%' | sed 's/%//') local raw_data=${${(f)$(command pmset -g batt)}[2]}
[[ $raw_data == *InternalBattery* ]] || return
local remain="" remain=${${(s: :)${${(s:; :)raw_data}[3]}}[1]}
# Logic for string output [[ $remain == *no* ]] && remain="..."
case $(echo $raw_data | awk -F ';' '{print $2}' | awk '{$1=$1};1') in [[ $raw_data =~ '([0-9]+)%' ]] && bat_percent=$match[1]
# for a short time after attaching power, status will be 'AC attached;'
case "${${(s:; :)raw_data}[2]}" in
'charging'|'finishing charge'|'AC attached') 'charging'|'finishing charge'|'AC attached')
current_state="charging" state=charging
remain=" ($tstring)"
;; ;;
'discharging') 'discharging')
[[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected" (( bat_percent < POWERLEVEL9K_BATTERY_LOW_THRESHOLD )) && state=low || state=disconnected
remain=" ($tstring)"
;; ;;
*) *)
current_state="charged" state=charged
remain=''
;; ;;
esac esac
fi ;;
if [[ "$OS" == 'Linux' ]] || [[ "$OS" == 'Android' ]]; then
local sysp="${ROOT_PREFIX}/sys/class/power_supply"
# Reported BAT0 or BAT1 depending on kernel version
[[ -a $sysp/BAT0 ]] && local bat=$sysp/BAT0
[[ -a $sysp/BAT1 ]] && local bat=$sysp/BAT1
# Android-related Linux|Android)
# Tested on: Moto G falcon (CM 13.0) local -a bats=( /sys/class/power_supply/(BAT*|battery)(FN) )
[[ -a $sysp/battery ]] && local bat=$sysp/battery (( $#bats )) || return
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
_p9k_read_file $dir/(energy|charge)_now(N) && (( energy_now+=_P9K_RETVAL ))
_p9k_read_file $dir/(energy|charge)_full(N) && (( energy_full+=_P9K_RETVAL ))
_p9k_read_file $dir/(power|current)_now(N) && (( power_now+=${pow::=$_P9K_RETVAL} ))
_p9k_read_file $dir/status(N) && local bat_status=$_P9K_RETVAL || continue
[[ $bat_status != Full ]] && is_full=0
[[ $bat_status == Charging ]] && is_charching=1
[[ $bat_status == (Charging|Discharging) && $pow == 0 ]] && is_calculating=1
done
# Return if no battery found if (( energy_full )); then
[[ -z $bat ]] && return bat_percent=$(( 100 * energy_now / energy_full ))
local capacity=$(cat $bat/capacity) (( bat_percent > 100 )) && bat_percent=100
local battery_status=$(cat $bat/status)
[[ $capacity -gt 100 ]] && local bat_percent=100 || local bat_percent=$capacity
[[ $battery_status =~ Charging || $battery_status =~ Full ]] && local connected=true
if [[ -z $connected ]]; then
[[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected"
else
[[ $bat_percent =~ 100 ]] && current_state="charged"
[[ $bat_percent -lt 100 ]] && current_state="charging"
fi
if [[ -f ${ROOT_PREFIX}/usr/bin/acpi ]]; then
local time_remaining=$(${ROOT_PREFIX}/usr/bin/acpi | awk '{ print $5 }')
if [[ $time_remaining =~ rate ]]; then
local tstring="..."
elif [[ $time_remaining =~ "[[:digit:]]+" ]]; then
local tstring=${(f)$(date -u -d "$(echo $time_remaining)" +%k:%M 2> /dev/null)}
fi
fi
[[ -n $tstring ]] && local remain=" ($tstring)"
fi fi
local message if (( is_full || bat_percent == 100 )); then
if [[ "$POWERLEVEL9K_BATTERY_VERBOSE" == true ]]; then state=charged
message="$bat_percent%%$remain"
else else
message="$bat_percent%%" if (( is_charching )); then
state=charging
elif (( bat_percent < POWERLEVEL9K_BATTERY_LOW_THRESHOLD )); then
state=low
else
state=disconnected
fi fi
# override default icon if we are using battery stages if (( power_now > 0 )); then
if [[ -n "$POWERLEVEL9K_BATTERY_STAGES" ]]; then (( is_charching )) && local -i e=$((energy_full - energy_now)) || local -i e=energy_now
local segment=$(( 100.0 / (${#POWERLEVEL9K_BATTERY_STAGES} - 1 ) )) local -i minutes=$(( 60 * e / power_now ))
if [[ $segment > 1 ]]; then (( minutes > 0 )) && remain=$((minutes/60)):${(l#2##0#)$((minutes%60))}
local offset=$(( ($bat_percent / $segment) + 1 )) elif (( is_calculating )); then
# check if the stages are in an array or a string remain="..."
[[ "${(t)POWERLEVEL9K_BATTERY_STAGES}" =~ "array" ]] && POWERLEVEL9K_BATTERY_ICON="$POWERLEVEL9K_BATTERY_STAGES[$offset]" || POWERLEVEL9K_BATTERY_ICON=${POWERLEVEL9K_BATTERY_STAGES:$offset:1}
fi fi
fi fi
# return if POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD is set and the battery percentage is greater or equal ;;
if (( bat_percent >= POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD )); then
*)
return return
fi ;;
esac
# override the default color if we are using a color level array (( bat_percent >= POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD )) && return
if (( #POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND )); then
local segment=$(( 100.0 / (${#POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND} - 1 ) )) local msg="$bat_percent%%"
local offset=$(( ($bat_percent / $segment) + 1 )) [[ $POWERLEVEL9K_BATTERY_VERBOSE == true && -n $remain ]] && msg+=" ($remain)"
"$1_prompt_segment" "$0_${current_state}" "$2" "${POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND[$offset]}" "${battery_states[$current_state]}" "BATTERY_ICON" 0 '' "${message}"
else local icon=BATTERY_ICON bg=$DEFAULT_COLOR
# Draw the prompt_segment if (( $#POWERLEVEL9K_BATTERY_STAGES || $#POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND )); then
"$1_prompt_segment" "$0_${current_state}" "$2" "${DEFAULT_COLOR}" "${battery_states[$current_state]}" "BATTERY_ICON" 0 '' "${message}" local -i idx=$#POWERLEVEL9K_BATTERY_STAGES
(( bat_percent < 100 )) && idx=$((bat_percent * $#POWERLEVEL9K_BATTERY_STAGES / 100 + 1))
if (( $#POWERLEVEL9K_BATTERY_STAGES )); then
icon+=_$idx
typeset -g POWERLEVEL9K_$icon=$POWERLEVEL9K_BATTERY_STAGES[idx]
fi
(( $#POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND )) && bg=$POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND[idx]
fi fi
$1_prompt_segment $0_$state $2 "$bg" "$_P9K_BATTERY_STATES[$state]" $icon 0 '' $msg
} }
################################################################ ################################################################

Loading…
Cancel
Save