| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Find the UPower device path for any connected headset/headphones |
| 4 | DEVICE_PATH=$(upower -e | grep -E 'headset|headphone' | head -n 1) |
| 5 | |
| 6 | # If no headset device is found in UPower, exit silently so Waybar hides the module |
| 7 | if [ -z "$DEVICE_PATH" ]; then |
| 8 | exit 0 |
| 9 | fi |
| 10 | |
| 11 | # Extract the percentage value (e.g., "90%") |
| 12 | PERCENTAGE=$(upower -i "$DEVICE_PATH" | awk '/percentage:/ {print $2}') |
| 13 | |
| 14 | # Output JSON format for Waybar |
| 15 | if [ -n "$PERCENTAGE" ]; then |
| 16 | printf '{"text": "%s", "tooltip": "Headset Battery: %s", "class": "connected"}\n' "$PERCENTAGE" "$PERCENTAGE" |
| 17 | fi |