forge
Explore
Log in

Don't you dare go Hollow

dotfiles

Clone
★ 0

0 watching0 forksShellGPL-3.0334.8 KiBb1320e9Tahinli17 days ago105 commits

media_menu.sh103 lines, 2.5 KiBHistoryRawDownload
1#!/bin/bash
2# Media picker (rofi): choose which MPRIS player the waybar music module follows.
3# Auto (no pin file) follows the latest player; picking a player writes
4# /tmp/playerctl_pin (line 1 bus name, 2 url host, 3 base name, 4 timestamp).
5# Bound to middle-click on custom/music.
6
7PIN=/tmp/playerctl_pin
8
9notify() {
10 notify-send "$@" 2>/dev/null || true
11}
12
13# host of an http(s) url, empty otherwise
14host_of() {
15 local url=$1
16 [[ $url =~ ^https?:// ]] || return 0
17 sed -E 's#^[a-z]+://([^/]+).*#\1#' <<<"$url"
18}
19
20# --- collect players ----------------------------------------------------------
21
22players=()
23while IFS= read -r p; do
24 [[ -n $p ]] && players+=("$p")
25done < <(playerctl -l 2>/dev/null)
26
27if (( ${#players[@]} == 0 )); then
28 notify "No media players"
29 exit 0
30fi
31
32pinned=""
33if [[ -f $PIN ]]; then
34 pinned=$(sed -n 1p "$PIN" 2>/dev/null)
35fi
36
37# parallel arrays, index-aligned with players[]
38titles=()
39labels=()
40places=()
41entries=()
42
43if [[ -z $pinned ]]; then
44 entries+=("📌 Auto (follow latest)")
45else
46 entries+=("Auto (follow latest)")
47fi
48
49for p in "${players[@]}"; do
50 status=$(playerctl -p "$p" status 2>/dev/null)
51 artist=$(playerctl -p "$p" metadata artist 2>/dev/null)
52 title=$(playerctl -p "$p" metadata title 2>/dev/null)
53 url=$(playerctl -p "$p" metadata xesam:url 2>/dev/null)
54
55 case "$status" in
56 Playing) icon="▶" ;;
57 Paused) icon="⏸" ;;
58 *) icon="⏹" ;;
59 esac
60
61 if [[ -n $artist && -n $title ]]; then
62 label="$artist - $title"
63 elif [[ -n $title ]]; then
64 label=$title
65 else
66 label="(no title)"
67 fi
68
69 host=$(host_of "$url")
70 base=${p%%.*}
71 place=${host:-$base}
72
73 titles+=("${title:-(no title)}")
74 labels+=("$label")
75 places+=("$place")
76
77 prefix=""
78 [[ $p == "$pinned" ]] && prefix="📌 "
79 entries+=("${prefix}${icon} ${label} — ${place} [${p}]")
80done
81
82# --- menu -------------------------------------------------------------------
83
84idx=$(printf '%s\n' "${entries[@]}" | rofi -dmenu -i -p "Media" -format i)
85[[ -z $idx ]] && exit 0
86[[ $idx =~ ^[0-9]+$ ]] || exit 0
87
88if (( idx == 0 )); then
89 rm -f "$PIN"
90 notify "Media: auto"
91 exit 0
92fi
93
94(( idx <= ${#players[@]} )) || exit 0
95i=$((idx - 1))
96player=${players[i]}
97
98tmp=$(mktemp /tmp/playerctl_pin.XXXXXX) || exit 1
99host=$(host_of "$(playerctl -p "$player" metadata xesam:url 2>/dev/null)")
100printf '%s\n%s\n%s\n%s\n' "$player" "$host" "${player%%.*}" "$(date +%s)" > "$tmp"
101mv -f "$tmp" "$PIN"
102
103notify "Media pinned" "${titles[i]} — ${places[i]}"