1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-17 10:10:07 +00:00

Fix MacOS specific battery segment logic.

The case statement was causing it to never display that it was charging
and some of the awk statements had an off-by-one bug to grab the correct
values.
This commit is contained in:
James Short 2017-01-18 14:50:00 -08:00
parent c5909e71ea
commit 22540e04b8

View file

@ -339,7 +339,7 @@ prompt_background_jobs() {
prompt_battery() { prompt_battery() {
# The battery can have four different states - default to 'unknown'. # The battery can have four different states - default to 'unknown'.
local current_state="unknown" local current_state='unknown'
typeset -AH battery_states typeset -AH battery_states
battery_states=( battery_states=(
'low' 'red' 'low' 'red'
@ -352,14 +352,14 @@ prompt_battery() {
if [[ $OS =~ OSX && -f /usr/bin/pmset && -x /usr/bin/pmset ]]; then if [[ $OS =~ OSX && -f /usr/bin/pmset && -x /usr/bin/pmset ]]; then
# obtain battery information from system # obtain battery information from system
local raw_data="$(pmset -g batt)" local raw_data="$(pmset -g batt | awk 'FNR==2{print}')"
# return if there is no battery on system # return if there is no battery on system
[[ -z $(echo $raw_data | grep "InternalBattery") ]] && return [[ -z $(echo $raw_data | grep "InternalBattery") ]] && return
# Time remaining on battery operation (charging/discharging) # Time remaining on battery operation (charging/discharging)
local tstring=$(echo $raw_data | awk 'FNR==2{print $5}') local tstring=$(echo $raw_data | awk -F ';' '{print $3}' | awk '{print $1}')
# If time has not been calculated by system yet # If time has not been calculated by system yet
[[ $tstring =~ '\(no' ]] && tstring="..." [[ $tstring =~ '(\(no|not)' ]] && tstring="..."
# percent of battery charged # percent of battery charged
typeset -i 10 bat_percent typeset -i 10 bat_percent
@ -367,12 +367,13 @@ prompt_battery() {
local remain="" local remain=""
# Logic for string output # Logic for string output
case $(echo $raw_data | awk 'FNR==2{print $4}') in case $(echo $raw_data | awk -F ';' '{print $2}' | awk '{$1=$1};1') in
'charging;|finishing charge;') # for a short time after attaching power, status will be 'AC attached;'
'charging'|'finishing charge'|'AC attached')
current_state="charging" current_state="charging"
remain=" ($tstring)" remain=" ($tstring)"
;; ;;
'discharging;') 'discharging')
[[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected" [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected"
remain=" ($tstring)" remain=" ($tstring)"
;; ;;