battery.plugin.zsh 6.0 KB

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