battery-status.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. # Waybar custom/battery module.
  3. # Bar text: battery icon + capacity%. Tooltip: time remaining + performance settings.
  4. bat=$(ls -d /sys/class/power_supply/BAT* 2>/dev/null | head -1)
  5. [ -z "$bat" ] && { echo '{"text":"?","tooltip":"No battery found","class":"unknown"}'; exit 0; }
  6. cap=$(cat "$bat/capacity" 2>/dev/null || echo 0)
  7. status=$(cat "$bat/status" 2>/dev/null || echo Unknown)
  8. ac=$(cat /sys/class/power_supply/AC/online 2>/dev/null || echo 0)
  9. # --- charge/energy figures (this laptop reports charge_* in uAh, current_* in uA) ---
  10. now=$(cat "$bat/charge_now" 2>/dev/null || cat "$bat/energy_now" 2>/dev/null || echo 0)
  11. full=$(cat "$bat/charge_full" 2>/dev/null || cat "$bat/energy_full" 2>/dev/null || echo 0)
  12. rate=$(cat "$bat/current_now" 2>/dev/null || cat "$bat/power_now" 2>/dev/null || echo 0)
  13. # --- time remaining ---
  14. time_str="calculating…"
  15. if [ "$rate" -gt 0 ] 2>/dev/null; then
  16. if [ "$status" = "Discharging" ]; then
  17. secs=$(( now * 3600 / rate ))
  18. label="remaining"
  19. elif [ "$status" = "Charging" ]; then
  20. secs=$(( (full - now) * 3600 / rate ))
  21. label="until full"
  22. else
  23. secs=0; label=""
  24. fi
  25. if [ "$secs" -gt 0 ]; then
  26. h=$(( secs / 3600 )); m=$(( (secs % 3600) / 60 ))
  27. time_str="${h}h ${m}m ${label}"
  28. fi
  29. fi
  30. [ "$status" = "Full" ] && time_str="fully charged"
  31. # --- performance settings ---
  32. prof=$(cat /sys/firmware/acpi/platform_profile 2>/dev/null || echo "n/a")
  33. gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null || echo "n/a")
  34. epp=$(cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference 2>/dev/null || echo "n/a")
  35. if [ "$ac" = "1" ]; then tlp="AC mode ⚡"; else tlp="battery mode 🔋"; fi
  36. # --- icon by capacity (FontAwesome) ---
  37. if [ "$cap" -ge 90 ]; then icon=$(printf '') # full
  38. elif [ "$cap" -ge 65 ]; then icon=$(printf '') # three-quarters
  39. elif [ "$cap" -ge 40 ]; then icon=$(printf '') # half
  40. elif [ "$cap" -ge 15 ]; then icon=$(printf '') # quarter
  41. else icon=$(printf '') # empty
  42. fi
  43. bolt=$(printf '')
  44. if [ "$status" = "Charging" ] || [ "$ac" = "1" ]; then
  45. text="$bolt $icon ${cap}%"
  46. else
  47. text="$icon ${cap}%"
  48. fi
  49. # --- css class ---
  50. class="$status"
  51. if [ "$cap" -le 15 ] && [ "$status" = "Discharging" ]; then class="critical discharging"
  52. elif [ "$cap" -le 30 ] && [ "$status" = "Discharging" ]; then class="warning discharging"
  53. fi
  54. tooltip="Battery: ${cap}% (${status})\n${time_str}\n\nPower profile: ${prof}\nCPU governor: ${gov}\nCPU EPP: ${epp}\nTLP: ${tlp}"
  55. printf '{"text": "%s", "tooltip": "%s", "class": "%s", "percentage": %s}\n' "$text" "$tooltip" "$class" "$cap"