From 7774294113603e8fd4e3c6f1c1c6fb5aeae6cdb2 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 23 Nov 2015 00:09:37 +0100 Subject: [PATCH 01/12] Performance improvement by calculating the battery percentage with zsh. --- powerlevel9k.zsh-theme | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 002267f4..5b14647a 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -238,7 +238,12 @@ prompt_battery() { local max_capacity=$(ioreg -n AppleSmartBattery | grep MaxCapacity | awk '{ print $5 }') local current_capacity=$(ioreg -n AppleSmartBattery | grep CurrentCapacity | awk '{ print $5 }') - [[ ! -z $max_capacity && ! -z $current_capacity ]] && local bat_percent=$(ruby -e "puts ($current_capacity.to_f / $max_capacity.to_f * 100).round.to_i") + if [[ -n "$max_capacity" && -n "$current_capacity" ]]; then + typeset -F 2 current_capacity + current_capacity="$current_capacity"+0.00001 + typeset -i 10 bat_percent + bat_percent=$(( (current_capacity / max_capacity) * 100 )) + fi # logic for string output [[ $charging =~ true && $connected =~ true ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGING" && local remain=" ($tstring)" From c0efe9c342450b4d20488633a2e1bfd1e30ce7e0 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 23 Nov 2015 00:11:19 +0100 Subject: [PATCH 02/12] Performance improvement: Avoid aggregation of data over and over again by extracting a local variable. --- powerlevel9k.zsh-theme | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 5b14647a..6fec74ca 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -220,23 +220,24 @@ prompt_battery() { [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local conn="$DEFAULT_COLOR_INVERTED" || local conn="$POWERLEVEL9K_BATTERY_FOREGROUND" if [[ $OS =~ OSX && -f /usr/sbin/ioreg && -x /usr/sbin/ioreg ]]; then + local raw_data=$(ioreg -n AppleSmartBattery) # return if there is no battery on system - [[ -z $(ioreg -n AppleSmartBattery | grep MaxCapacity) ]] && return + [[ -z $(echo $raw_data | grep MaxCapacity) ]] && return # get charge status - [[ $(ioreg -n AppleSmartBattery | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]] && local connected=true - [[ $(ioreg -n AppleSmartBattery | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]] && local charging=true + [[ $(echo $raw_data | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]] && local connected=true + [[ $(echo $raw_data | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]] && local charging=true # convert time remaining from minutes to hours:minutes date string - local time_remaining=$(ioreg -n AppleSmartBattery | grep TimeRemaining | awk '{ print $5 }') + local time_remaining=$(echo $raw_data | grep TimeRemaining | awk '{ print $5 }') if [[ ! -z $time_remaining ]]; then # this value is set to a very high number when the system is calculating [[ $time_remaining -gt 10000 ]] && local tstring="..." || local tstring=${(f)$(date -u -r $(($time_remaining * 60)) +%k:%M)} fi # get charge values - local max_capacity=$(ioreg -n AppleSmartBattery | grep MaxCapacity | awk '{ print $5 }') - local current_capacity=$(ioreg -n AppleSmartBattery | grep CurrentCapacity | awk '{ print $5 }') + local max_capacity=$(echo $raw_data | grep MaxCapacity | awk '{ print $5 }') + local current_capacity=$(echo $raw_data | grep CurrentCapacity | awk '{ print $5 }') if [[ -n "$max_capacity" && -n "$current_capacity" ]]; then typeset -F 2 current_capacity From b316089c6bca112c88f6030f4f5b70d4236f2dbc Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 23 Nov 2015 00:12:52 +0100 Subject: [PATCH 03/12] Make use of set_default method for better code readability. --- powerlevel9k.zsh-theme | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 6fec74ca..1f0069c3 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -211,11 +211,11 @@ prompt_aws() { prompt_battery() { icons[BATTERY_ICON]=$'\UE894' # set default values of not specified in shell - [[ -z $POWERLEVEL9K_BATTERY_CHARGING ]] && POWERLEVEL9K_BATTERY_CHARGING="yellow" - [[ -z $POWERLEVEL9K_BATTERY_CHARGED ]] && POWERLEVEL9K_BATTERY_CHARGED="green" - [[ -z $POWERLEVEL9K_BATTERY_DISCONNECTED ]] && POWERLEVEL9K_BATTERY_DISCONNECTED="$DEFAULT_COLOR_INVERTED" - [[ -z $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && POWERLEVEL9K_BATTERY_LOW_THRESHOLD=10 - [[ -z $POWERLEVEL9K_BATTERY_LOW_COLOR ]] && POWERLEVEL9K_BATTERY_LOW_COLOR="red" + set_default POWERLEVEL9K_BATTERY_CHARGING "yellow" + set_default POWERLEVEL9K_BATTERY_CHARGED "green" + set_default POWERLEVEL9K_BATTERY_DISCONNECTED "$DEFAULT_COLOR_INVERTED" + set_default POWERLEVEL9K_BATTERY_LOW_THRESHOLD 10 + set_default POWERLEVEL9K_BATTERY_LOW_COLOR "red" [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local fg_color="" || local fg_color="%F{$POWERLEVEL9K_BATTERY_FOREGROUND}" [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local conn="$DEFAULT_COLOR_INVERTED" || local conn="$POWERLEVEL9K_BATTERY_FOREGROUND" From 8d72b5eb123973f94e1188d697ca5e3e62d8269f Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 23 Nov 2015 00:42:15 +0100 Subject: [PATCH 04/12] Renamed variable "conn" to "icon_color" --- powerlevel9k.zsh-theme | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 1f0069c3..6faa6523 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -217,7 +217,7 @@ prompt_battery() { set_default POWERLEVEL9K_BATTERY_LOW_THRESHOLD 10 set_default POWERLEVEL9K_BATTERY_LOW_COLOR "red" [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local fg_color="" || local fg_color="%F{$POWERLEVEL9K_BATTERY_FOREGROUND}" - [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local conn="$DEFAULT_COLOR_INVERTED" || local conn="$POWERLEVEL9K_BATTERY_FOREGROUND" + [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local icon_color="$DEFAULT_COLOR_INVERTED" || local icon_color="$POWERLEVEL9K_BATTERY_FOREGROUND" if [[ $OS =~ OSX && -f /usr/sbin/ioreg && -x /usr/sbin/ioreg ]]; then local raw_data=$(ioreg -n AppleSmartBattery) @@ -247,10 +247,10 @@ prompt_battery() { fi # logic for string output - [[ $charging =~ true && $connected =~ true ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGING" && local remain=" ($tstring)" - [[ ! $charging =~ true && $connected =~ true ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGED" && local remain="" + [[ $charging =~ true && $connected =~ true ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGING" && local remain=" ($tstring)" + [[ ! $charging =~ true && $connected =~ true ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGED" && local remain="" if [[ ! $connected =~ true ]]; then - [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local conn="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local conn="$POWERLEVEL9K_BATTERY_DISCONNECTED" + [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local icon_color="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local icon_color="$POWERLEVEL9K_BATTERY_DISCONNECTED" local remain=" ($tstring)" fi fi @@ -265,10 +265,10 @@ prompt_battery() { [[ -z $bat ]] && return [[ $(cat $bat/capacity) -gt 100 ]] && local bat_percent=100 || local bat_percent=$(cat $bat/capacity) [[ $(cat $bat/status) =~ Charging ]] && local connected=true - [[ $(cat $bat/status) =~ Charging && $bat_percent =~ 100 ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGED" - [[ $(cat $bat/status) =~ Charging && $bat_percent -lt 100 ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGING" + [[ $(cat $bat/status) =~ Charging && $bat_percent =~ 100 ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGED" + [[ $(cat $bat/status) =~ Charging && $bat_percent -lt 100 ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGING" if [[ -z $connected ]]; then - [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local conn="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local conn="$POWERLEVEL9K_BATTERY_DISCONNECTED" + [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local icon_color="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local icon_color="$POWERLEVEL9K_BATTERY_DISCONNECTED" fi if [[ -f /usr/bin/acpi ]]; then local time_remaining=$(acpi | awk '{ print $5 }') @@ -282,7 +282,7 @@ prompt_battery() { fi # display prompt_segment - [[ ! -z $bat_percent ]] && "$1_prompt_segment" "$0" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "%F{$conn}$(print_icon 'BATTERY_ICON')$fg_color $bat_percent%%$remain%f" + [[ ! -z $bat_percent ]] && "$1_prompt_segment" "$0" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "%F{$icon_color}$(print_icon 'BATTERY_ICON')$fg_color $bat_percent%%$remain%f" } # Context: user@hostname (who am I and where am I) From 239aa29a1287d58ad61e6b9ff342fb7a7f2bf31e Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 23 Nov 2015 00:48:59 +0100 Subject: [PATCH 05/12] Better use of test flags. --- powerlevel9k.zsh-theme | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 6faa6523..7ce82e0a 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -230,7 +230,7 @@ prompt_battery() { # convert time remaining from minutes to hours:minutes date string local time_remaining=$(echo $raw_data | grep TimeRemaining | awk '{ print $5 }') - if [[ ! -z $time_remaining ]]; then + if [[ -n $time_remaining ]]; then # this value is set to a very high number when the system is calculating [[ $time_remaining -gt 10000 ]] && local tstring="..." || local tstring=${(f)$(date -u -r $(($time_remaining * 60)) +%k:%M)} fi @@ -278,11 +278,11 @@ prompt_battery() { local tstring=${(f)$(date -u -d "$(echo $time_remaining)" +%k:%M)} fi fi - [[ ! -z $tstring ]] && local remain=" ($tstring)" + [[ -n $tstring ]] && local remain=" ($tstring)" fi # display prompt_segment - [[ ! -z $bat_percent ]] && "$1_prompt_segment" "$0" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "%F{$icon_color}$(print_icon 'BATTERY_ICON')$fg_color $bat_percent%%$remain%f" + [[ -n $bat_percent ]] && "$1_prompt_segment" "$0" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "%F{$icon_color}$(print_icon 'BATTERY_ICON')$fg_color $bat_percent%%$remain%f" } # Context: user@hostname (who am I and where am I) From da2b8e342efa952dea4b664fac6179d75804a2b9 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 23 Nov 2015 18:10:09 +0100 Subject: [PATCH 06/12] Arithmetics in ZSH 101: If you want to calculate a percentage, multiply the numerator with 100 _before_ the devision. With that method there is no need for hacks to convert the numerator into a float. --- powerlevel9k.zsh-theme | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 7ce82e0a..c1d1abf0 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -240,10 +240,8 @@ prompt_battery() { local current_capacity=$(echo $raw_data | grep CurrentCapacity | awk '{ print $5 }') if [[ -n "$max_capacity" && -n "$current_capacity" ]]; then - typeset -F 2 current_capacity - current_capacity="$current_capacity"+0.00001 typeset -i 10 bat_percent - bat_percent=$(( (current_capacity / max_capacity) * 100 )) + bat_percent=$(( (current_capacity * 100) / max_capacity )) fi # logic for string output From dad38b0592141fc4243fb8c67659a3a499f6cc4c Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 24 Nov 2015 02:36:37 +0100 Subject: [PATCH 07/12] Better code structure. --- powerlevel9k.zsh-theme | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c1d1abf0..989f04ce 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -224,10 +224,6 @@ prompt_battery() { # return if there is no battery on system [[ -z $(echo $raw_data | grep MaxCapacity) ]] && return - # get charge status - [[ $(echo $raw_data | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]] && local connected=true - [[ $(echo $raw_data | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]] && local charging=true - # convert time remaining from minutes to hours:minutes date string local time_remaining=$(echo $raw_data | grep TimeRemaining | awk '{ print $5 }') if [[ -n $time_remaining ]]; then @@ -244,12 +240,20 @@ prompt_battery() { bat_percent=$(( (current_capacity * 100) / max_capacity )) fi - # logic for string output - [[ $charging =~ true && $connected =~ true ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGING" && local remain=" ($tstring)" - [[ ! $charging =~ true && $connected =~ true ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGED" && local remain="" - if [[ ! $connected =~ true ]]; then + local remain="" + ## logic for string output + # Powerplug connected + if [[ $(echo $raw_data | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]]; then + # Battery is charging + if [[ $(echo $raw_data | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]]; then + icon_color="$POWERLEVEL9K_BATTERY_CHARGING" + remain=" ($tstring)" + else + icon_color="$POWERLEVEL9K_BATTERY_CHARGED" + fi + else [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local icon_color="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local icon_color="$POWERLEVEL9K_BATTERY_DISCONNECTED" - local remain=" ($tstring)" + remain=" ($tstring)" fi fi @@ -261,6 +265,7 @@ prompt_battery() { # return if no battery found [[ -z $bat ]] && return + [[ $(cat $bat/capacity) -gt 100 ]] && local bat_percent=100 || local bat_percent=$(cat $bat/capacity) [[ $(cat $bat/status) =~ Charging ]] && local connected=true [[ $(cat $bat/status) =~ Charging && $bat_percent =~ 100 ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGED" From c8137fcf1a8461fddb98ada19e03c40834287c21 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 24 Nov 2015 18:17:04 +0100 Subject: [PATCH 08/12] Pre-Grep the raw data to save some memory and to avoid cluttering up the output of `setopt xtrace` too much. --- powerlevel9k.zsh-theme | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 989f04ce..fb5e20d4 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -220,7 +220,9 @@ prompt_battery() { [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local icon_color="$DEFAULT_COLOR_INVERTED" || local icon_color="$POWERLEVEL9K_BATTERY_FOREGROUND" if [[ $OS =~ OSX && -f /usr/sbin/ioreg && -x /usr/sbin/ioreg ]]; then - local raw_data=$(ioreg -n AppleSmartBattery) + # Pre-Grep all needed informations to save some memory and + # as little pollution of the xtrace output as possible. + local raw_data=$(ioreg -n AppleSmartBattery | grep -E "MaxCapacity|TimeRemaining|CurrentCapacity|ExternalConnected|IsCharging") # return if there is no battery on system [[ -z $(echo $raw_data | grep MaxCapacity) ]] && return From 3437c7850cda86e85626e95216644f1fa420ddaa Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 24 Nov 2015 18:31:22 +0100 Subject: [PATCH 09/12] Battery icon is already defined in the icons-array. --- powerlevel9k.zsh-theme | 1 - 1 file changed, 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index fb5e20d4..9eaebaae 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -209,7 +209,6 @@ prompt_aws() { } prompt_battery() { - icons[BATTERY_ICON]=$'\UE894' # set default values of not specified in shell set_default POWERLEVEL9K_BATTERY_CHARGING "yellow" set_default POWERLEVEL9K_BATTERY_CHARGED "green" From 323610cebf133ba998e5e5c964d2d41a547ba4b6 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 24 Nov 2015 18:36:56 +0100 Subject: [PATCH 10/12] More structure to the battery segment: Now there is a concept of "states" that a battery can have. That makes the code much clearer and users could adjust the colors of these states as they want. --- powerlevel9k.zsh-theme | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 9eaebaae..ec447a47 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -209,14 +209,18 @@ prompt_aws() { } prompt_battery() { + # The battery can have different states. + # Default is "unknown" + local current_state="unknown" + typeset -AH battery_states + battery_states=( + 'low' 'red' + 'charging' 'yellow' + 'charged' 'green' + 'disconnected' "$DEFAULT_COLOR_INVERTED" + ) # set default values of not specified in shell - set_default POWERLEVEL9K_BATTERY_CHARGING "yellow" - set_default POWERLEVEL9K_BATTERY_CHARGED "green" - set_default POWERLEVEL9K_BATTERY_DISCONNECTED "$DEFAULT_COLOR_INVERTED" set_default POWERLEVEL9K_BATTERY_LOW_THRESHOLD 10 - set_default POWERLEVEL9K_BATTERY_LOW_COLOR "red" - [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local fg_color="" || local fg_color="%F{$POWERLEVEL9K_BATTERY_FOREGROUND}" - [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local icon_color="$DEFAULT_COLOR_INVERTED" || local icon_color="$POWERLEVEL9K_BATTERY_FOREGROUND" if [[ $OS =~ OSX && -f /usr/sbin/ioreg && -x /usr/sbin/ioreg ]]; then # Pre-Grep all needed informations to save some memory and @@ -247,13 +251,13 @@ prompt_battery() { if [[ $(echo $raw_data | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]]; then # Battery is charging if [[ $(echo $raw_data | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]]; then - icon_color="$POWERLEVEL9K_BATTERY_CHARGING" + current_state="charging" remain=" ($tstring)" else - icon_color="$POWERLEVEL9K_BATTERY_CHARGED" + current_state="charged" fi else - [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local icon_color="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local icon_color="$POWERLEVEL9K_BATTERY_DISCONNECTED" + [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected" remain=" ($tstring)" fi fi @@ -269,10 +273,10 @@ prompt_battery() { [[ $(cat $bat/capacity) -gt 100 ]] && local bat_percent=100 || local bat_percent=$(cat $bat/capacity) [[ $(cat $bat/status) =~ Charging ]] && local connected=true - [[ $(cat $bat/status) =~ Charging && $bat_percent =~ 100 ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGED" - [[ $(cat $bat/status) =~ Charging && $bat_percent -lt 100 ]] && local icon_color="$POWERLEVEL9K_BATTERY_CHARGING" + [[ $(cat $bat/status) =~ Charging && $bat_percent =~ 100 ]] && current_state="charged" + [[ $(cat $bat/status) =~ Charging && $bat_percent -lt 100 ]] && current_state="charging" if [[ -z $connected ]]; then - [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local icon_color="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local icon_color="$POWERLEVEL9K_BATTERY_DISCONNECTED" + [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected" fi if [[ -f /usr/bin/acpi ]]; then local time_remaining=$(acpi | awk '{ print $5 }') @@ -286,7 +290,7 @@ prompt_battery() { fi # display prompt_segment - [[ -n $bat_percent ]] && "$1_prompt_segment" "$0" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "%F{$icon_color}$(print_icon 'BATTERY_ICON')$fg_color $bat_percent%%$remain%f" + [[ -n $bat_percent ]] && "$1_prompt_segment" "${0}_${current_state}" "$DEFAULT_COLOR" "${battery_states[$current_state]}" "$(print_icon 'BATTERY_ICON') $bat_percent%%$remain" } # Context: user@hostname (who am I and where am I) From 23e2a5406718dfd9117de8b63825cf63ee195ff2 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 25 Nov 2015 00:21:20 +0100 Subject: [PATCH 11/12] Added a verbose mode. --- powerlevel9k.zsh-theme | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index ec447a47..76e75441 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -289,8 +289,14 @@ prompt_battery() { [[ -n $tstring ]] && local remain=" ($tstring)" fi + # prepare string + local message="$(print_icon 'BATTERY_ICON')" + if [[ "$POWERLEVEL9K_BATTERY_VERBOSE" == true ]]; then + message="$message $bat_percent%%$remain" + fi + # display prompt_segment - [[ -n $bat_percent ]] && "$1_prompt_segment" "${0}_${current_state}" "$DEFAULT_COLOR" "${battery_states[$current_state]}" "$(print_icon 'BATTERY_ICON') $bat_percent%%$remain" + [[ -n $bat_percent ]] && "$1_prompt_segment" "${0}_${current_state}" "$DEFAULT_COLOR" "${battery_states[$current_state]}" "$message" } # Context: user@hostname (who am I and where am I) From b8d7d1387e0a77ba4046db48c0fcad2c59773942 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 25 Nov 2015 00:26:10 +0100 Subject: [PATCH 12/12] By default: set verbose mode on. --- powerlevel9k.zsh-theme | 2 ++ 1 file changed, 2 insertions(+) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 76e75441..595b233d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -291,6 +291,8 @@ prompt_battery() { # prepare string local message="$(print_icon 'BATTERY_ICON')" + # Default behavior: Be verbose! + set_default POWERLEVEL9K_BATTERY_VERBOSE true if [[ "$POWERLEVEL9K_BATTERY_VERBOSE" == true ]]; then message="$message $bat_percent%%$remain" fi