battery.plugin.zsh 6.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
###########################################
# 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   #
###########################################
10
11
12
# Author: J (927589452)                   #
# Modified to add support for FreeBSD     #
###########################################
13

14
if [[ "$OSTYPE" = darwin* ]] ; then
15

16
  function battery_pct() {
17
18
19
    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"\ =\ //')
20
21
22
    integer i=$(((currentcapacity/maxcapacity) * 100))
    echo $i
  }
Ron Shapiro's avatar
Ron Shapiro committed
23
24
25
26

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

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

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

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

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
elif [[ "$OSTYPE" = freebsd*  ]] ; then

  function battery_is_charging() {
    [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]]
  }

  function battery_pct() {
    if (( $+commands[sysctl] )) ; then
      echo "$(sysctl -n hw.acpi.battery.life)"
    fi
  }

  function battery_pct_remaining() {
    if [ ! $(battery_is_charging) ] ; then
      battery_pct
    else
      echo "External Power"
    fi
  }

  function battery_time_remaining() {
    remaining_time=$(sysctl -n hw.acpi.battery.time)
    if [[ $remaining_time -ge 0 ]] ; then
      # calculation from https://www.unix.com/shell-programming-and-scripting/23695-convert-minutes-hours-minutes-seconds.html
      ((hour=$remaining_time/60))
      ((minute=$remaining_time-$hour*60))
      echo $hour:$minute
    fi
  }

  function battery_pct_prompt() {
    b=$(battery_pct_remaining)
    if [ ! $(battery_is_charging) ] ; then
      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
  }

116
elif [[ "$OSTYPE" = linux*  ]] ; then
117

118
  function battery_is_charging() {
119
    ! [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]]
120
121
122
  }

  function battery_pct() {
123
    if (( $+commands[acpi] )) ; then
124
      echo "$(acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]')"
125
    fi
126
127
  }

128
  function battery_pct_remaining() {
129
130
131
132
    if [ ! $(battery_is_charging) ] ; then
      battery_pct
    else
      echo "External Power"
133
134
135
136
    fi
  }

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

  function battery_pct_prompt() {
    b=$(battery_pct_remaining) 
144
    if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
145
146
147
148
149
150
151
      if [ $b -gt 50 ] ; then
        color='green'
      elif [ $b -gt 20 ] ; then
        color='yellow'
      else
        color='red'
      fi
152
      echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}"
153
154
155
156
    else
      echo "∞"
    fi
  }
157

158
else
159
160
161
  # Empty functions so we don't cause errors in prompts
  function battery_pct_remaining() {
  }
162

163
164
  function battery_time_remaining() {
  }
165

166
167
  function battery_pct_prompt() {
  }
Peter Hoeg's avatar
Peter Hoeg committed
168
fi
169
170
171

function battery_level_gauge() {
  local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
172
173
  local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))};
  local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))};
174
175
176
177
178
179
180
181
182
183
184
185
  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);
186
  local filled empty gauge_color
187
188

  if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
189
190
    filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ));
    empty=$(( $gauge_slots - $filled ));
191

192
193
194
195
196
197
    if [[ $filled -gt $green_threshold ]]; then
      gauge_color=$color_green;
    elif [[ $filled -gt $yellow_threshold ]]; then
      gauge_color=$color_yellow;
    else
      gauge_color=$color_red;
198
199
    fi
  else
200
201
    filled=$gauge_slots;
    empty=0;
202
    filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
203
204
  fi

205
206
  local charging=' '
  battery_is_charging && charging=$charging_symbol;
207

208
  # Charging status and prefix
209
  printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
210
211
212
  # Filled slots
  [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  # Empty slots
213
  [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
214
  # Suffix
215
216
217
218
  printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
}