fixed battery display

This commit is contained in:
2025-07-23 12:46:08 +02:00
parent 1ed4ef0963
commit a4434d7f97
4 changed files with 232 additions and 49 deletions

View File

@@ -1,24 +1,54 @@
#!/bin/bash
# Get the current power source from tlp-stat
power_source=$(tlp-stat -s | grep -i "power source" | awk '{print $4}')
# --- Configuration ---
CRITICAL_THRESHOLD=15
WARNING_THRESHOLD=50
# ---------------------
if [ "$power_source" == "AC" ]; then
# On AC power
icon=""
tooltip="TLP Profile: AC"
class="ac"
elif [ "$power_source" == "battery" ]; then
# On Battery power
icon=""
tooltip="TLP Profile: Battery"
class="bat"
else
# Fallback for unknown state
icon=""
tooltip="TLP Profile: Unknown"
class="unknown"
# Get the battery path from upower
battery_path=$(upower -e | grep 'BAT')
# Handle case where no battery is found
if [ -z "$battery_path" ]; then
# Check if we are on AC power anyway
if [[ $(tlp-stat -s | grep "Power source" | awk '{print $4}') == "AC" ]]; then
printf '{"text": "", "tooltip": "AC Power (No Battery)", "class": "ac"}\n'
else
printf '{"text": "", "tooltip": "Error: Battery not found", "class": "unknown"}\n'
fi
exit 0
fi
# Output in JSON format for Waybar
printf '{"text": "%s", "tooltip": "%s", "class": "%s"}\n' "$icon" "$tooltip" "$class"
# Get battery percentage and state
percentage=$(upower -i "$battery_path" | grep "percentage" | awk '{print $2}' | tr -d '%')
state=$(upower -i "$battery_path" | grep "state" | awk '{print $2}')
tlp_profile=$(tlp-stat -s | grep "Power source" | awk '{print $4}')
# Set icon, class, and tooltip based on state and percentage
if [ "$state" == "charging" ] || [ "$tlp_profile" == "AC" ]; then
icon=""
class="charging"
tooltip="TLP: AC | Charging at ${percentage}%"
elif [ "$state" == "discharging" ]; then
tooltip="TLP: Battery | Discharging at ${percentage}%"
if [ "$percentage" -le "$CRITICAL_THRESHOLD" ]; then
icon="" # Very low
class="critical"
elif [ "$percentage" -le "$WARNING_THRESHOLD" ]; then
icon="" # Low
class="warning"
elif [ "$percentage" -le 85 ]; then
icon="" # Healthy
class="bat"
else
icon="" # Full
class="bat"
fi
else # Fallback for "fully-charged", "pending-charge", etc.
icon=""
class="charging"
tooltip="TLP: AC | Fully Charged at ${percentage}%"
fi
# Output JSON for Waybar
printf '{"text": "%s %s%%", "tooltip": "%s", "class": "%s"}\n' "$icon" "$percentage" "$tooltip" "$class"