2019-06-03 18:26:53 +05:30
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Appends and optionally plays music in MOC
|
|
|
|
#
|
|
|
|
# Notes:
|
2021-05-15 23:02:01 +05:30
|
|
|
# - if selection is available, plays it, else plays the current file or directory
|
|
|
|
# - appends tracks and exits is MOC is running, else clears playlist and adds tracks
|
|
|
|
# - to let mocp shuffle tracks, set SHUFFLE=1
|
2019-06-03 18:26:53 +05:30
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
2020-05-06 10:42:29 +05:30
|
|
|
# Authors: Arun Prakash Jana, ath3
|
2019-06-03 18:26:53 +05:30
|
|
|
|
2019-06-27 02:19:55 +02:00
|
|
|
IFS="$(printf '\n\r')"
|
2020-04-24 10:12:24 +05:30
|
|
|
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
2019-06-03 18:26:53 +05:30
|
|
|
cmd=$(pgrep -x mocp 2>/dev/null)
|
|
|
|
ret=$cmd
|
|
|
|
|
2020-03-16 06:40:02 +05:30
|
|
|
SHUFFLE="${SHUFFLE:-0}"
|
2019-06-27 02:19:55 +02:00
|
|
|
|
2019-11-22 02:14:25 +05:30
|
|
|
mocp_add ()
|
|
|
|
{
|
2020-03-16 06:40:02 +05:30
|
|
|
if [ "$SHUFFLE" = 1 ]; then
|
2019-09-16 23:35:43 +05:30
|
|
|
if [ "$resp" = "y" ]; then
|
2019-06-27 02:19:55 +02:00
|
|
|
arr=$(tr '\0' '\n' < "$selection")
|
|
|
|
elif [ -n "$1" ]; then
|
|
|
|
arr="$1"
|
|
|
|
fi
|
|
|
|
|
|
|
|
for entry in $arr
|
|
|
|
do
|
|
|
|
if [ -d "$entry" ]; then
|
2020-04-27 00:05:11 +05:30
|
|
|
arr2=$arr2$(find "$entry" -type f \( ! -iname "*.m3u" ! -iname "*.pls" \))
|
|
|
|
elif echo "$entry" | grep -qv '\.m3u$\|\.pls$' ; then
|
2019-06-27 02:19:55 +02:00
|
|
|
arr2=$(printf "%s\n%s" "$entry" "$arr2")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-04-27 08:23:22 +05:30
|
|
|
mocp -o shuffle
|
|
|
|
echo "$arr2" | xargs -d "\n" mocp -a
|
2019-06-03 18:26:53 +05:30
|
|
|
else
|
2019-09-16 23:35:43 +05:30
|
|
|
if [ "$resp" = "y" ]; then
|
2019-06-27 02:19:55 +02:00
|
|
|
xargs < "$selection" -0 mocp -a
|
2019-06-03 18:26:53 +05:30
|
|
|
else
|
|
|
|
mocp -a "$1"
|
|
|
|
fi
|
|
|
|
fi
|
2019-06-27 02:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ ! -s "$selection" ] && [ -z "$1" ]; then
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2020-02-23 15:23:39 +05:30
|
|
|
if [ "$2" = "opener" ]; then
|
|
|
|
:
|
|
|
|
elif [ -s "$selection" ]; then
|
2019-11-22 02:14:25 +05:30
|
|
|
printf "Work with selection? Enter 'y' to confirm: "
|
|
|
|
read -r resp
|
2019-09-16 23:35:43 +05:30
|
|
|
fi
|
|
|
|
|
2019-06-27 02:19:55 +02:00
|
|
|
if [ -z "$ret" ]; then
|
|
|
|
# mocp not running
|
|
|
|
mocp -S
|
|
|
|
else
|
2019-08-15 04:01:53 +05:30
|
|
|
# mocp running, check if it's playing
|
|
|
|
state=$(mocp -i | grep "State:" | cut -d' ' -f2)
|
|
|
|
|
2019-11-22 02:14:25 +05:30
|
|
|
if [ "$state" = 'PLAY' ]; then
|
2019-08-15 04:01:53 +05:30
|
|
|
# add to playlist and exit
|
|
|
|
mocp_add "$1"
|
|
|
|
|
|
|
|
# uncomment the line below to show mocp interface after appending
|
|
|
|
# mocp
|
|
|
|
|
|
|
|
exit
|
|
|
|
fi
|
2019-06-03 18:26:53 +05:30
|
|
|
fi
|
2019-06-07 08:44:49 +05:30
|
|
|
|
2019-08-15 04:01:53 +05:30
|
|
|
# clear selection and play
|
|
|
|
mocp -c
|
2019-09-16 23:35:43 +05:30
|
|
|
mocp_add "$1" "$resp"
|
2019-08-15 04:01:53 +05:30
|
|
|
mocp -p
|
|
|
|
|
2019-06-07 08:44:49 +05:30
|
|
|
# uncomment the line below to show mocp interface after appending
|
|
|
|
# mocp
|
2021-03-27 17:07:58 +05:30
|
|
|
|
|
|
|
# Clear selection
|
|
|
|
if [ "$resp" = "y" ] && [ -p "$NNN_PIPE" ]; then
|
|
|
|
printf "-" > "$NNN_PIPE"
|
|
|
|
fi
|