battery.plugin.zsh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. typeset -F maxcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
  13. typeset -F currentcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
  14. integer i=$(((currentcapacity/maxcapacity) * 100))
  15. echo $i
  16. }
  17. function battery_pct_remaining() {
  18. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  19. battery_pct
  20. else
  21. echo "External Power"
  22. fi
  23. }
  24. function battery_time_remaining() {
  25. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  26. timeremaining=$(ioreg -rc "AppleSmartBattery"| grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
  27. echo "~$((timeremaining / 60)):$((timeremaining % 60))"
  28. else
  29. echo "∞"
  30. fi
  31. }
  32. function battery_pct_prompt () {
  33. if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
  34. b=$(battery_pct_remaining)
  35. if [ $b -gt 50 ] ; then
  36. color='green'
  37. elif [ $b -gt 20 ] ; then
  38. color='yellow'
  39. else
  40. color='red'
  41. fi
  42. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  43. else
  44. echo "∞"
  45. fi
  46. }
  47. elif [[ $(uname) == "Linux" ]] ; then
  48. function battery_pct_remaining() {
  49. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  50. echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')"
  51. fi
  52. }
  53. function battery_time_remaining() {
  54. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  55. echo $(acpi | cut -f3 -d ',')
  56. fi
  57. }
  58. function battery_pct_prompt() {
  59. b=$(battery_pct_remaining)
  60. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  61. if [ $b -gt 50 ] ; then
  62. color='green'
  63. elif [ $b -gt 20 ] ; then
  64. color='yellow'
  65. else
  66. color='red'
  67. fi
  68. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  69. else
  70. echo "∞"
  71. fi
  72. }
  73. else
  74. # Empty functions so we don't cause errors in prompts
  75. function battery_pct_remaining() {
  76. }
  77. function battery_time_remaining() {
  78. }
  79. function battery_pct_prompt() {
  80. }
  81. fi