41d9717252375936af072810c5c168f30fcdc840

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #2402 from bric3/battery_level_gauge

Adds a method that print a battery gauge

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
  2index 9f053383a7afbb560742ff3c338dbf77d282d658..16ad3e6510a6937f528fb120740bc4efcfd592d5 100644
  3--- a/plugins/battery/battery.plugin.zsh
  4+++ b/plugins/battery/battery.plugin.zsh
  5@@ -11,8 +11,9 @@
  6 if [[ $(uname) == "Darwin" ]] ; then
  7 
  8   function battery_pct() {
  9-    typeset -F maxcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
 10-    typeset -F currentcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
 11+    local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
 12+    typeset -F maxcapacity=$(echo $smart_battery_status | grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
 13+    typeset -F currentcapacity=$(echo $smart_battery_status | grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
 14     integer i=$(((currentcapacity/maxcapacity) * 100))
 15     echo $i
 16   }
 17@@ -26,8 +27,9 @@ if [[ $(uname) == "Darwin" ]] ; then
 18   }
 19 
 20   function battery_time_remaining() {
 21-    if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
 22-      timeremaining=$(ioreg -rc "AppleSmartBattery"| grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
 23+  	local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
 24+    if [[ $(echo $smart_battery_status | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
 25+      timeremaining=$(echo $smart_battery_status | grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
 26       echo "~$((timeremaining / 60)):$((timeremaining % 60))"
 27     else
 28       echo "∞"
 29@@ -49,6 +51,10 @@ if [[ $(uname) == "Darwin" ]] ; then
 30       echo "∞"
 31     fi
 32   }
 33+  
 34+  function battery_is_charging() {
 35+	  [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]
 36+  }
 37 
 38 elif [[ $(uname) == "Linux"  ]] ; then
 39 
 40@@ -79,14 +85,66 @@ elif [[ $(uname) == "Linux"  ]] ; then
 41       echo "∞"
 42     fi
 43   }
 44+  
 45+  function battery_pct() {
 46+    # todo for on linux
 47+  }
 48+  
 49+  function battery_is_charging() {
 50+    # todo on linux
 51+	false
 52+  }
 53+
 54 else
 55-	# Empty functions so we don't cause errors in prompts
 56-	function battery_pct_remaining() {
 57-	}
 58+  # Empty functions so we don't cause errors in prompts
 59+  function battery_pct_remaining() {
 60+  }
 61 
 62-	function battery_time_remaining() {
 63-	}
 64+  function battery_time_remaining() {
 65+  }
 66 
 67-	function battery_pct_prompt() {
 68-	}
 69+  function battery_pct_prompt() {
 70+  }
 71 fi
 72+
 73+function battery_level_gauge() {
 74+  local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
 75+  local green_threshold=${BATTERY_GREEN_THRESHOLD:-6};
 76+  local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-4};
 77+  local color_green=${BATTERY_COLOR_GREEN:-%F{green}};
 78+  local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}};
 79+  local color_red=${BATTERY_COLOR_RED:-%F{red}};
 80+  local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}};
 81+  local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['};
 82+  local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'};
 83+  local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'};
 84+  local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'};
 85+  local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow};
 86+  local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'};
 87+
 88+  local battery_remaining_percentage=$(battery_pct);
 89+
 90+  if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
 91+    local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots)));
 92+    local empty=$(($gauge_slots - $filled));
 93+	
 94+    if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green;
 95+    elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow;
 96+    else local gauge_color=$color_red;
 97+    fi
 98+  else
 99+    local filled=$gauge_slots;
100+    local empty=0;
101+  	filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
102+  fi
103+
104+