battery.plugin.zsh
9609 bytes
1###########################################
2# Battery plugin for oh-my-zsh #
3# Original Author: Peter hoeg (peterhoeg) #
4# Email: peter@speartail.com #
5###########################################
6# Author: Sean Jones (neuralsandwich) #
7# Email: neuralsandwich@gmail.com #
8# Modified to add support for Apple Mac #
9###########################################
10# Author: J (927589452) #
11# Modified to add support for FreeBSD #
12###########################################
13# Author: Avneet Singh (kalsi-avneet) #
14# Modified to add support for Android #
15###########################################
16# Author: Not Pua (im-notpua) #
17# Modified to add support for OpenBSD #
18###########################################
19
20: ${BATTERY_SHOW_WATTS:=false}
21
22
23if [[ "$OSTYPE" = darwin* ]]; then
24 function get_charger_power() {
25 echo "$(ioreg -rc AppleSmartBattery | grep -o '"Watts"=[0-9]\+' | head -1 | grep -o '[0-9]\+')W "
26 }
27 function battery_is_charging() {
28 ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
29 }
30 function battery_pct() {
31 pmset -g batt | grep -Eo "\d+%" | cut -d% -f1
32 }
33 function battery_pct_remaining() {
34 if battery_is_charging; then
35 echo "External Power"
36 else
37 battery_pct
38 fi
39 }
40 function battery_time_remaining() {
41 local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
42 if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then
43 timeremaining=$(echo $smart_battery_status | command grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
44 if [ $timeremaining -gt 720 ]; then
45 echo "::"
46 else
47 echo "~$((timeremaining / 60)):$((timeremaining % 60))"
48 fi
49 else
50 echo "∞"
51 fi
52 }
53 function battery_pct_prompt () {
54 local battery_pct color
55 if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then
56 battery_pct=$(battery_pct_remaining)
57 if [[ $battery_pct -gt 50 ]]; then
58 color='green'
59 elif [[ $battery_pct -gt 20 ]]; then
60 color='yellow'
61 else
62 color='red'
63 fi
64 echo "%{$fg[$color]%}[${battery_pct}%%]%{$reset_color%}"
65 else
66 if [[ "${BATTERY_SHOW_WATTS}" = "true" ]] ; then
67 watts=$(get_charger_power)
68 fi
69 echo "${watts}${BATTERY_CHARGING-⚡️}"
70 fi
71 }
72
73elif [[ "$OSTYPE" = freebsd* ]]; then
74 function battery_is_charging() {
75 [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]]
76 }
77 function battery_pct() {
78 if (( $+commands[sysctl] )); then
79 sysctl -n hw.acpi.battery.life
80 fi
81 }
82 function battery_pct_remaining() {
83 if ! battery_is_charging; then
84 battery_pct
85 else
86 echo "External Power"
87 fi
88 }
89 function battery_time_remaining() {
90 local remaining_time
91 remaining_time=$(sysctl -n hw.acpi.battery.time)
92 if [[ $remaining_time -ge 0 ]]; then
93 ((hour = $remaining_time / 60 ))
94 ((minute = $remaining_time % 60 ))
95 printf %02d:%02d $hour $minute
96 fi
97 }
98 function battery_pct_prompt() {
99 local battery_pct color
100 battery_pct=$(battery_pct_remaining)
101 if battery_is_charging; then
102 echo "∞"
103 else
104 if [[ $battery_pct -gt 50 ]]; then
105 color='green'
106 elif [[ $battery_pct -gt 20 ]]; then
107 color='yellow'
108 else
109 color='red'
110 fi
111 echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
112 fi
113 }
114elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then
115 function battery_is_charging() {
116 termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }'
117 }
118 function battery_pct() {
119 # Sample output:
120 # {
121 # "health": "GOOD",
122 # "percentage": 93,
123 # "plugged": "UNPLUGGED",
124 # "status": "DISCHARGING",
125 # "temperature": 29.0,
126 # "current": 361816
127 # }
128 termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}'
129 }
130 function battery_pct_remaining() {
131 if ! battery_is_charging; then
132 battery_pct
133 else
134 echo "External Power"
135 fi
136 }
137 function battery_time_remaining() { } # Not available on android
138 function battery_pct_prompt() {
139 local battery_pct color
140 battery_pct=$(battery_pct_remaining)
141 if battery_is_charging; then
142 echo "∞"
143 else
144 if [[ $battery_pct -gt 50 ]]; then
145 color='green'
146 elif [[ $battery_pct -gt 20 ]]; then
147 color='yellow'
148 else
149 color='red'
150 fi
151 echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
152 fi
153 }
154elif [[ "$OSTYPE" = openbsd* ]]; then
155 function battery_is_charging() {
156 [[ $(apm -b) -eq 3 ]]
157 }
158 function battery_pct() {
159 apm -l
160 }
161 function battery_pct_remaining() {
162 if ! battery_is_charging; then
163 battery_pct
164 else
165 echo "External Power"
166 fi
167 }
168 function battery_time_remaining() {
169 local remaining_time
170 remaining_time=$(apm -m)
171 if [[ $remaining_time -ge 0 ]]; then
172 ((hour = $remaining_time / 60 ))
173 ((minute = $remaining_time % 60 ))
174 printf %02d:%02d $hour $minute
175 fi
176 }
177 function battery_pct_prompt() {
178 local battery_pct color
179 battery_pct=$(battery_pct_remaining)
180 if battery_is_charging; then
181 echo "∞"
182 else
183 if [[ $battery_pct -gt 50 ]]; then
184 color='green'
185 elif [[ $battery_pct -gt 20 ]]; then
186 color='yellow'
187 else
188 color='red'
189 fi
190 echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
191 fi
192 }
193
194elif [[ "$OSTYPE" = linux* ]]; then
195 function battery_is_charging() {
196 if (( $+commands[acpitool] )); then
197 ! acpitool 2>/dev/null | command grep -qE '^\s+Battery.*Discharging'
198 elif (( $+commands[acpi] )); then
199 ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging'
200 fi
201 }
202 function battery_pct() {
203 if (( $+commands[acpitool] )); then
204 # Sample output:
205 # Battery #1 : Unknown, 99.55%
206 # Battery #2 : Discharging, 49.58%, 01:12:05
207 # All batteries : 62.60%, 02:03:03
208 local -i pct=$(acpitool 2>/dev/null | command awk -F, '
209 /^\s+All batteries/ {
210 gsub(/[^0-9.]/, "", $1)
211 pct=$1
212 exit
213 }
214 !pct && /^\s+Battery/ {
215 gsub(/[^0-9.]/, "", $2)
216 pct=$2
217 }
218 END { print pct }
219 ')
220 echo $pct
221 elif (( $+commands[acpi] )); then
222 # Sample output:
223 # Battery 0: Discharging, 0%, rate information unavailable
224 # Battery 1: Full, 100%
225 acpi 2>/dev/null | command awk -F, '
226 /rate information unavailable/ { next }
227 /^Battery.*: /{ gsub(/[^0-9]/, "", $2); print $2; exit }
228 '
229 fi
230 }
231 function battery_pct_remaining() {
232 if ! battery_is_charging; then
233 battery_pct
234 else
235 echo "External Power"
236 fi
237 }
238 function battery_time_remaining() {
239 if ! battery_is_charging; then
240 acpi 2>/dev/null | command grep -v "rate information unavailable" | cut -f3 -d ','
241 fi
242 }
243 function battery_pct_prompt() {
244 local battery_pct color
245 battery_pct=$(battery_pct_remaining)
246 if battery_is_charging; then
247 echo "∞"
248 else
249 if [[ $battery_pct -gt 50 ]]; then
250 color='green'
251 elif [[ $battery_pct -gt 20 ]]; then
252 color='yellow'
253 else
254 color='red'
255 fi
256 echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
257 fi
258 }
259else
260 # Empty functions so we don't cause errors in prompts
261 function battery_is_charging { false }
262 function battery_pct \
263 battery_pct_remaining \
264 battery_time_remaining \
265 battery_pct_prompt { }
266fi
267
268function battery_level_gauge() {
269 local gauge_slots=${BATTERY_GAUGE_SLOTS:-10}
270 local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))}
271 local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))}
272 local color_green=${BATTERY_COLOR_GREEN:-%F{green}}
273 local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}}
274 local color_red=${BATTERY_COLOR_RED:-%F{red}}
275 local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}}
276 local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['}
277 local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'}
278 local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'}
279 local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'}
280 local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow}
281 local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}
282
283 local -i battery_remaining_percentage=$(battery_pct)
284 local filled empty gauge_color
285
286 if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
287 filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ))
288 empty=$(( $gauge_slots - $filled ))
289
290 if [[ $filled -gt $green_threshold ]]; then
291 gauge_color=$color_green
292 elif [[ $filled -gt $yellow_threshold ]]; then
293 gauge_color=$color_yellow
294 else
295 gauge_color=$color_red
296 fi
297 else
298 filled=$gauge_slots
299 empty=0
300 filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'}
301 fi
302
303 local charging=' '
304 battery_is_charging && charging=$charging_symbol
305
306 # Charging status and prefix
307 print -n ${charging_color}${charging}${color_reset}${battery_prefix}${gauge_color}
308 # Filled slots
309 [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
310 # Empty slots
311 [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
312 # Suffix
313 print -n ${color_reset}${battery_suffix}${color_reset}
314}