| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  | ########################################### | 
					
						
							|  |  |  | # 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   # | 
					
						
							|  |  |  | ########################################### | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  | # Author: J (927589452)                   # | 
					
						
							|  |  |  | # Modified to add support for FreeBSD     # | 
					
						
							|  |  |  | ########################################### | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  | if [[ "$OSTYPE" = darwin* ]]; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |   function battery_is_charging() { | 
					
						
							|  |  |  |     ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes' | 
					
						
							| 
									
										
										
										
											2013-04-20 22:37:36 -04:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-04-29 10:18:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |   function battery_pct() { | 
					
						
							|  |  |  |     local smart_battery_status="$(ioreg -rc AppleSmartBattery)" | 
					
						
							|  |  |  |     local -F maxcapacity=$(command grep '^.*"MaxCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*"MaxCapacity"\ =\ //') | 
					
						
							|  |  |  |     local -F currentcapacity=$(command grep '^.*"CurrentCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*CurrentCapacity"\ =\ //') | 
					
						
							|  |  |  |     echo $(( (currentcapacity/maxcapacity) * 100 )) | 
					
						
							| 
									
										
										
										
											2013-04-29 10:18:32 -04:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-17 12:01:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |   function battery_pct_remaining() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if battery_is_charging; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |       echo "External Power" | 
					
						
							| 
									
										
										
										
											2013-04-29 10:18:32 -04:00
										 |  |  |     else | 
					
						
							|  |  |  |       battery_pct | 
					
						
							| 
									
										
										
										
											2011-10-20 14:12:13 +08:00
										 |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function battery_time_remaining() { | 
					
						
							| 
									
										
										
										
											2014-03-17 12:01:00 -04:00
										 |  |  |     local smart_battery_status="$(ioreg -rc "AppleSmartBattery")" | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then | 
					
						
							|  |  |  |       timeremaining=$(echo $smart_battery_status | command grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //') | 
					
						
							|  |  |  |       if [ $timeremaining -gt 720 ]; then | 
					
						
							| 
									
										
										
										
											2014-02-17 13:32:24 +13:00
										 |  |  |         echo "::" | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         echo "~$((timeremaining / 60)):$((timeremaining % 60))" | 
					
						
							|  |  |  |       fi | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       echo "∞" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_pct_prompt () { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |       b=$(battery_pct_remaining) | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |       if [[ $b -gt 50 ]]; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |         color='green' | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |       elif [[ $b -gt 20 ]]; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |         color='yellow' | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         color='red' | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |       echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}" | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2013-03-02 11:09:57 +00:00
										 |  |  |       echo "∞" | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-17 12:01:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  | elif [[ "$OSTYPE" = freebsd* ]]; then | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function battery_is_charging() { | 
					
						
							|  |  |  |     [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]] | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_pct() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if (( $+commands[sysctl] )); then | 
					
						
							|  |  |  |       sysctl -n hw.acpi.battery.life | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_pct_remaining() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if ! battery_is_charging; then | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  |       battery_pct | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       echo "External Power" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_time_remaining() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     local remaining_time | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  |     remaining_time=$(sysctl -n hw.acpi.battery.time) | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if [[ $remaining_time -ge 0 ]]; then | 
					
						
							|  |  |  |       ((hour = $remaining_time / 60 )) | 
					
						
							|  |  |  |       ((minute = $remaining_time % 60 )) | 
					
						
							|  |  |  |       printf %02d:%02d $hour $minute | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_pct_prompt() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     local b color | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  |     b=$(battery_pct_remaining) | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if battery_is_charging; then | 
					
						
							|  |  |  |       echo "∞" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       if [[ $b -gt 50 ]]; then | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  |         color='green' | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |       elif [[ $b -gt 20 ]]; then | 
					
						
							| 
									
										
										
										
											2019-10-09 19:24:44 +02:00
										 |  |  |         color='yellow' | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         color='red' | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |       echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  | elif [[ "$OSTYPE" = linux*  ]]; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 14:47:35 -03:00
										 |  |  |   function battery_is_charging() { | 
					
						
							| 
									
										
										
										
											2020-02-11 13:49:04 +01:00
										 |  |  |     ! acpi 2>/dev/null | command grep -q '^Battery.*Discharging' | 
					
						
							| 
									
										
										
										
											2014-03-02 14:47:35 -03:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_pct() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     if (( $+commands[acpi] )); then | 
					
						
							| 
									
										
										
										
											2020-02-11 13:49:04 +01:00
										 |  |  |       acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]' | 
					
						
							| 
									
										
										
										
											2014-03-17 12:01:00 -04:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2014-03-02 14:47:35 -03:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-02 11:09:57 +00:00
										 |  |  |   function battery_pct_remaining() { | 
					
						
							| 
									
										
										
										
											2020-02-11 13:49:04 +01:00
										 |  |  |     if ! battery_is_charging; then | 
					
						
							| 
									
										
										
										
											2014-03-02 14:47:35 -03:00
										 |  |  |       battery_pct | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       echo "External Power" | 
					
						
							| 
									
										
										
										
											2013-03-02 11:09:57 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_time_remaining() { | 
					
						
							| 
									
										
										
										
											2020-02-11 13:49:04 +01:00
										 |  |  |     if ! battery_is_charging; then | 
					
						
							|  |  |  |       acpi 2>/dev/null | cut -f3 -d ',' | 
					
						
							| 
									
										
										
										
											2013-03-02 11:09:57 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function battery_pct_prompt() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     local b color | 
					
						
							|  |  |  |     b=$(battery_pct_remaining) | 
					
						
							| 
									
										
										
										
											2020-02-11 13:49:04 +01:00
										 |  |  |     if battery_is_charging; then | 
					
						
							|  |  |  |       echo "∞" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       if [[ $b -gt 50 ]]; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |         color='green' | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |       elif [[ $b -gt 20 ]]; then | 
					
						
							| 
									
										
										
										
											2012-12-02 19:36:33 +00:00
										 |  |  |         color='yellow' | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         color='red' | 
					
						
							|  |  |  |       fi | 
					
						
							| 
									
										
										
										
											2017-02-23 08:50:49 +01:00
										 |  |  |       echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" | 
					
						
							| 
									
										
										
										
											2013-03-02 11:09:57 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-17 12:01:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 21:00:11 -05:00
										 |  |  | else | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  |   # Empty functions so we don't cause errors in prompts | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |   function battery_is_charging { false } | 
					
						
							|  |  |  |   function battery_pct \
 | 
					
						
							|  |  |  |     battery_pct_remaining \
 | 
					
						
							|  |  |  |     battery_time_remaining \
 | 
					
						
							|  |  |  |     battery_pct_prompt { } | 
					
						
							| 
									
										
										
										
											2011-10-20 14:12:13 +08:00
										 |  |  | fi | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function battery_level_gauge() { | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |   local gauge_slots=${BATTERY_GAUGE_SLOTS:-10} | 
					
						
							|  |  |  |   local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))} | 
					
						
							|  |  |  |   local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.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) | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |   local filled empty gauge_color | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 )) | 
					
						
							|  |  |  |     empty=$(( $gauge_slots - $filled )) | 
					
						
							| 
									
										
										
										
											2014-03-17 12:01:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |     if [[ $filled -gt $green_threshold ]]; then | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |       gauge_color=$color_green | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |     elif [[ $filled -gt $yellow_threshold ]]; then | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |       gauge_color=$color_yellow | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |       gauge_color=$color_red | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  |     fi | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |     filled=$gauge_slots | 
					
						
							|  |  |  |     empty=0 | 
					
						
							|  |  |  |     filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'} | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |   local charging=' ' | 
					
						
							| 
									
										
										
										
											2020-02-11 14:17:46 +01:00
										 |  |  |   battery_is_charging && charging=$charging_symbol | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |   # Charging status and prefix | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  |   printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%} | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |   # Filled slots | 
					
						
							|  |  |  |   [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled} | 
					
						
							|  |  |  |   # Empty slots | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  |   [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty} | 
					
						
							| 
									
										
										
										
											2020-02-11 13:32:13 +01:00
										 |  |  |   # Suffix | 
					
						
							| 
									
										
										
										
											2014-01-06 02:09:15 +01:00
										 |  |  |   printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%} | 
					
						
							|  |  |  | } |