]> Sergey Matveev's repositories - nnn.git/blob - plugins/mocq
Clear less'es screen
[nnn.git] / plugins / mocq
1 #!/usr/bin/env sh
2
3 # Description: Appends and optionally plays music in MOC
4 #
5 # Notes:
6 #   - if selection is available, plays it, else plays the current file or directory
7 #   - appends tracks and exits is MOC is running, else clears playlist and adds tracks
8 #   - to let mocp shuffle tracks, set SHUFFLE=1
9 #
10 # Shell: POSIX compliant
11 # Authors: Arun Prakash Jana, ath3
12
13 IFS="$(printf '\n\r')"
14 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
15 cmd=$(pgrep -x mocp 2>/dev/null)
16 ret=$cmd
17
18 SHUFFLE="${SHUFFLE:-0}"
19
20 mocp_add ()
21 {
22     if [ "$SHUFFLE" = 1 ]; then
23         if [ "$resp" = "y" ]; then
24             arr=$(tr '\0' '\n' < "$selection")
25         elif [ -n "$1" ]; then
26             arr="$1"
27         fi
28
29         for entry in $arr
30         do
31             if [ -d "$entry" ]; then
32                 arr2=$arr2$(find "$entry" -type f \( ! -iname "*.m3u" ! -iname "*.pls" \))
33             elif echo "$entry" | grep -qv '\.m3u$\|\.pls$' ; then
34                 arr2=$(printf "%s\n%s" "$entry" "$arr2")
35             fi
36         done
37
38         mocp -o shuffle
39         echo "$arr2" | xargs -d "\n" mocp -a
40     else
41         if [ "$resp" = "y" ]; then
42             xargs < "$selection" -0 mocp -a
43         else
44             mocp -a "$1"
45         fi
46     fi
47 }
48
49 if [ ! -s "$selection" ] && [ -z "$1" ]; then
50    exit
51 fi
52
53 if [ "$2" = "opener" ]; then
54     :
55 elif [ -s "$selection" ]; then
56     printf "Work with selection? Enter 'y' to confirm: "
57     read -r resp
58 fi
59
60 if [ -z "$ret" ]; then
61     # mocp not running
62     mocp -S
63 else
64     # mocp running, check if it's playing
65     state=$(mocp -i | grep "State:" | cut -d' ' -f2)
66
67     if [ "$state" = 'PLAY' ]; then
68         # add to playlist and exit
69         mocp_add "$1"
70
71         # uncomment the line below to show mocp interface after appending
72         # mocp
73
74         exit
75     fi
76 fi
77
78 # clear selection and play
79 mocp -c
80 mocp_add "$1" "$resp"
81 mocp -p
82
83 # uncomment the line below to show mocp interface after appending
84 # mocp
85
86 # Clear selection
87 if [ "$resp" = "y" ] && [ -p "$NNN_PIPE" ]; then
88     printf "-" > "$NNN_PIPE"
89 fi