i3bat.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env bash
  2. readarray -t output <<< $(acpi battery)
  3. battery_count=${#output[@]}
  4. for line in "${output[@]}";
  5. do
  6. percentages+=($(echo "$line" | grep -o -m1 '[0-9]\{1,3\}%' | tr -d '%'))
  7. statuses+=($(echo "$line" | egrep -o -m1 'Discharging|Charging|AC|Full|Unknown'))
  8. remaining=$(echo "$line" | egrep -o -m1 '[0-9][0-9]:[0-9][0-9]')
  9. if [[ -n $remaining ]]; then
  10. remainings+=(" ($remaining)")
  11. else
  12. remainings+=("")
  13. fi
  14. done
  15. end=$(($battery_count - 1))
  16. for i in $(seq 0 $end);
  17. do
  18. if (( percentages[$i] > 0 && percentages[$i] < 25 )); then
  19. squares="$percentages%"
  20. elif (( percentages[$i] >= 20 && percentages[$i] < 50 )); then
  21. squares="$percentages%"
  22. elif (( percentages[$i] >= 40 && percentages[$i] < 75 )); then
  23. squares="$percentages%"
  24. elif (( percentages[$i] >= 60 && percentages[$i] < 100 )); then
  25. squares="$percentages%"
  26. elif (( percentages[$i] >=80 )); then
  27. squares="$percentages%"
  28. fi
  29. case "${statuses[$i]}" in
  30. "Charging")
  31. squares="charging "
  32. ;;
  33. "Full")
  34. squares="100%"
  35. ;;
  36. "AC")
  37. squares=""
  38. ;;
  39. esac
  40. message="$message $squares"
  41. done
  42. echo $message