-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpc_control
executable file
·364 lines (285 loc) · 12 KB
/
mpc_control
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/bin/bash
#Taken from https://github.com/bubbl3gum/mpd_control
#when set to exit, mpd_control will exit if you press escape
#when set to break, mpd_control will go the upper level if possible
ESC_ACTION="break"
# source configuration file for dmenu if exists
if [ -f $HOME/.dmenurc ]; then
. $HOME/.dmenurc
else
DMENU='dmenu -i -l 20'
fi
addaftercurrent(){
#playlist is empty, just add the song
if [ "$(mpc playlist | wc -l)" == "0" ]; then
mpc add "$1"
#there is no current song so mpd is stopped
#it seems to be impossible to determine the current songs' position when
#mpd is stopped, so just add to the end
elif [ -z "$(mpc current)" ]; then
mpc play
CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
END_POS=$(mpc playlist | wc -l)
mpc add "$1"
mpc move $(($END_POS+1)) $(($CUR_POS+1))
mpc stop
#at least 1 song is in the playlist, determine the position of the
#currently played song and add $1 after it
else
CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
END_POS=$(mpc playlist | wc -l)
mpc add "$1"
mpc move $(($END_POS+1)) $(($CUR_POS+1))
fi
}
addaftercurrentandplay(){
#playlist is empty, just add the song
if [ "$(mpc playlist | wc -l)" == "0" ]; then
mpc add "$1"
mpc play
#there is no current song so mpd is stopped
#it seems to be impossible to determine the current songs' position when
#mpd is stopped, so just add to the end
elif [ -z "$(mpc current)" ]; then
mpc play
CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
END_POS=$(mpc playlist | wc -l)
mpc add "$1"
mpc move $(($END_POS+1)) $(($CUR_POS+1))
mpc play $(($CUR_POS+1))
#at least 1 song is in the playlist, determine the position of the
#currently played song and add $1 after it
else
CUR_POS=$(mpc | tail -2 | head -1 | awk '{print $2}' | sed 's/#//' | awk -F/ '{print $1}')
END_POS=$(mpc playlist | wc -l)
mpc add "$1"
mpc move $(($END_POS+1)) $(($CUR_POS+1))
mpc play $(($CUR_POS+1))
fi
}
case $1 in
-a|--artist)
while true; do
ARTIST="$(mpc list artist | sort -f | $DMENU)";
if [ "$ARTIST" = "" ]; then $ESC_ACTION; fi
while true; do
ALBUMS=$(mpc list album artist "$ARTIST" | sort -f);
ALBUM=$(echo -e "replace all\nadd all\n--------------------------\n$ALBUMS" | $DMENU);
if [ "$ALBUM" = "" ]; then $ESC_ACTION;
elif [ "$ALBUM" = "replace all" ]; then
CUR_SONG=$(mpc current)
mpc clear
mpc find artist "$ARTIST" | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
$ESC_ACTION
elif [ "$ALBUM" = "add all" ]; then
mpc find artist "$ARTIST" | mpc add
$ESC_ACTION
fi
while true; do
TITLES=$(mpc list title artist "$ARTIST" album "$ALBUM")
TITLE=$(echo -e "replace all\nadd all\n--------------------------\n$TITLES" | $DMENU);
if [ "$TITLE" = "" ]; then $ESC_ACTION
elif [ "$TITLE" = "replace all" ]; then
CUR_SONG=$(mpc current)
mpc clear;
mpc find artist "$ARTIST" album "$ALBUM" | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
$ESC_ACTION
elif [ "$TITLE" = "add all" ]; then
mpc find artist "$ARTIST" album "$ALBUM" | mpc add
$ESC_ACTION
fi
while true; do
DEC=$(echo -e "add after current and play\nadd after current\nreplace\nadd at the end" | $DMENU);
case $DEC in
"")
$ESC_ACTION
;;
"add after current and play")
addaftercurrentandplay "$(mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 )"
;;
"add after current")
addaftercurrent "$(mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 )"
;;
"replace")
CUR_SONG=$(mpc current)
mpc clear
mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
;;
"add at the end")
mpc find artist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 | mpc add
;;
esac
$ESC_ACTION
done
done
done
done
;;
-t|--track)
TITLE=$(mpc list title | sort -f | $DMENU)
if [ "$TITLE" = "" ]; then exit; fi
SONG=$(mpc find title "$TITLE" | head -1)
addaftercurrentandplay "$SONG"
;;
-n|--nexttrack)
TITLE=$(mpc list title | sort -f | $DMENU)
if [ "$TITLE" = "" ]; then exit; fi
SONG=$(mpc find title "$TITLE" | head -1)
addaftercurrent "$SONG"
;;
-p|--playlist)
PLAYLIST=$(mpc lsplaylists | $DMENU);
if [ "$PLAYLIST" = "" ]; then exit; fi
CUR_SONG=$(mpc current)
mpc clear
mpc load "$PLAYLIST";
if [ -n "$CUR_SONG" ]; then mpc play; fi
;;
-j|--jump)
TITLE=$(mpc playlist | $DMENU);
if [ "$TITLE" = "" ]; then exit; fi
POS=$(mpc playlist | grep -n "$TITLE" | awk -F: '{print $1}' | cut -d$'\n' -f1)
mpc play $POS;
;;
-l|--longplayer)
while true; do
ALBUM=$(mpc list album | sort -f | $DMENU);
if [ "$ALBUM" = "" ]; then $ESC_ACTION;
fi
while true; do
TITLES=$(mpc list title album "$ALBUM")
TITLE=$(echo -e "replace all\nadd all\n--------------------------\n$TITLES" | $DMENU);
if [ "$TITLE" = "" ]; then $ESC_ACTION
elif [ "$TITLE" = "replace all" ]; then
CUR_SONG=$(mpc current)
mpc clear;
mpc find album "$ALBUM" | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
$ESC_ACTION
elif [ "$TITLE" = "add all" ]; then
mpc find album "$ALBUM" | mpc add
$ESC_ACTION
fi
while true; do
DEC=$(echo -e "add after current and play\nadd after current\nreplace\nadd at the end" | $DMENU);
case $DEC in
"")
$ESC_ACTION
;;
"add after current and play")
addaftercurrentandplay "$(mpc find album "$ALBUM" title "$TITLE" | head -1 )"
;;
"add after current")
addaftercurrent "$(mpc find album "$ALBUM" title "$TITLE" | head -1 )"
;;
"replace")
CUR_SONG=$(mpc current)
mpc clear
mpc find album "$ALBUM" title "$TITLE" | head -1 | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
;;
"add at the end")
mpc find album "$ALBUM" title "$TITLE" | head -1 | mpc add
;;
esac
$ESC_ACTION
done
done
done
;;
-c|--composer)
while true; do
ARTIST="$(mpc list albumartist | sort -f | $DMENU)";
if [ "$ARTIST" = "" ]; then $ESC_ACTION; fi
while true; do
ALBUMS=$(mpc list album albumartist "$ARTIST" | sort -f);
ALBUM=$(echo -e "replace all\nadd all\n--------------------------\n$ALBUMS" | $DMENU);
if [ "$ALBUM" = "" ]; then $ESC_ACTION;
elif [ "$ALBUM" = "replace all" ]; then
CUR_SONG=$(mpc current)
mpc clear
mpc find albumartist "$ARTIST" | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
$ESC_ACTION
elif [ "$ALBUM" = "add all" ]; then
mpc find albumartist "$ARTIST" | mpc add
$ESC_ACTION
fi
while true; do
TITLES=$(mpc list title albumartist "$ARTIST" album "$ALBUM")
TITLE=$(echo -e "replace all\nadd all\n--------------------------\n$TITLES" | $DMENU);
if [ "$TITLE" = "" ]; then $ESC_ACTION
elif [ "$TITLE" = "replace all" ]; then
CUR_SONG=$(mpc current)
mpc clear;
mpc find albumartist "$ARTIST" album "$ALBUM" | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
$ESC_ACTION
elif [ "$TITLE" = "add all" ]; then
mpc find albumartist "$ARTIST" album "$ALBUM" | mpc add
$ESC_ACTION
fi
while true; do
DEC=$(echo -e "add after current and play\nadd after current\nreplace\nadd at the end" | $DMENU);
case $DEC in
"")
$ESC_ACTION
;;
"add after current and play")
addaftercurrentandplay "$(mpc find albumartist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 )"
;;
"add after current")
addaftercurrent "$(mpc find albumartist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 )"
;;
"replace")
CUR_SONG=$(mpc current)
mpc clear
mpc find albumartist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
;;
"add at the end")
mpc find albumartist "$ARTIST" album "$ALBUM" title "$TITLE" | head -1 | mpc add
;;
esac
$ESC_ACTION
done
done
done
done
;;
-g|--genre)
GENRE="$(mpc list Genre | sort -f | $DMENU)";
if [[ $GENRE != "" ]]; then
TITLES=$(mpc list title genre "$GENRE" | sort -f);
TITLE=$(echo -e "replace all\nadd all\n--------------------------\n$TITLES" | $DMENU);
if [ "$TITLE" = "replace all" ]; then
CUR_SONG=$(mpc current)
mpc clear
mpc find genre "$GENRE" | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
elif [ "$TITLE" = "add all" ]; then
mpc find genre "$GENRE" | mpc add
if [ -n "$CUR_SONG" ]; then mpc play; fi
elif [[ $TITLE != "" ]]; then
SONG=$(mpc find title "$TITLE" | head -1)
addaftercurrentandplay "$SONG"
fi
fi
;;
-h|--help)
echo -e "-a, --artist search for artist, then album, then title"
echo -e "-c, --composer search for composer, then album, then title"
echo -e "-t, --track search for a single track in the whole database"
echo -e "-p, --playlist search for a playlist load it"
echo -e "-j, --jump jump to another song in the current playlist"
echo -e "-l, --longplayer search for album, then title"
echo -e "-g, --genre search for genre, then title"
;;
*)
echo "Usage: mpc_control [OPTION]"
echo "Try 'mpc_control --help' for more information."
;;
esac