battery.plugin.zsh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. # Author: Avneet Singh (kalsi-avneet) #
  14. # Modified to add support for Android #
  15. ###########################################
  16. if [[ "$OSTYPE" = darwin* ]]; then
  17. function battery_is_charging() {
  18. ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
  19. }
  20. function battery_pct() {
  21. pmset -g batt | grep -Eo "\d+%" | cut -d% -f1
  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-android ]] && (( ${+commands[termux-battery-status]} )); then
  101. function battery_is_charging() {
  102. termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }'
  103. }
  104. function battery_pct() {
  105. # Sample output:
  106. # {
  107. # "health": "GOOD",
  108. # "percentage": 93,
  109. # "plugged": "UNPLUGGED",
  110. # "status": "DISCHARGING",
  111. # "temperature": 29.0,
  112. # "current": 361816
  113. # }
  114. termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}'
  115. }
  116. function battery_pct_remaining() {
  117. if ! battery_is_charging; then
  118. battery_pct
  119. else
  120. echo "External Power"
  121. fi
  122. }
  123. function battery_time_remaining() { } # Not available on android
  124. function battery_pct_prompt() {
  125. local battery_pct color
  126. battery_pct=$(battery_pct_remaining)
  127. if battery_is_charging; then
  128. echo "∞"
  129. else
  130. if [[ $battery_pct -gt 50 ]]; then
  131. color='green'
  132. elif [[ $battery_pct -gt 20 ]]; then
  133. color='yellow'
  134. else
  135. color='red'
  136. fi
  137. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  138. fi
  139. }
  140. elif [[ "$OSTYPE" = linux* ]]; then
  141. function battery_is_charging() {
  142. if (( $+commands[acpitool] )); then
  143. ! acpitool 2>/dev/null | command grep -qE '^\s+Battery.*Discharging'
  144. elif (( $+commands[acpi] )); then
  145. ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging'
  146. fi
  147. }
  148. function battery_pct() {
  149. if (( $+commands[acpitool] )); then
  150. # Sample output:
  151. # Battery #1 : Unknown, 99.55%
  152. # Battery #2 : Discharging, 49.58%, 01:12:05
  153. # All batteries : 62.60%, 02:03:03
  154. local -i pct=$(acpitool 2>/dev/null | command awk -F, '
  155. /^\s+All batteries/ {
  156. gsub(/[^0-9.]/, "", $1)
  157. pct=$1
  158. exit
  159. }
  160. !pct && /^\s+Battery/ {
  161. gsub(/[^0-9.]/, "", $2)
  162. pct=$2
  163. }
  164. END { print pct }
  165. ')
  166. echo $pct
  167. elif (( $+commands[acpi] )); then
  168. # Sample output:
  169. # Battery 0: Discharging, 0%, rate information unavailable
  170. # Battery 1: Full, 100%
  171. acpi 2>/dev/null | command awk -F, '
  172. /rate information unavailable/ { next }
  173. /^Battery.*: /{ gsub(/[^0-9]/, "", $2); print $2; exit }
  174. '
  175. fi
  176. }
  177. function battery_pct_remaining() {
  178. if ! battery_is_charging; then
  179. battery_pct
  180. else
  181. echo "External Power"
  182. fi
  183. }
  184. function battery_time_remaining() {
  185. if ! battery_is_charging; then
  186. acpi 2>/dev/null | command grep -v "rate information unavailable" | cut -f3 -d ','
  187. fi
  188. }
  189. function battery_pct_prompt() {
  190. local battery_pct color
  191. battery_pct=$(battery_pct_remaining)
  192. if battery_is_charging; then
  193. echo "∞"
  194. else
  195. if [[ $battery_pct -gt 50 ]]; then
  196. color='green'
  197. elif [[ $battery_pct -gt 20 ]]; then
  198. color='yellow'
  199. else
  200. color='red'
  201. fi
  202. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  203. fi
  204. }
  205. else
  206. # Empty functions so we don't cause errors in prompts
  207. function battery_is_charging { false }
  208. function battery_pct \
  209. battery_pct_remaining \
  210. battery_time_remaining \
  211. battery_pct_prompt { }
  212. fi
  213. function battery_level_gauge() {
  214. local gauge_slots=${BATTERY_GAUGE_SLOTS:-10}
  215. local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))}
  216. local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))}
  217. local color_green=${BATTERY_COLOR_GREEN:-%F{green}}
  218. local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}}
  219. local color_red=${BATTERY_COLOR_RED:-%F{red}}
  220. local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}}
  221. local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['}
  222. local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'}
  223. local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'}
  224. local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'}
  225. local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow}
  226. local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}
  227. local -i battery_remaining_percentage=$(battery_pct)
  228. local filled empty gauge_color
  229. if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
  230. filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ))
  231. empty=$(( $gauge_slots - $filled ))
  232. if [[ $filled -gt $green_threshold ]]; then
  233. gauge_color=$color_green
  234. elif [[ $filled -gt $yellow_threshold ]]; then
  235. gauge_color=$color_yellow
  236. else
  237. gauge_color=$color_red
  238. fi
  239. else
  240. filled=$gauge_slots
  241. empty=0
  242. filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'}
  243. fi
  244. local charging=' '
  245. battery_is_charging && charging=$charging_symbol
  246. # Charging status and prefix
  247. print -n ${charging_color}${charging}${color_reset}${battery_prefix}${gauge_color}
  248. # Filled slots
  249. [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  250. # Empty slots
  251. [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
  252. # Suffix
  253. print -n ${color_reset}${battery_suffix}${color_reset}
  254. }