Files
nixos/Droidnix/generated/.config/scripts/media.sh
T
2026-04-13 10:32:04 +02:00

61 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# --- This file has been auto-generated. For permanent changes alter the appropriate block in the README.org. ---
player=$(playerctl -l 2>/dev/null | head -n1)
if [ -z "$player" ]; then
jq -c -n '{text: "", tooltip: "", class: "inactive"}'
exit 0
fi
status=$(playerctl --player="$player" status 2>/dev/null)
if [ "$status" != "Playing" ] && [ "$status" != "Paused" ]; then
jq -c -n '{text: "", tooltip: "", class: "inactive"}'
exit 0
fi
artist=$(playerctl --player="$player" metadata artist 2>/dev/null)
title=$(playerctl --player="$player" metadata title 2>/dev/null)
album=$(playerctl --player="$player" metadata album 2>/dev/null)
art=$(playerctl --player="$player" metadata mpris:artUrl 2>/dev/null)
length=$(playerctl --player="$player" metadata mpris:length 2>/dev/null)
position=$(playerctl --player="$player" position 2>/dev/null)
# Progress percentage — pure bash, no bc needed
if [ -n "$length" ] && [ -n "$position" ] && [ "$length" -gt 0 ] 2>/dev/null; then
# position is in seconds (float), length is in microseconds
pos_us=$(echo "$position" | awk '{printf "%d", $1 * 1000000}')
progress=$(( pos_us * 100 / length ))
else
progress=0
fi
# Spotify device
device=""
if [[ "$player" == *"spotify"* ]]; then
device=$(playerctl --player="$player" metadata xesam:url 2>/dev/null | grep -o 'device=[^&]*' | cut -d= -f2)
fi
# Ticker text — truncate if long
text="${artist}${title}"
if [ ${#text} -gt 40 ]; then
text="${text:0:37}..."
fi
# Icon
if [ "$status" = "Paused" ]; then
icon="⏸ "
else
icon="▶ "
fi
# Build tooltip
tooltip="${artist}\n${title}\n${album}"
if [ -n "$device" ]; then
tooltip="${tooltip}\n󰓻 ${device}"
fi
tooltip="${tooltip}\n${progress}%"
jq -c -n \
--arg text "${icon}${text}" \
--arg tooltip "$tooltip" \
--arg class "$player" \
--arg art "$art" \
'{text: $text, tooltip: $tooltip, class: $class, alt: $art}'