2019-06-03 12:56:53 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Appends and optionally plays music in MOC
|
|
|
|
#
|
|
|
|
# Notes:
|
|
|
|
# - 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
|
2019-06-27 00:19:55 +00:00
|
|
|
# - to randomize the order of files appended to the playlist, set SHUFFLE=1
|
|
|
|
# if you add a directory with many files when SHUFFLE=1 is set, it might take a very long time to finish!
|
2019-06-03 12:56:53 +00:00
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
2019-06-27 00:19:55 +00:00
|
|
|
# Author: Arun Prakash Jana, ath3
|
2019-06-03 12:56:53 +00:00
|
|
|
|
2019-06-27 00:19:55 +00:00
|
|
|
IFS="$(printf '\n\r')"
|
2019-06-20 13:40:47 +00:00
|
|
|
selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection
|
2019-06-03 12:56:53 +00:00
|
|
|
cmd=$(pgrep -x mocp 2>/dev/null)
|
|
|
|
ret=$cmd
|
|
|
|
|
2019-06-27 00:19:55 +00:00
|
|
|
SHUFFLE=0
|
|
|
|
|
|
|
|
mocp_add() {
|
|
|
|
if [ $SHUFFLE = 1 ]; then
|
|
|
|
if [ -s "$selection" ]; then
|
|
|
|
arr=$(tr '\0' '\n' < "$selection")
|
|
|
|
elif [ -n "$1" ]; then
|
|
|
|
arr="$1"
|
|
|
|
fi
|
|
|
|
|
|
|
|
for entry in $arr
|
|
|
|
do
|
|
|
|
if [ -d "$entry" ]; then
|
|
|
|
arr2=$arr2$(find "$entry" -type f)
|
|
|
|
else
|
|
|
|
arr2=$(printf "%s\n%s" "$entry" "$arr2")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
arr2=$(echo "$arr2" | awk 'BEGIN{srand();}{print rand()"\t"$0}' | sort -k1 -n | cut -f2-)
|
|
|
|
for entry in $arr2
|
|
|
|
do
|
|
|
|
if [ -f "$entry" ] && [ "$(echo "$entry" | grep -v '\.m3u$\|\.pls$')" ]; then
|
|
|
|
mocp -a "$entry"
|
|
|
|
fi
|
|
|
|
done
|
2019-06-03 12:56:53 +00:00
|
|
|
|
|
|
|
else
|
2019-06-27 00:19:55 +00:00
|
|
|
if [ -s "$selection" ]; then
|
|
|
|
xargs < "$selection" -0 mocp -a
|
2019-06-03 12:56:53 +00:00
|
|
|
else
|
|
|
|
mocp -a "$1"
|
|
|
|
fi
|
|
|
|
fi
|
2019-06-27 00:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ ! -s "$selection" ] && [ -z "$1" ]; then
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$ret" ]; then
|
|
|
|
# mocp not running
|
|
|
|
mocp -S
|
|
|
|
|
|
|
|
# clear selection and play
|
|
|
|
mocp -c
|
|
|
|
mocp_add "$1"
|
|
|
|
mocp -p
|
|
|
|
else
|
|
|
|
# mocp running, just append
|
|
|
|
mocp_add "$1"
|
2019-06-03 12:56:53 +00:00
|
|
|
fi
|
2019-06-07 03:14:49 +00:00
|
|
|
|
|
|
|
# uncomment the line below to show mocp interface after appending
|
|
|
|
# mocp
|