battery.plugin.zsh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
  49. function battery_pct_remaining() { echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')" }
  50. function battery_time_remaining() { echo $(acpi | cut -f3 -d ',') }
  51. function battery_pct_prompt() {
  52. b=$(battery_pct_remaining)
  53. if [ $b -gt 50 ] ; then
  54. color='green'
  55. elif [ $b -gt 20 ] ; then
  56. color='yellow'
  57. else
  58. color='red'
  59. fi
  60. echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
  61. }
  62. else
  63. error_msg='no battery'
  64. function battery_pct_remaining() { echo $error_msg }
  65. function battery_time_remaining() { echo $error_msg }
  66. function battery_pct_prompt() { echo '' }
  67. fi
  68. fi