master ampv
 1#!/bin/rc
 2
 3musicdir=$home/usr/Music
 4socket=/tmp/.mpvsocket
 5playlist=$home/usr/ampv-playlist
 6tmpfile=/tmp/.ampv.tmp
 7menu=(dmenu -i -l 20)
 8
 9fn player {exec mpv '--input-ipc-server='$socket $*}
10fn tosocket {printf '{ "command": '$*' }\n' | nc -UN $socket}
11fn getfilepath {tosocket '["get_property", "path"]' |  awk -F '"' '{print $4}'}
12fn addtolist {
13		find $musicdir -type f -name *.opus | $menu >$tmpfile
14		printf 'loadlist '$tmpfile' append\n' | nc -UN $socket
15}
16
17switch($1) {
18	case -a
19		songfile=`{getfilepath}
20		printf '%s\n' $"songfile >> $playlist
21	case -A
22		addtolist
23	case -d
24		songfile=`{getfilepath}
25		grep -v $"songfile $playlist > $tmpfile
26		mv -f $tmpfile $playlist
27	case -D
28		songfile=`{getfilepath}
29		rm -rf $"songfile
30	case -i
31		tosocket '["get_property", "filtered-metadata"]' | \
32			sed -e 's/{"data":{//' \
33				-e 's/"},"request_id":0,"error":"success"}//' \
34				-e 's/_/ /g' -e 's/":/: /g' -e 's/,"/\n/g' -e 's/"//g'
35	case -n
36		tosocket '["playlist-next"]'
37	case -N
38		addtolist
39		tosocket '["get_property", "playlist-pos"]' | awk -F '"' '{print $3}' | \
40			sed -e 's/://' -e 's/,//' >$tmpfile
41		pos=`{awk <$tmpfile '{print $1 + 1}'}
42		tosocket '["get_property", "playlist-count"]' | awk -F '"' '{print $3}' | \
43			sed -e 's/://' -e 's/,//' >$tmpfile
44		count=`{awk <$tmpfile '{print $1 - 1}'}
45		printf 'playlist-move '$count' '$pos\n | nc -UN $socket
46	case -p
47		tosocket '["cycle", "pause"]'
48	case -P
49		tosocket '["playlist-prev"]'
50	case -s
51		tosocket '["playlist-shuffle"]'
52	case -S
53		player --idle &
54	case -g
55		tosocket '["quit"]'
56		shift
57		if (~ $#* 0) {
58			find $musicdir -type f -name *.opus > $tmpfile
59			music='--playlist='$tmpfile
60		}
61		if not music=$*
62		player --no-audio-display $music
63	case -u
64		tosocket '["playlist-unshuffle"]'
65	case *
66		printf 'Usage: ampv -a|-d|-D|-i|-n|-p|-P|-s|-S|-g|-u\n'
67		exit 1
68}