battery.plugin.zsh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. # Author: Not Pua (im-notpua) #
  17. # Modified to add support for OpenBSD #
  18. ###########################################
  19. if [[ "$OSTYPE" = darwin* ]]; then
  20. function battery_is_charging() {
  21. ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
  22. }
  23. function battery_pct() {
  24. pmset -g batt | grep -Eo "\d+%" | cut -d% -f1
  25. }
  26. function battery_pct_remaining() {
  27. if battery_is_charging; then
  28. echo "External Power"
  29. else
  30. battery_pct
  31. fi
  32. }
  33. function battery_time_remaining() {
  34. local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
  35. if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then
  36. timeremaining=$(echo $smart_battery_status | command grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
  37. if [ $timeremaining -gt 720 ]; then
  38. echo "::"
  39. else
  40. echo "~$((timeremaining / 60)):$((timeremaining % 60))"
  41. fi
  42. else
  43. echo "∞"
  44. fi
  45. }
  46. function battery_pct_prompt () {
  47. local battery_pct color
  48. if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then
  49. battery_pct=$(battery_pct_remaining)
  50. if [[ $battery_pct -gt 50 ]]; then
  51. color='green'
  52. elif [[ $battery_pct -gt 20 ]]; then
  53. color='yellow'
  54. else
  55. color='red'
  56. fi
  57. echo "%{$fg[$color]%}[${battery_pct}%%]%{$reset_color%}"
  58. else
  59. echo "∞"
  60. fi
  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. 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. local remaining_time
  80. remaining_time=$(sysctl -n hw.acpi.battery.time)
  81. if [[ $remaining_time -ge 0 ]]; then
  82. ((hour = $remaining_time / 60 ))
  83. ((minute = $remaining_time % 60 ))
  84. printf %02d:%02d $hour $minute
  85. fi
  86. }
  87. function battery_pct_prompt() {
  88. local battery_pct color
  89. battery_pct=$(battery_pct_remaining)
  90. if battery_is_charging; then
  91. echo "∞"
  92. else
  93. if [[ $battery_pct -gt 50 ]]; then
  94. color='green'
  95. elif [[ $battery_pct -gt 20 ]]; then
  96. color='yellow'
  97. else
  98. color='red'
  99. fi
  100. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  101. fi
  102. }
  103. elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then
  104. function battery_is_charging() {
  105. termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }'
  106. }
  107. function battery_pct() {
  108. # Sample output:
  109. # {
  110. # "health": "GOOD",
  111. # "percentage": 93,
  112. # "plugged": "UNPLUGGED",
  113. # "status": "DISCHARGING",
  114. # "temperature": 29.0,
  115. # "current": 361816
  116. # }
  117. termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}'
  118. }
  119. function battery_pct_remaining() {
  120. if ! battery_is_charging; then
  121. battery_pct
  122. else
  123. echo "External Power"
  124. fi
  125. }
  126. function battery_time_remaining() { } # Not available on android
  127. function battery_pct_prompt() {
  128. local battery_pct color
  129. battery_pct=$(battery_pct_remaining)
  130. if battery_is_charging; then
  131. echo "∞"
  132. else
  133. if [[ $battery_pct -gt 50 ]]; then
  134. color='green'
  135. elif [[ $battery_pct -gt 20 ]]; then
  136. color='yellow'
  137. else
  138. color='red'
  139. fi
  140. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  141. fi
  142. }
  143. elif [[ "$OSTYPE" = openbsd* ]]; then
  144. function battery_is_charging() {
  145. [[ $(apm -b) -eq 3 ]]
  146. }
  147. function battery_pct() {
  148. apm -l
  149. }
  150. function battery_pct_remaining() {
  151. if ! battery_is_charging; then
  152. battery_pct
  153. else
  154. echo "External Power"
  155. fi
  156. }
  157. function battery_time_remaining() {
  158. local remaining_time
  159. remaining_time=$(apm -m)
  160. if [[ $remaining_time -ge 0 ]]; then
  161. ((hour = $remaining_time / 60 ))
  162. ((minute = $remaining_time % 60 ))
  163. printf %02d:%02d $hour $minute
  164. fi
  165. }
  166. function battery_pct_prompt() {
  167. local battery_pct color
  168. battery_pct=$(battery_pct_remaining)
  169. if battery_is_charging; then
  170. echo "∞"
  171. else
  172. if [[ $battery_pct -gt 50 ]]; then
  173. color='green'
  174. elif [[ $battery_pct -gt 20 ]]; then
  175. color='yellow'
  176. else
  177. color='red'
  178. fi
  179. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  180. fi
  181. }
  182. elif [[ "$OSTYPE" = linux* ]]; then
  183. function battery_is_charging() {
  184. if (( $+commands[acpitool] )); then
  185. ! acpitool 2>/dev/null | command grep -qE '^\s+Battery.*Discharging'
  186. elif (( $+commands[acpi] )); then
  187. ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging'
  188. fi
  189. }
  190. function battery_pct() {
  191. if (( $+commands[acpitool] )); then
  192. # Sample output:
  193. # Battery #1 : Unknown, 99.55%
  194. # Battery #2 : Discharging, 49.58%, 01:12:05
  195. # All batteries : 62.60%, 02:03:03
  196. local -i pct=$(acpitool 2>/dev/null | command awk -F, '
  197. /^\s+All batteries/ {
  198. gsub(/[^0-9.]/, "", $1)
  199. pct=$1
  200. exit
  201. }
  202. !pct && /^\s+Battery/ {
  203. gsub(/[^0-9.]/, "", $2)
  204. pct=$2
  205. }
  206. END { print pct }
  207. ')
  208. echo $pct
  209. elif (( $+commands[acpi] )); then
  210. # Sample output:
  211. # Battery 0: Discharging, 0%, rate information unavailable
  212. # Battery 1: Full, 100%
  213. acpi 2>/dev/null | command awk -F, '
  214. /rate information unavailable/ { next }
  215. /^Battery.*: /{ gsub(/[^0-9]/, "", $2); print $2; exit }
  216. '
  217. fi
  218. }
  219. function battery_pct_remaining() {
  220. if ! battery_is_charging; then
  221. battery_pct
  222. else
  223. echo "External Power"
  224. fi
  225. }
  226. function battery_time_remaining() {
  227. if ! battery_is_charging; then
  228. acpi 2>/dev/null | command grep -v "rate information unavailable" | cut -f3 -d ','
  229. fi
  230. }
  231. function battery_pct_prompt() {
  232. local battery_pct color
  233. battery_pct=$(battery_pct_remaining)
  234. if battery_is_charging; then
  235. echo "∞"
  236. else
  237. if [[ $battery_pct -gt 50 ]]; then
  238. color='green'
  239. elif [[ $battery_pct -gt 20 ]]; then
  240. color='yellow'
  241. else
  242. color='red'
  243. fi
  244. echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
  245. fi
  246. }
  247. else
  248. # Empty functions so we don't cause errors in prompts
  249. function battery_is_charging { false }
  250. function battery_pct \
  251. battery_pct_remaining \
  252. battery_time_remaining \
  253. battery_pct_prompt { }
  254. fi
  255. function battery_level_gauge() {
  256. local gauge_slots=${BATTERY_GAUGE_SLOTS:-10}
  257. local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))}
  258. local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))}
  259. local color_green=${BATTERY_COLOR_GREEN:-%F{green}}
  260. local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}}
  261. local color_red=${BATTERY_COLOR_RED:-%F{red}}
  262. local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}}
  263. local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['}
  264. local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'}
  265. local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'}
  266. local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'}
  267. local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow}
  268. local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}
  269. local -i battery_remaining_percentage=$(battery_pct)
  270. local filled empty gauge_color
  271. if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
  272. filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ))
  273. empty=$(( $gauge_slots - $filled ))
  274. if [[ $filled -gt $green_threshold ]]; then
  275. gauge_color=$color_green
  276. elif [[ $filled -gt $yellow_threshold ]]; then
  277. gauge_color=$color_yellow
  278. else
  279. gauge_color=$color_red
  280. fi
  281. else
  282. filled=$gauge_slots
  283. empty=0
  284. filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'}
  285. fi
  286. local charging=' '
  287. battery_is_charging && charging=$charging_symbol
  288. # Charging status and prefix
  289. print -n ${charging_color}${charging}${color_reset}${battery_prefix}${gauge_color}
  290. # Filled slots
  291. [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  292. # Empty slots
  293. [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
  294. # Suffix
  295. print -n ${color_reset}${battery_suffix}${color_reset}
  296. }