Browse Source

Sync: 2026-07-21 20:37:47

Gabriel Capella 1 week ago
parent
commit
395d383891

+ 6 - 17
dots/.config/waybar/config

@@ -37,7 +37,7 @@
         "idle_inhibitor",
         "tray",
 // @host jellyfish
-        "battery",
+        "custom/battery",
 // @end
         "clock#date",
         "clock#time"
@@ -62,23 +62,12 @@
     },
 // @end
 
-    "battery": {
+    // Custom battery module: shows time remaining AND performance settings
+    // (power profile, CPU governor, EPP, TLP mode) in the hover tooltip.
+    "custom/battery": {
+        "exec": "~/.config/waybar/scripts/battery-status.sh",
+        "return-type": "json",
         "interval": 10,
-        "states": {
-            "warning": 30,
-            "critical": 15
-        },
-        // Connected to AC
-        "format": "  {icon}  {capacity}%", // Icon: bolt
-        // Not connected to AC
-        "format-discharging": "{icon}  {capacity}%",
-        "format-icons": [
-            "", // Icon: battery-full
-            "", // Icon: battery-three-quarters
-            "", // Icon: battery-half
-            "", // Icon: battery-quarter
-            ""  // Icon: battery-empty
-        ],
         "tooltip": true
     },
 

+ 65 - 0
dots/.config/waybar/scripts/battery-status.sh

@@ -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"

+ 11 - 5
dots/.config/waybar/style.css

@@ -56,6 +56,7 @@
 
 /* Each module */
 #battery,
+#custom-battery,
 #clock,
 #cpu,
 #custom-brightness,
@@ -79,26 +80,31 @@
 }
 
 
-#battery {
+#battery,
+#custom-battery {
     animation-timing-function: linear;
     animation-iteration-count: infinite;
     animation-direction: alternate;
 }
 
-#battery.warning {
+#battery.warning,
+#custom-battery.warning {
     color: orange;
 }
 
-#battery.critical {
+#battery.critical,
+#custom-battery.critical {
     color: red;
 }
 
-#battery.warning.discharging {
+#battery.warning.discharging,
+#custom-battery.warning.discharging {
     animation-name: blink-warning;
     animation-duration: 3s;
 }
 
-#battery.critical.discharging {
+#battery.critical.discharging,
+#custom-battery.critical.discharging {
     animation-name: blink-critical;
     animation-duration: 2s;
 }