battery.plugin.zsh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if [[ "$OSTYPE" = darwin* ]] ; then
  11. function battery_pct() {
  12. local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
  13. typeset -F maxcapacity=$(echo $smart_battery_status | grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
  14. typeset -F currentcapacity=$(echo $smart_battery_status | grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
  15. integer i=$(((currentcapacity/maxcapacity) * 100))
  16. echo $i
  17. }
  18. function plugged_in() {
  19. [ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ Yes') -eq 1 ]
  20. }
  21. function battery_pct_remaining() {
  22. if plugged_in ; then
  23. echo "External Power"
  24. else
  25. battery_pct
  26. fi
  27. }
  28. function battery_time_remaining() {
  29. local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
  30. if [[ $(echo $smart_battery_status | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  31. timeremaining=$(echo $smart_battery_status | grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
  32. if [ $timeremaining -gt 720 ] ; then
  33. echo "::"
  34. else
  35. echo "~$((timeremaining / 60)):$((timeremaining % 60))"
  36. fi
  37. else
  38. echo "∞"
  39. fi
  40. }
  41. function battery_pct_prompt () {
  42. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  43. b=$(battery_pct_remaining)
  44. if [ $b -gt 50 ] ; then
  45. color='green'
  46. elif [ $b -gt 20 ] ; then
  47. color='yellow'
  48. else
  49. color='red'
  50. fi
  51. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  52. else
  53. echo "∞"
  54. fi
  55. }
  56. function battery_is_charging() {
  57. [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]
  58. }
  59. elif [[ "$OSTYPE" = linux* ]] ; then
  60. function battery_is_charging() {
  61. ! [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]]
  62. }
  63. function battery_pct() {
  64. if (( $+commands[acpi] )) ; then
  65. echo "$(acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]')"
  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. if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  77. echo $(acpi 2>/dev/null | cut -f3 -d ',')
  78. fi
  79. }
  80. function battery_pct_prompt() {
  81. b=$(battery_pct_remaining)
  82. if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  83. if [ $b -gt 50 ] ; then
  84. color='green'
  85. elif [ $b -gt 20 ] ; then
  86. color='yellow'
  87. else
  88. color='red'
  89. fi
  90. echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}"
  91. else
  92. echo "∞"
  93. fi
  94. }
  95. else
  96. # Empty functions so we don't cause errors in prompts
  97. function battery_pct_remaining() {
  98. }
  99. function battery_time_remaining() {
  100. }
  101. function battery_pct_prompt() {
  102. }
  103. fi
  104. function battery_level_gauge() {
  105. local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
  106. local green_threshold=${BATTERY_GREEN_THRESHOLD:-6};
  107. local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-4};
  108. local color_green=${BATTERY_COLOR_GREEN:-%F{green}};
  109. local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}};
  110. local color_red=${BATTERY_COLOR_RED:-%F{red}};
  111. local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}};
  112. local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['};
  113. local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'};
  114. local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'};
  115. local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'};
  116. local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow};
  117. local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'};
  118. local battery_remaining_percentage=$(battery_pct);
  119. if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
  120. local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots)));
  121. local empty=$(($gauge_slots - $filled));
  122. if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green;
  123. elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow;
  124. else local gauge_color=$color_red;
  125. fi
  126. else
  127. local filled=$gauge_slots;
  128. local empty=0;
  129. filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
  130. fi
  131. local charging=' ' && battery_is_charging && charging=$charging_symbol;
  132. printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
  133. printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  134. [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
  135. printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
  136. }