battery.plugin.zsh 2.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
###########################################
# Battery plugin for oh-my-zsh            #
# Original Author: Peter hoeg (peterhoeg) #
# Email: peter@speartail.com              #
###########################################
# Author: Sean Jones (neuralsandwich)     #
# Email: neuralsandwich@gmail.com         #
# Modified to add support for Apple Mac   #
###########################################

11
if [[ $(uname) == "Darwin" ]] ; then
12

13
14
15
16
17
18
  function battery_pct() {
    typeset -F maxcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
    typeset -F currentcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
    integer i=$(((currentcapacity/maxcapacity) * 100))
    echo $i
  }
Ron Shapiro's avatar
Ron Shapiro committed
19
20
21
22

  function plugged_in() {
    [ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ Yes') -eq 1 ]
  }
23
  
24
  function battery_pct_remaining() {
Ron Shapiro's avatar
Ron Shapiro committed
25
    if plugged_in ; then
26
      echo "External Power"
Ron Shapiro's avatar
Ron Shapiro committed
27
28
    else
      battery_pct
Peter Hoeg's avatar
Peter Hoeg committed
29
30
    fi
  }
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

  function battery_time_remaining() {
    if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
      timeremaining=$(ioreg -rc "AppleSmartBattery"| grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
      echo "~$((timeremaining / 60)):$((timeremaining % 60))"
    else
      echo "∞"
    fi
  }

  function battery_pct_prompt () {
    if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
      b=$(battery_pct_remaining)
      if [ $b -gt 50 ] ; then
        color='green'
      elif [ $b -gt 20 ] ; then
        color='yellow'
      else
        color='red'
      fi
      echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
    else
      echo ""
    fi
  }

57
elif [[ $(uname) == "Linux"  ]] ; then
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

  if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
    function battery_pct_remaining() { echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')" }
    function battery_time_remaining() { echo $(acpi | cut -f3 -d ',') }
    function battery_pct_prompt() {
      b=$(battery_pct_remaining)
      if [ $b -gt 50 ] ; then
        color='green'
      elif [ $b -gt 20 ] ; then
        color='yellow'
      else
        color='red'
      fi
      echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
    }
  else
    error_msg='no battery'
    function battery_pct_remaining() { echo $error_msg }
    function battery_time_remaining() { echo $error_msg }
    function battery_pct_prompt() { echo '' }
  fi
Peter Hoeg's avatar
Peter Hoeg committed
79
fi