battery_check.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. ## A script to grab battery state for my (demuredemeanor) tmux statusbar
  3. ### Thinkpad Multi Battery (TMB) ### {{{
  4. if [ -e /sys/class/power_supply/BAT0/uevent ]; then
  5. BAT0="/sys/class/power_supply/BAT0/uevent"
  6. else
  7. BAT0=""
  8. fi
  9. ## Check for BAT1
  10. if [ -e /sys/class/power_supply/BAT1/uevent ]; then
  11. BAT1="/sys/class/power_supply/BAT1/uevent"
  12. else
  13. BAT1=""
  14. fi
  15. ## Aggregate data
  16. if [ "${BAT0}" != "" ] || [ "${BAT1}" != "" ]; then
  17. ## Originally "/sys/class/power_supply/BAT{0..1}/uevent" but changed into variables make work for non thinkpad cases. paste fails if it can't find a passed file.
  18. BASE="$(paste -d = ${BAT0} ${BAT1} 2>/dev/null)"
  19. PERC="$(echo "${BASE}" | awk 'BEGIN {CHARGE="U"} /ENERGY_FULL=/||/ENERGY_NOW=/||/CHARGE_NOW=/||/CHARGE_FULL=/ {split($0,a,"="); if(a[1]~/FULL/){FULL=a[2]+a[4]}; if(a[1]~/NOW/){NOW=a[2]+a[4]};} END {if(NOW!=""){PERC=int((NOW/FULL)*100)} else {PERC="none"}; print(PERC)}')"
  20. fi
  21. ### End Thinkpad Multi Battery (TMB) ### }}}
  22. ### Termux battery ### {{{
  23. if [ $OSTYPE == "linux-android" ]; then
  24. if [ $(command -v termux-battery-status) ]; then
  25. PERC=$(termux-battery-status | awk '/percentage/ {sub(/,/,""); print $2}')
  26. fi
  27. fi
  28. ### End Termux battery ### }}}
  29. ## Prep Output
  30. if [ ! -z "${PERC}" ]; then
  31. case ${PERC} in
  32. 100|9[5-9]) ICON="" ;; ## Full Icon 95-100%
  33. 8[5-9]|9[0-4]) ICON="" ;; ## 90% Icon 85-94%
  34. 7[5-9]|8[0-4]) ICON="" ;; ## 80% Icon 75-84%
  35. 6[5-9]|7[0-4]) ICON="" ;; ## 70% Icon 65-74%
  36. 5[5-9]|6[0-4]) ICON="" ;; ## 60% Icon 55-64%
  37. 4[5-9]|5[0-4]) ICON="" ;; ## 50% Icon 45-54%
  38. 3[5-9]|4[0-4]) ICON="" ;; ## 40% Icon 35-44%
  39. 2[5-9]|3[0-4]) ICON="" ;; ## 30% Icon 25-34%
  40. 1[5-9]|2[0-4]) ICON="" ;; ## 20% Icon 15-24%
  41. [5-9]|1[0-4]) ICON="" ;; ## 10% Icon 5-14%
  42. [0-4]) ICON="" ;; ## Empty Icon 0-4%
  43. *) ICON="" ;; ## Catchall Question Icon
  44. esac
  45. #echo -e "${ICON} ${PERC}%\n"
  46. echo "${ICON} ${PERC}%"
  47. else
  48. echo -e "\n"
  49. fi