aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mocplay
blob: 5f37c37773bf69c195671a68ffedc9e3ee8f7f0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/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
# - 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!
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana, ath3

IFS="$(printf '\n\r')"
selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection
cmd=$(pgrep -x mocp 2>/dev/null)
ret=$cmd

SHUFFLE=0

mocp_add() {
    if [ $SHUFFLE = 1 ]; then
        if [ "$resp" = "y" ]; 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

    else
        if [ "$resp" = "y" ]; then
            xargs < "$selection" -0 mocp -a
        else
            mocp -a "$1"
        fi
    fi
}

if [ ! -s "$selection" ] && [ -z "$1" ]; then
   exit
fi

if [ -s "$selection" ]; then
    echo -n "Work with selection? Enter 'y' to confirm: "
    read resp
fi

if [ -z "$ret" ]; then
    # mocp not running
    mocp -S
else
    # mocp running, check if it's playing
    state=$(mocp -i | grep "State:" | cut -d' ' -f2)

    if [ $state = 'PLAY' ]; then
        # add to playlist and exit
        mocp_add "$1"

        # uncomment the line below to show mocp interface after appending
        # mocp

        exit
    fi
fi

# clear selection and play
mocp -c
mocp_add "$1" "$resp"
mocp -p

# uncomment the line below to show mocp interface after appending
# mocp