battery.plugin.zsh 6.3 KB

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