3 # Description: Add selection or hovered file/directory to cmus queue
5 # Dependencies: cmus, pgrep, xdotool (optional)
8 # 1. If adding selection, files/dirs are added in the same order they were selected in nnn
9 # 2. A new window will be opened if cmus is not running already, playback will start immediately
10 # 3. If cmus is already running, files will be appended to the queue with no forced playback
13 # 1. Add cava and cmus-lyrics as optional dependencies
14 # 2. Start cava and/or cmus-lyrics in tmux or kitty panes next to cmus
16 # Shell: POSIX compliant
19 # (Optional) Set preferred terminal emulator for cmus if not set in your env,
20 # or leave commented out to use OS default
23 if ! type cmus >/dev/null; then
29 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
32 type xdotool >/dev/null && nnnwindow="$(xdotool getactivewindow)"
34 kitty | gnome-terminal | st)
35 nohup "$TERMINAL" -- cmus & ;;
37 nohup "$TERMINAL" cmus & ;;
39 nohup x-terminal-emulator -e cmus & ;;
41 nohup "$TERMINAL" -e cmus & ;;
43 # Give the new terminal some time to open
44 until cmus-remote -C; do sleep 0.1; done
45 [ -n "$nnnwindow" ] && xdotool windowactivate "$nnnwindow"
49 if [ "$REPLY" = "s" ]; then
50 xargs < "$selection" -0 cmus-remote -q
51 elif [ -n "$1" ]; then
56 # If active selection,then ask what to do
57 if [ -s "$selection" ]; then
58 printf "Queue [s]election or [c]urrently hovered? [default=c]: "
62 # If cmus is not running, start and play queue
63 if ! pgrep cmus >/dev/null; then
64 printf "cmus is not running, starting it in a new %s window.\n" "$TERMINAL"
68 printf "Files added to cmus queue.\n"
69 else # Append to existing queue if cmus is already running
71 printf "Files appended to current cmus queue.\n"
75 cmus-remote -C "view 4"
78 if [ -p "$NNN_PIPE" ]; then
79 printf "-" > "$NNN_PIPE"