battery.plugin.zsh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. fi