cmusq (2284B)
1 #!/usr/bin/env sh 2 3 # Description: Add selection or hovered file/directory to cmus queue 4 # 5 # Dependencies: cmus, pgrep, xdotool (optional) 6 # 7 # Notes: 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 11 # 12 # TODO: 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 15 # 16 # Shell: POSIX compliant 17 # Author: Kabouik 18 19 # (Optional) Set preferred terminal emulator for cmus if not set in your env, 20 # or leave commented out to use OS default 21 #TERMINAL="kitty" 22 23 if ! type cmus >/dev/null; then 24 printf "cmus missing" 25 read -r _ 26 exit 1 27 fi 28 29 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} 30 31 start_cmus() { 32 type xdotool >/dev/null && nnnwindow="$(xdotool getactivewindow)" 33 case "$TERMINAL" in 34 kitty | gnome-terminal | st) 35 nohup "$TERMINAL" -- cmus & ;; 36 havoc) 37 nohup "$TERMINAL" cmus & ;; 38 "") 39 nohup x-terminal-emulator -e cmus & ;; 40 *) 41 nohup "$TERMINAL" -e cmus & ;; 42 esac 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" 46 } >/dev/null 2>&1 47 48 fill_queue() { 49 if [ "$REPLY" = "s" ]; then 50 xargs < "$selection" -0 cmus-remote -q 51 elif [ -n "$1" ]; then 52 cmus-remote -q "$1" 53 fi 54 } 55 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]: " 59 read -r REPLY 60 fi 61 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" 65 start_cmus 66 fill_queue "$1" 67 cmus-remote -p 68 printf "Files added to cmus queue.\n" 69 else # Append to existing queue if cmus is already running 70 fill_queue "$1" 71 printf "Files appended to current cmus queue.\n" 72 fi 73 74 # Change view 75 cmus-remote -C "view 4" 76 77 # Clear selection 78 if [ -p "$NNN_PIPE" ]; then 79 printf "-" > "$NNN_PIPE" 80 fi