39e61614f206f6a552f53c8d1a8b956d8c9ab32f

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

Message

Clean up Linux battery commands and syntax

Diff

 1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
 2index 5322dd18a2ca164b436b0ae2117d81d6b36e1aaa..c1037ad2643be9b19435595139127242c5dd156c 100644
 3--- a/plugins/battery/battery.plugin.zsh
 4+++ b/plugins/battery/battery.plugin.zsh
 5@@ -116,17 +116,17 @@ elif [[ "$OSTYPE" = freebsd*  ]] ; then
 6 elif [[ "$OSTYPE" = linux*  ]] ; then
 7 
 8   function battery_is_charging() {
 9-    ! [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]]
10+    ! acpi 2>/dev/null | command grep -q '^Battery.*Discharging'
11   }
12 
13   function battery_pct() {
14     if (( $+commands[acpi] )) ; then
15-      echo "$(acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]')"
16+      acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]'
17     fi
18   }
19 
20   function battery_pct_remaining() {
21-    if [ ! $(battery_is_charging) ] ; then
22+    if ! battery_is_charging; then
23       battery_pct
24     else
25       echo "External Power"
26@@ -134,15 +134,17 @@ elif [[ "$OSTYPE" = linux*  ]] ; then
27   }
28 
29   function battery_time_remaining() {
30-    if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
31-      echo $(acpi 2>/dev/null | cut -f3 -d ',')
32+    if ! battery_is_charging; then
33+      acpi 2>/dev/null | cut -f3 -d ','
34     fi
35   }
36 
37   function battery_pct_prompt() {
38-    b=$(battery_pct_remaining) 
39-    if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
40-      if [ $b -gt 50 ] ; then
41+    local b=$(battery_pct_remaining)
42+    if battery_is_charging; then
43+      echo "∞"
44+    else
45+      if [[ $b -gt 50 ]]; then
46         color='green'
47       elif [ $b -gt 20 ] ; then
48         color='yellow'
49@@ -150,8 +152,6 @@ elif [[ "$OSTYPE" = linux*  ]] ; then
50         color='red'
51       fi
52       echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}"
53-    else
54-      echo "∞"
55     fi
56   }
57