battery.plugin.zsh 6.3 KB

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