|
|
@@ -0,0 +1,65 @@
|
|
|
+#!/bin/bash
|
|
|
+# Waybar custom/battery module.
|
|
|
+# Bar text: battery icon + capacity%. Tooltip: time remaining + performance settings.
|
|
|
+
|
|
|
+bat=$(ls -d /sys/class/power_supply/BAT* 2>/dev/null | head -1)
|
|
|
+[ -z "$bat" ] && { echo '{"text":"?","tooltip":"No battery found","class":"unknown"}'; exit 0; }
|
|
|
+
|
|
|
+cap=$(cat "$bat/capacity" 2>/dev/null || echo 0)
|
|
|
+status=$(cat "$bat/status" 2>/dev/null || echo Unknown)
|
|
|
+ac=$(cat /sys/class/power_supply/AC/online 2>/dev/null || echo 0)
|
|
|
+
|
|
|
+# --- charge/energy figures (this laptop reports charge_* in uAh, current_* in uA) ---
|
|
|
+now=$(cat "$bat/charge_now" 2>/dev/null || cat "$bat/energy_now" 2>/dev/null || echo 0)
|
|
|
+full=$(cat "$bat/charge_full" 2>/dev/null || cat "$bat/energy_full" 2>/dev/null || echo 0)
|
|
|
+rate=$(cat "$bat/current_now" 2>/dev/null || cat "$bat/power_now" 2>/dev/null || echo 0)
|
|
|
+
|
|
|
+# --- time remaining ---
|
|
|
+time_str="calculating…"
|
|
|
+if [ "$rate" -gt 0 ] 2>/dev/null; then
|
|
|
+ if [ "$status" = "Discharging" ]; then
|
|
|
+ secs=$(( now * 3600 / rate ))
|
|
|
+ label="remaining"
|
|
|
+ elif [ "$status" = "Charging" ]; then
|
|
|
+ secs=$(( (full - now) * 3600 / rate ))
|
|
|
+ label="until full"
|
|
|
+ else
|
|
|
+ secs=0; label=""
|
|
|
+ fi
|
|
|
+ if [ "$secs" -gt 0 ]; then
|
|
|
+ h=$(( secs / 3600 )); m=$(( (secs % 3600) / 60 ))
|
|
|
+ time_str="${h}h ${m}m ${label}"
|
|
|
+ fi
|
|
|
+fi
|
|
|
+[ "$status" = "Full" ] && time_str="fully charged"
|
|
|
+
|
|
|
+# --- performance settings ---
|
|
|
+prof=$(cat /sys/firmware/acpi/platform_profile 2>/dev/null || echo "n/a")
|
|
|
+gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null || echo "n/a")
|
|
|
+epp=$(cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference 2>/dev/null || echo "n/a")
|
|
|
+if [ "$ac" = "1" ]; then tlp="AC mode ⚡"; else tlp="battery mode 🔋"; fi
|
|
|
+
|
|
|
+# --- icon by capacity (FontAwesome) ---
|
|
|
+if [ "$cap" -ge 90 ]; then icon=$(printf '') # full
|
|
|
+elif [ "$cap" -ge 65 ]; then icon=$(printf '') # three-quarters
|
|
|
+elif [ "$cap" -ge 40 ]; then icon=$(printf '') # half
|
|
|
+elif [ "$cap" -ge 15 ]; then icon=$(printf '') # quarter
|
|
|
+else icon=$(printf '') # empty
|
|
|
+fi
|
|
|
+bolt=$(printf '')
|
|
|
+
|
|
|
+if [ "$status" = "Charging" ] || [ "$ac" = "1" ]; then
|
|
|
+ text="$bolt $icon ${cap}%"
|
|
|
+else
|
|
|
+ text="$icon ${cap}%"
|
|
|
+fi
|
|
|
+
|
|
|
+# --- css class ---
|
|
|
+class="$status"
|
|
|
+if [ "$cap" -le 15 ] && [ "$status" = "Discharging" ]; then class="critical discharging"
|
|
|
+elif [ "$cap" -le 30 ] && [ "$status" = "Discharging" ]; then class="warning discharging"
|
|
|
+fi
|
|
|
+
|
|
|
+tooltip="Battery: ${cap}% (${status})\n${time_str}\n\nPower profile: ${prof}\nCPU governor: ${gov}\nCPU EPP: ${epp}\nTLP: ${tlp}"
|
|
|
+
|
|
|
+printf '{"text": "%s", "tooltip": "%s", "class": "%s", "percentage": %s}\n' "$text" "$tooltip" "$class" "$cap"
|