battery.plugin.zsh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. echo "~$((timeremaining / 60)):$((timeremaining % 60))"
  33. else
  34. echo "∞"
  35. fi
  36. }
  37. function battery_pct_prompt () {
  38. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  39. b=$(battery_pct_remaining)
  40. if [ $b -gt 50 ] ; then
  41. color='green'
  42. elif [ $b -gt 20 ] ; then
  43. color='yellow'
  44. else
  45. color='red'
  46. fi
  47. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  48. else
  49. echo "∞"
  50. fi
  51. }
  52. function battery_is_charging() {
  53. [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]
  54. }
  55. elif [[ $(uname) == "Linux" ]] ; then
  56. function battery_pct_remaining() {
  57. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  58. echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')"
  59. fi
  60. }
  61. function battery_time_remaining() {
  62. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  63. echo $(acpi | cut -f3 -d ',')
  64. fi
  65. }
  66. function battery_pct_prompt() {
  67. b=$(battery_pct_remaining)
  68. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  69. if [ $b -gt 50 ] ; then
  70. color='green'
  71. elif [ $b -gt 20 ] ; then
  72. color='yellow'
  73. else
  74. color='red'
  75. fi
  76. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  77. else
  78. echo "∞"
  79. fi
  80. }
  81. function battery_pct() {
  82. # todo for on linux
  83. }
  84. function battery_is_charging() {
  85. # todo on linux
  86. false
  87. }
  88. else
  89. # Empty functions so we don't cause errors in prompts
  90. function battery_pct_remaining() {
  91. }
  92. function battery_time_remaining() {
  93. }
  94. function battery_pct_prompt() {
  95. }
  96. fi
  97. function battery_level_gauge() {
  98. local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
  99. local green_threshold=${BATTERY_GREEN_THRESHOLD:-6};
  100. local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-4};
  101. local color_green=${BATTERY_COLOR_GREEN:-%F{green}};
  102. local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}};
  103. local color_red=${BATTERY_COLOR_RED:-%F{red}};
  104. local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}};
  105. local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['};
  106. local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'};
  107. local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'};
  108. local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'};
  109. local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow};
  110. local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'};
  111. local battery_remaining_percentage=$(battery_pct);
  112. if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
  113. local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots)));
  114. local empty=$(($gauge_slots - $filled));
  115. if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green;
  116. elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow;
  117. else local gauge_color=$color_red;
  118. fi
  119. else
  120. local filled=$gauge_slots;
  121. local empty=0;
  122. filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
  123. fi
  124. local charging=' ' && battery_is_charging && charging=$charging_symbol;
  125. printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
  126. printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  127. [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
  128. printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
  129. }