battery.plugin.zsh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 [[ $(uname) == "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 [[ $(uname) == "Linux" ]] ; then
  60. function battery_is_charging() {
  61. ! [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]]
  62. }
  63. function battery_pct() {
  64. echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')"
  65. }
  66. function battery_pct_remaining() {
  67. if [ ! $(battery_is_charging) ] ; then
  68. battery_pct
  69. else
  70. echo "External Power"
  71. fi
  72. }
  73. function battery_time_remaining() {
  74. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  75. echo $(acpi | cut -f3 -d ',')
  76. fi
  77. }
  78. function battery_pct_prompt() {
  79. b=$(battery_pct_remaining)
  80. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  81. if [ $b -gt 50 ] ; then
  82. color='green'
  83. elif [ $b -gt 20 ] ; then
  84. color='yellow'
  85. else
  86. color='red'
  87. fi
  88. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  89. else
  90. echo "∞"
  91. fi
  92. }
  93. else
  94. # Empty functions so we don't cause errors in prompts
  95. function battery_pct_remaining() {
  96. }
  97. function battery_time_remaining() {
  98. }
  99. function battery_pct_prompt() {
  100. }
  101. fi
  102. function battery_level_gauge() {
  103. local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
  104. local green_threshold=${BATTERY_GREEN_THRESHOLD:-6};
  105. local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-4};
  106. local color_green=${BATTERY_COLOR_GREEN:-%F{green}};
  107. local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}};
  108. local color_red=${BATTERY_COLOR_RED:-%F{red}};
  109. local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}};
  110. local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['};
  111. local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'};
  112. local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'};
  113. local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'};
  114. local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow};
  115. local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'};
  116. local battery_remaining_percentage=$(battery_pct);
  117. if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
  118. local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots)));
  119. local empty=$(($gauge_slots - $filled));
  120. if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green;
  121. elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow;
  122. else local gauge_color=$color_red;
  123. fi
  124. else
  125. local filled=$gauge_slots;
  126. local empty=0;
  127. filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
  128. fi
  129. local charging=' ' && battery_is_charging && charging=$charging_symbol;
  130. printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
  131. printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  132. [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
  133. printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
  134. }