1bd7a7ad21c51268ba0f9a0d8b643a82b5788a6a

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Fix calculation for battery percentage (#4774)

Co-authored-by: Michael Wolman <michael.s.wolman@gmail.com>

Diff

 1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
 2index 6b668471606f9ac8248f0898815a45deb37ba927..5322dd18a2ca164b436b0ae2117d81d6b36e1aaa 100644
 3--- a/plugins/battery/battery.plugin.zsh
 4+++ b/plugins/battery/battery.plugin.zsh
 5@@ -169,8 +169,8 @@ fi
 6 
 7 function battery_level_gauge() {
 8   local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
 9-  local green_threshold=${BATTERY_GREEN_THRESHOLD:-6};
10-  local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-4};
11+  local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))};
12+  local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))};
13   local color_green=${BATTERY_COLOR_GREEN:-%F{green}};
14   local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}};
15   local color_red=${BATTERY_COLOR_RED:-%F{red}};
16@@ -183,26 +183,35 @@ function battery_level_gauge() {
17   local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'};
18 
19   local battery_remaining_percentage=$(battery_pct);
20+  local filled empty gauge_color
21 
22   if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
23-    local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots)));
24-    local empty=$(($gauge_slots - $filled));
25+    filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ));
26+    empty=$(( $gauge_slots - $filled ));
27 
28-    if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green;
29-    elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow;
30-    else local gauge_color=$color_red;
31+    if [[ $filled -gt $green_threshold ]]; then
32+      gauge_color=$color_green;
33+    elif [[ $filled -gt $yellow_threshold ]]; then
34+      gauge_color=$color_yellow;
35+    else
36+      gauge_color=$color_red;
37     fi
38   else
39-    local filled=$gauge_slots;
40-    local empty=0;
41+    filled=$gauge_slots;
42+    empty=0;
43     filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
44   fi
45 
46-  local charging=' ' && battery_is_charging && charging=$charging_symbol;
47+  local charging=' '
48+  battery_is_charging && charging=$charging_symbol;
49 
50+  # Charging status and prefix
51   printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
52-  printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
53+  # Filled slots
54+  [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
55+  # Empty slots
56   [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
57+  # Suffix
58   printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
59 }
60