diff options
| author | Donald Williams <129223418+dwilliam62@users.noreply.github.com> | 2025-09-20 12:37:50 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-20 12:37:50 -0400 |
| commit | 88f1b4d4ebbcdb58daecbbee2c0cdab791366452 (patch) | |
| tree | 3fcd7c0e55bc9940c9043c9a0cd2268db0863704 /config/hypr | |
| parent | cb3aabe4e7d2316ec851204e48770f587de98052 (diff) | |
| parent | 0cd0ca4466f3874052f592766a914791b8ab1a95 (diff) | |
Merge pull request #811 from dwilliam62/feature/waybarcava-hardening
WaybarCava: safer single-instance handling, cleanup, and robustness
Diffstat (limited to 'config/hypr')
| -rwxr-xr-x | config/hypr/scripts/WaybarCava.sh | 51 |
1 files changed, 32 insertions, 19 deletions
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" <<EOF [general] -# Older systems show significant CPU use with default framerate -# Setting maximum framerate to 30 -# You can increase the value if you wish framerate = 30 bars = 10 @@ -35,8 +51,5 @@ data_format = ascii ascii_max_range = 7 EOF -# Kill cava if it's already running -pkill -f "cava -p $config_file" - -# Read stdout from cava and perform substitution in a single sed command -cava -p "$config_file" | sed -u "$dict" +# Stream cava output and translate digits 0..7 to bar glyphs +exec cava -p "$config_file" | sed -u "$dict" |
