battery.plugin.zsh 5.03 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
  function battery_pct() {
14
15
16
    local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
    typeset -F maxcapacity=$(echo $smart_battery_status | grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
    typeset -F currentcapacity=$(echo $smart_battery_status | grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
17
18
19
    integer i=$(((currentcapacity/maxcapacity) * 100))
    echo $i
  }
Ron Shapiro's avatar
Ron Shapiro committed
20
21
22
23

  function plugged_in() {
    [ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ Yes') -eq 1 ]
  }
24

25
  function battery_pct_remaining() {
Ron Shapiro's avatar
Ron Shapiro committed
26
    if plugged_in ; then
27
      echo "External Power"
Ron Shapiro's avatar
Ron Shapiro committed
28
29
    else
      battery_pct
Peter Hoeg's avatar
Peter Hoeg committed
30
31
    fi
  }
32
33

  function battery_time_remaining() {
34
    local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
35
36
    if [[ $(echo $smart_battery_status | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
      timeremaining=$(echo $smart_battery_status | grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
37
38
39
40
41
      if [ $timeremaining -gt 720 ] ; then
        echo "::"
      else
        echo "~$((timeremaining / 60)):$((timeremaining % 60))"
      fi
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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
59
      echo "∞"
60
61
    fi
  }
62

63
  function battery_is_charging() {
64
    [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]
65
  }
66

67
elif [[ $(uname) == "Linux"  ]] ; then
68

69
70
71
72
73
  function battery_is_charging() {
    ! [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]]
  }

  function battery_pct() {
74
75
76
    if (( $+commands[acpi] )) ; then
      echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')"
    fi
77
78
  }

79
  function battery_pct_remaining() {
80
81
82
83
    if [ ! $(battery_is_charging) ] ; then
      battery_pct
    else
      echo "External Power"
84
85
86
87
88
89
90
91
92
93
94
95
    fi
  }

  function battery_time_remaining() {
    if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
      echo $(acpi | cut -f3 -d ',')
    fi
  }

  function battery_pct_prompt() {
    b=$(battery_pct_remaining) 
    if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
96
97
98
99
100
101
102
103
      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%}"
104
105
106
107
    else
      echo "∞"
    fi
  }
108

109
else
110
111
112
  # Empty functions so we don't cause errors in prompts
  function battery_pct_remaining() {
  }
113

114
115
  function battery_time_remaining() {
  }
116

117
118
  function battery_pct_prompt() {
  }
Peter Hoeg's avatar
Peter Hoeg committed
119
fi
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

function battery_level_gauge() {
  local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
  local green_threshold=${BATTERY_GREEN_THRESHOLD:-6};
  local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-4};
  local color_green=${BATTERY_COLOR_GREEN:-%F{green}};
  local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}};
  local color_red=${BATTERY_COLOR_RED:-%F{red}};
  local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}};
  local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['};
  local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'};
  local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'};
  local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'};
  local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow};
  local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'};

  local battery_remaining_percentage=$(battery_pct);

  if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
    local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots)));
    local empty=$(($gauge_slots - $filled));
141

142
143
144
145
146
147
148
    if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green;
    elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow;
    else local gauge_color=$color_red;
    fi
  else
    local filled=$gauge_slots;
    local empty=0;
149
    filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
150
151
152
153
154
155
156
157
158
159
160
  fi

  local charging=' ' && battery_is_charging && charging=$charging_symbol;

  printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
  printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
  printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
}