e226fc048afaf7c849e089304f855f4ebb26ee95

Author
Sean Jones <neuralsandwich@gmail.com>
Committer
Sean Jones <neuralsandwich@gmail.com>
Date

Message

Added support for Mac to battery plugin

Diff

 1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
 2index bc75c5cf9ad01bd58ef29c4d83782ce5a849baff..9f404088a0bf66a8cedc4b0b49f0a67f9d1ebb4f 100644
 3--- a/plugins/battery/battery.plugin.zsh
 4+++ b/plugins/battery/battery.plugin.zsh
 5@@ -1,20 +1,71 @@
 6-if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
 7-  function battery_pct_remaining() { echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')" }
 8-  function battery_time_remaining() { echo $(acpi | cut -f3 -d ',') }
 9-  function battery_pct_prompt() {
10-    b=$(battery_pct_remaining)
11-    if [ $b -gt 50 ] ; then
12-      color='green'
13-    elif [ $b -gt 20 ] ; then
14-      color='yellow'
15+###########################################
16+# Battery plugin for oh-my-zsh            #
17+# Original Author: Peter hoeg (peterhoeg) #
18+# Email: peter@speartail.com              #
19+###########################################
20+# Author: Sean Jones (neuralsandwich)     #
21+# Email: neuralsandwich@gmail.com         #
22+# Modified to add support for Apple Mac   #
23+###########################################
24+
25+if [[ $(uname) -eq "Darwin" ]] ; then
26+
27+  function battery_pct_remaining() {
28+    if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
29+      typeset -F maxcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
30+      typeset -F currentcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
31+      integer i=$(((currentcapacity/maxcapacity) * 100))
32+      echo $i
33     else
34-      color='red'
35+      echo "External Power"
36     fi
37-    echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
38   }
39-else
40-  error_msg='no battery'
41-  function battery_pct_remaining() { echo $error_msg }
42-  function battery_time_remaining() { echo $error_msg }
43-  function battery_pct_prompt() { echo '' }
44+
45+  function battery_time_remaining() {
46+    if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
47+      timeremaining=$(ioreg -rc "AppleSmartBattery"| grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
48+      echo "~$((timeremaining / 60)):$((timeremaining % 60))"
49+    else
50+      echo "∞"
51+    fi
52+  }
53+
54+  function battery_pct_prompt () {
55+    if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
56+      b=$(battery_pct_remaining)
57+      if [ $b -gt 50 ] ; then
58+        color='green'
59+      elif [ $b -gt 20 ] ; then
60+        color='yellow'
61+      else
62+        color='red'
63+      fi
64+      echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
65+    else
66+      echo ""
67+    fi
68+  }
69+
70+elif [[ $(uname) -eq "Linux"  ]] ; then
71+
72+  if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
73+    function battery_pct_remaining() { echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')" }
74+    function battery_time_remaining() { echo $(acpi | cut -f3 -d ',') }
75+    function battery_pct_prompt() {
76+      b=$(battery_pct_remaining)
77+      if [ $b -gt 50 ] ; then
78+        color='green'
79+      elif [ $b -gt 20 ] ; then
80+        color='yellow'
81+      else
82+        color='red'
83+      fi
84+      echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
85+    }
86+  else
87+    error_msg='no battery'
88+    function battery_pct_remaining() { echo $error_msg }
89+    function battery_time_remaining() { echo $error_msg }
90+    function battery_pct_prompt() { echo '' }
91+  fi
92 fi