battery.plugin.zsh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if [[ "$OSTYPE" = darwin* ]]; then
  14. function battery_is_charging() {
  15. ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
  16. }
  17. function battery_pct() {
  18. local battery_status="$(ioreg -rc AppleSmartBattery)"
  19. local -i capacity=$(sed -n -e '/MaxCapacity/s/^.*"MaxCapacity"\ =\ //p' <<< $battery_status)
  20. local -i current=$(sed -n -e '/CurrentCapacity/s/^.*"CurrentCapacity"\ =\ //p' <<< $battery_status)
  21. echo $(( current * 100 / capacity ))
  22. }
  23. function battery_pct_remaining() {
  24. if battery_is_charging; then
  25. echo "External Power"
  26. else
  27. battery_pct
  28. fi
  29. }
  30. function battery_time_remaining() {
  31. local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
  32. if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then
  33. timeremaining=$(echo $smart_battery_status | command grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
  34. if [ $timeremaining -gt 720 ]; then
  35. echo "::"
  36. else
  37. echo "~$((timeremaining / 60)):$((timeremaining % 60))"
  38. fi
  39. else
  40. echo "∞"
  41. fi
  42. }
  43. function battery_pct_prompt () {
  44. local battery_pct color
  45. if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then
  46. battery_pct=$(battery_pct_remaining)
  47. if [[ $battery_pct -gt 50 ]]; then
  48. color='green'
  49. elif [[ $battery_pct -gt 20 ]]; then
  50. color='yellow'
  51. else
  52. color='red'
  53. fi
  54. echo "%{$fg[$color]%}[${battery_pct}%%]%{$reset_color%}"
  55. else
  56. echo "∞"
  57. fi
  58. }
  59. elif [[ "$OSTYPE" = freebsd* ]]; then
  60. function battery_is_charging() {
  61. [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]]
  62. }
  63. function battery_pct() {
  64. if (( $+commands[sysctl] )); then
  65. sysctl -n hw.acpi.battery.life
  66. fi
  67. }
  68. function battery_pct_remaining() {
  69. if ! battery_is_charging; then
  70. battery_pct
  71. else
  72. echo "External Power"
  73. fi
  74. }
  75. function battery_time_remaining() {
  76. local remaining_time
  77. remaining_time=$(sysctl -n hw.acpi.battery.time)
  78. if [[ $remaining_time -ge 0 ]]; then
  79. ((hour = $remaining_time / 60 ))
  80. ((minute = $remaining_time % 60 ))
  81. printf %02d:%02d $hour $minute
  82. fi
  83. }
  84. function battery_pct_prompt() {
  85. local battery_pct color
  86. battery_pct=$(battery_pct_remaining)
  87. if battery_is_charging; then
  88. echo "∞"
  89. else
  90. if [[ $battery_pct -gt 50 ]]; then
  91. color='green'
  92. elif [[ $battery_pct -gt 20 ]]; then
  93. color='yellow'
  94. else
  95. color='red'
  96. fi
  97. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  98. fi
  99. }
  100. elif [[ "$OSTYPE" = linux* ]]; then
  101. function battery_is_charging() {
  102. ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging'
  103. }
  104. function battery_pct() {
  105. if (( $+commands[acpi] )); then
  106. acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -E '^Battery.*(Full|(Disc|C)harging)' | cut -f2 -d ',' | tr -cd '[:digit:]'
  107. fi
  108. }
  109. function battery_pct_remaining() {
  110. if ! battery_is_charging; then
  111. battery_pct
  112. else
  113. echo "External Power"
  114. fi
  115. }
  116. function battery_time_remaining() {
  117. if ! battery_is_charging; then
  118. acpi 2>/dev/null | command grep -v "rate information unavailable" | cut -f3 -d ','
  119. fi
  120. }
  121. function battery_pct_prompt() {
  122. local battery_pct color
  123. battery_pct=$(battery_pct_remaining)
  124. if battery_is_charging; then
  125. echo "∞"
  126. else
  127. if [[ $battery_pct -gt 50 ]]; then
  128. color='green'
  129. elif [[ $battery_pct -gt 20 ]]; then
  130. color='yellow'
  131. else
  132. color='red'
  133. fi
  134. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  135. fi
  136. }
  137. else
  138. # Empty functions so we don't cause errors in prompts
  139. function battery_is_charging { false }
  140. function battery_pct \
  141. battery_pct_remaining \
  142. battery_time_remaining \
  143. battery_pct_prompt { }
  144. fi
  145. function battery_level_gauge() {
  146. local gauge_slots=${BATTERY_GAUGE_SLOTS:-10}
  147. local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))}
  148. local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))}
  149. local color_green=${BATTERY_COLOR_GREEN:-%F{green}}
  150. local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}}
  151. local color_red=${BATTERY_COLOR_RED:-%F{red}}
  152. local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}}
  153. local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['}
  154. local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'}
  155. local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'}
  156. local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'}
  157. local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow}
  158. local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}
  159. local battery_remaining_percentage=$(battery_pct)
  160. local filled empty gauge_color
  161. if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
  162. filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ))
  163. empty=$(( $gauge_slots - $filled ))
  164. if [[ $filled -gt $green_threshold ]]; then
  165. gauge_color=$color_green
  166. elif [[ $filled -gt $yellow_threshold ]]; then
  167. gauge_color=$color_yellow
  168. else
  169. gauge_color=$color_red
  170. fi
  171. else
  172. filled=$gauge_slots
  173. empty=0
  174. filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'}
  175. fi
  176. local charging=' '
  177. battery_is_charging && charging=$charging_symbol
  178. # Charging status and prefix
  179. print -n ${charging_color}${charging}${color_reset}${battery_prefix}${gauge_color}
  180. # Filled slots
  181. [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  182. # Empty slots
  183. [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
  184. # Suffix
  185. print -n ${color_reset}${battery_suffix}${color_reset}
  186. }