battery.plugin.zsh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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) -eq "Darwin" ]] ; then
  11. function battery_pct_remaining() {
  12. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  13. typeset -F maxcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
  14. typeset -F currentcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
  15. integer i=$(((currentcapacity/maxcapacity) * 100))
  16. echo $i
  17. else
  18. echo "External Power"
  19. fi
  20. }
  21. function battery_time_remaining() {
  22. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  23. timeremaining=$(ioreg -rc "AppleSmartBattery"| grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
  24. echo "~$((timeremaining / 60)):$((timeremaining % 60))"
  25. else
  26. echo "∞"
  27. fi
  28. }
  29. function battery_pct_prompt () {
  30. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  31. b=$(battery_pct_remaining)
  32. if [ $b -gt 50 ] ; then
  33. color='green'
  34. elif [ $b -gt 20 ] ; then
  35. color='yellow'
  36. else
  37. color='red'
  38. fi
  39. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  40. else
  41. echo ""
  42. fi
  43. }
  44. elif [[ $(uname) -eq "Linux" ]] ; then
  45. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  46. function battery_pct_remaining() { echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')" }
  47. function battery_time_remaining() { echo $(acpi | cut -f3 -d ',') }
  48. function battery_pct_prompt() {
  49. b=$(battery_pct_remaining)
  50. if [ $b -gt 50 ] ; then
  51. color='green'
  52. elif [ $b -gt 20 ] ; then
  53. color='yellow'
  54. else
  55. color='red'
  56. fi
  57. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  58. }
  59. else
  60. error_msg='no battery'
  61. function battery_pct_remaining() { echo $error_msg }
  62. function battery_time_remaining() { echo $error_msg }
  63. function battery_pct_prompt() { echo '' }
  64. fi
  65. fi