From 4709d34b3e6b83b7e232be8524bc542d97500332 Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Sun, 28 Dec 2025 12:33:51 +0100 Subject: [PATCH] feat: changes to battery warning --- hosts/nb/modules/sway/sway.nix | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/hosts/nb/modules/sway/sway.nix b/hosts/nb/modules/sway/sway.nix index 6fc071d..3198322 100644 --- a/hosts/nb/modules/sway/sway.nix +++ b/hosts/nb/modules/sway/sway.nix @@ -37,8 +37,10 @@ let capacity=$(cat "$cap_file") status=$(cat "$status_file" 2>/dev/null || echo "Unknown") + stamp="/run/user/$(id -u)/.battery_swaynag_stamp" + pidfile="/run/user/$(id -u)/.battery_swaynag_pid" + if [[ "$capacity" -lt 20 && "$status" != "Charging" && "$status" != "Full" ]]; then - stamp="/run/user/$(id -u)/.battery_swaynag_stamp" now=$(date +%s) last=0 if [[ -f "$stamp" ]]; then @@ -46,10 +48,27 @@ let fi # Avoid spamming: at most once every 5 minutes if (( now - last >= 300 )); then + # Kill previous battery swaynag if still running + if [[ -f "$pidfile" ]]; then + old_pid=$(cat "$pidfile" 2>/dev/null || echo "") + if [[ -n "$old_pid" ]] && kill -0 "$old_pid" 2>/dev/null; then + kill "$old_pid" 2>/dev/null || true + fi + fi echo "$now" > "$stamp" swaynag -t warning -m "Battery low: ''${capacity}% - plug in the charger." -b "Dismiss" "true" & + echo $! > "$pidfile" disown || true fi + else + # Charging or battery OK - close any existing warning bar + if [[ -f "$pidfile" ]]; then + old_pid=$(cat "$pidfile" 2>/dev/null || echo "") + if [[ -n "$old_pid" ]] && kill -0 "$old_pid" 2>/dev/null; then + kill "$old_pid" 2>/dev/null || true + fi + rm -f "$pidfile" "$stamp" + fi fi '';