dotfiles/.config/hypr/scripts/WaybarCava.sh
2026-05-13 21:22:17 +02:00

69 lines
1.8 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# ==================================================
# KoolDots (2026)
# Project URL: https://github.com/LinuxBeginnings
# License: GNU GPLv3
# SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# WaybarCava.sh — safer single-instance handling, cleanup, and robustness
# Original concept by LinuxBeginnings; this variant focuses on lifecycle hardening.
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
# Proactively reap any stale Waybar-spawned cava (unique temp conf names)
pkill -f 'waybar-cava\..*\.conf' 2>/dev/null || true
# 0..7 → ▁▂▃▄▅▆▇█
bar="▁▂▃▄▅▆▇█"
dict="s/;//g"
bar_length=${#bar}
for ((i = 0; i < bar_length; i++)); do
dict+=";s/$i/${bar:$i:1}/g"
done
# Single-instance guard (only kill our previous instance if its 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() {
# Kill children (cava, sed) of this script, then remove files
pkill -P "$$" 2>/dev/null || true
rm -f "$config_file" "$pidfile"
}
trap cleanup EXIT INT TERM
cat >"$config_file" <<EOF
[general]
framerate = 30
bars = 10
[input]
method = pulse
source = auto
[output]
method = raw
raw_target = /dev/stdout
data_format = ascii
ascii_max_range = 7
EOF
# Stream cava output and translate digits 0..7 to bar glyphs
# (no exec: keep this shell as the parent so the trap can reap children)
cava -p "$config_file" | sed -u "$dict"