From 1e21d13d8f802989a8f68fef00c427f84208edb4 Mon Sep 17 00:00:00 2001 From: Don Williams Date: Wed, 3 Sep 2025 21:19:18 -0400 Subject: WaybarCava: safer single-instance handling, cleanup, and robustness improvements --- config/hypr/scripts/WaybarCava.sh | 51 ++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 19 deletions(-) (limited to 'config/hypr') diff --git a/config/hypr/scripts/WaybarCava.sh b/config/hypr/scripts/WaybarCava.sh index d31a05b5..6809e60e 100755 --- a/config/hypr/scripts/WaybarCava.sh +++ b/config/hypr/scripts/WaybarCava.sh @@ -1,26 +1,42 @@ -#!/bin/bash -# /* ---- πŸ’« https://github.com/JaKooLit πŸ’« ---- */ ## -# Not my own work. This was added through Github PR. Credit to original author +#!/usr/bin/env bash +# WaybarCava.sh β€” safer single-instance handling, cleanup, and robustness +# Original concept by JaKooLit; this variant focuses on lifecycle hardening. -#----- Optimized bars animation without much CPU usage increase -------- +set -euo pipefail + +# Ensure cava exists +if ! command -v cava >/dev/null 2>&1; then + echo "cava not found in PATH" >&2 + exit 1 +fi + +# 0..7 β†’ β–β–‚β–ƒβ–„β–…β–†β–‡β–ˆ bar="β–β–‚β–ƒβ–„β–…β–†β–‡β–ˆ" dict="s/;//g" - -# Calculate the length of the bar outside the loop bar_length=${#bar} - -# Create dictionary to replace char with bar for ((i = 0; i < bar_length; i++)); do - dict+=";s/$i/${bar:$i:1}/g" + dict+=";s/$i/${bar:$i:1}/g" done -# Create cava config -config_file="/tmp/bar_cava_config" +# Single-instance guard (only kill our previous instance if it’s still alive) +RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp}" +pidfile="$RUNTIME_DIR/waybar-cava.pid" +if [[ -f "$pidfile" ]]; then + oldpid="$(cat "$pidfile" || true)" + if [[ -n "$oldpid" ]] && kill -0 "$oldpid" 2>/dev/null; then + kill "$oldpid" 2>/dev/null || true + sleep 0.1 || true + fi +fi +printf '%d' $$ >"$pidfile" + +# Unique temp config + cleanup on exit +config_file="$(mktemp "$RUNTIME_DIR/waybar-cava.XXXXXX.conf")" +cleanup() { rm -f "$config_file" "$pidfile"; } +trap cleanup EXIT INT TERM + cat >"$config_file" <