battery.plugin.zsh 4.8 KB

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