nnn/plugins/mocplay

90 lines
2 KiB
Plaintext
Raw Normal View History

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
2020-04-27 02:53:22 +00:00
# - to let mocp shuffle tracks, set SHUFFLE=1
2019-06-03 12:56:53 +00:00
#
# Shell: POSIX compliant
2020-05-06 05:12:29 +00:00
# Authors: Arun Prakash Jana, ath3
2019-06-03 12:56:53 +00:00
2019-06-27 00:19:55 +00:00
IFS="$(printf '\n\r')"
2020-04-24 04:42:24 +00:00
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
2019-06-03 12:56:53 +00:00
cmd=$(pgrep -x mocp 2>/dev/null)
ret=$cmd
SHUFFLE="${SHUFFLE:-0}"
2019-06-27 00:19:55 +00:00
2019-11-21 20:44:25 +00:00
mocp_add ()
{
if [ "$SHUFFLE" = 1 ]; then
if [ "$resp" = "y" ]; then
2019-06-27 00:19:55 +00:00
arr=$(tr '\0' '\n' < "$selection")
elif [ -n "$1" ]; then
arr="$1"
fi
for entry in $arr
do
if [ -d "$entry" ]; then
2020-04-26 18:35:11 +00:00
arr2=$arr2$(find "$entry" -type f \( ! -iname "*.m3u" ! -iname "*.pls" \))
elif echo "$entry" | grep -qv '\.m3u$\|\.pls$' ; then
2019-06-27 00:19:55 +00:00
arr2=$(printf "%s\n%s" "$entry" "$arr2")
fi
done
2020-04-27 02:53:22 +00:00
mocp -o shuffle
echo "$arr2" | xargs -d "\n" mocp -a
2019-06-03 12:56:53 +00:00
else
if [ "$resp" = "y" ]; then
2019-06-27 00:19:55 +00:00
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 [ "$2" = "opener" ]; then
:
elif [ -s "$selection" ]; then
2019-11-21 20:44:25 +00:00
printf "Work with selection? Enter 'y' to confirm: "
read -r resp
fi
2019-06-27 00:19:55 +00:00
if [ -z "$ret" ]; then
# mocp not running
mocp -S
else
2019-08-14 22:31:53 +00:00
# mocp running, check if it's playing
state=$(mocp -i | grep "State:" | cut -d' ' -f2)
2019-11-21 20:44:25 +00:00
if [ "$state" = 'PLAY' ]; then
2019-08-14 22:31:53 +00:00
# add to playlist and exit
mocp_add "$1"
# uncomment the line below to show mocp interface after appending
# mocp
exit
fi
2019-06-03 12:56:53 +00:00
fi
2019-06-07 03:14:49 +00:00
2019-08-14 22:31:53 +00:00
# clear selection and play
mocp -c
mocp_add "$1" "$resp"
2019-08-14 22:31:53 +00:00
mocp -p
2019-06-07 03:14:49 +00:00
# uncomment the line below to show mocp interface after appending
# mocp
# Clear selection
if [ "$resp" = "y" ] && [ -p "$NNN_PIPE" ]; then
printf "-" > "$NNN_PIPE"
fi