-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshpl
executable file
·351 lines (330 loc) · 7.59 KB
/
shpl
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
#!/bin/bash
#
# shellplayer.sh: POSIX shell driven music player wrapper.
# Supplies functionality for add song, loop current, stop looping, etc..
#
# Dependencies:
# POSIX environment
# 'sponge' (moreutils)
# 'shuf' (e.g. GNU coreutils)
# any media player (default is mplayer; edit the 'Play' function)
#
# TODO
# Provide usage info.
# Implement uniqing of playlist.
#
. "${SHELLPLAYER_CONFIG:-$HOME_CONFIG.config}" || exit
mkdir -p "$tmpdir" || {
Complain "could not create directory: $tmpdir"
exit 1
}
for file in "$cursongfile" "$playlist" "$delayfile" "$pidfile" "$playerpidfile" "$sleeppidfile"
do
>> "$file" || {
Complain "could not create file: $file"
exit 1
}
done
Usage ()
{
echo 'for now, read the code'
}
CleanUp ()
{
> "$cursongfile"
> "$pidfile"
> "$playerpidfile"
> "$sleeppidfile"
}
IsRunning ()
{
read -r pid < "$pidfile"
test "$pid"
}
VerifyIsRunning ()
{
IsRunning || {
Complain 'currently not running'
return 1
}
}
VerifyIsNotRunning ()
{
! IsRunning || {
Complain 'already running'
return 1
}
}
PLLineOneEmpty ()
{
read -r line < "$playlist"
test -z "$line"
}
PLEmpty ()
{
while read -r line
do test "$line" && return 1
done < "$playlist"
}
# If the playlist is "empty", make it really empty
PLEmpty && > "$playlist"
if [ -z "$1" ]
then
Complain 'no command given; run "shellplayer help" for help'
exit 1
fi
# To allow the command-list to be dynamically changed,
# we use this while loop instead of 'for cmd'
while [ $# -gt 0 ] && { cmd=$1; shift; }
do case $cmd in
# Playlist
i|in|inj|inject)
sponge "$playlist"
;;
a|ad|add)
# This command takes filenames as arguments
# If the user wants to chain other commands after this one,
# the filenames must be terminated with '/'
for arg
do
shift
case $arg in
/)
break
;;
'')
echo # A 'stopper'; empty lines cause a stop
;;
/*|eval:*)
printf '%s\n' "$arg"
;;
*)
printf '%s\n' "$(pwd)/$arg"
esac >> "$playlist"
done
;;
e|ed|edit)
Edit "$playlist"
;;
sh|shuf|shuffle)
if PLEmpty
then
Complain 'playlist is empty'
exit 1
fi
PLLineOneEmpty && ln1empty=true || ln1empty=false
sed /./!d "$playlist" \
| { $ln1empty && echo; shuf; } \
| sponge "$playlist"
;;
l|ls|lst|list)
i=0
while i=$((i+1)); read -r line
do printf '%5d %s\n' $i "$line"
done < "$playlist"
[ $i -eq 1 ] && Say 'playlist is empty'
;;
L|LS|LST|List)
cat "$playlist"
;;
cl|clr|clear)
> "$playlist"
;;
# Looping
ln|lo|lon|lop|lopon|loop|loopon)
>> "$looper" || {
Complain "could not create file: $looper"
exit 1
}
;;
lf|lof|lopof|loopoff|nl|nol|nolo|nolop|noloop)
rm -f "$looper" || {
Complain "could not remove file: $looper"
exit 1
}
;;
# Delay
d|delay)
# This behaviour is expected;
# this command cannot be chained without an argument
if [ "$1" ]
then printf '%s\n' "$1" > "$delayfile"; shift
else
read delay < "$delayfile"
Say "delay is $delay"
fi
;;
D|Delay)
read delay < "$delayfile"
printf '%s\n' "$delay"
;;
# Print current song
c|cur|current)
read -r song < "$cursongfile"
if [ "$song" ]
then Say "playing: $song"
else Say 'not playing anything'
fi
;;
C|Cur|Current)
read -r song < "$cursongfile"
printf '%s\n' "$song"
;;
# Start playing
r|run)
VerifyIsNotRunning || exit
(
unset ppid spid
trap '[ $ppid ] && kill $ppid; exit' TERM
trap '[ $ppid ] && kill $ppid; s=0' USR1 # Kill running song
while true
do
unset line
if [ -e "$looper" ]
then
if [ -e "$nexter" ] || ! IsRunning
then
rm -f "$nexter"
read -r line < "$playlist"
fi
else
read -r line < "$playlist"
fi
if [ "${line+.}" ]
then
sed 1d "$playlist" | sponge "$playlist"
case $line in
'')
CleanUp; exit
;;
/*)
printf '%s\n' "$line" > "$cursongfile"
;;
*)
case $line in
eval:*)
eval "${line#eval:}"
;;
*) Complain wtf
esac
continue
esac
fi
read -r song < "$cursongfile"
Play "$song" < /dev/null > /dev/null 2>&1 &
ppid=$!
echo $ppid > "$playerpidfile"
s=; wait $ppid; s=${s:-$?}
unset ppid
> "$playerpidfile"
if [ "$exitonerror" ]
then
test $s -eq 0 || {
Complain "media player exited with status $s"
CleanUp; exit 1
}
fi
# So we don't sleep unnecessarily
if PLLineOneEmpty && ! [ -e "$looper" ]
then
sed 1d "$playlist" | sponge "$playlist"
CleanUp; exit
fi
read delay < "$delayfile"
sleep "$delay" &
spid=$!
echo $spid > "$sleeppidfile"
wait $spid
> "$sleeppidfile"
done
)&
echo $! > "$pidfile"
;;
p|pl|play)
# This command works like 'add' but turns into
# "clear add <arguments> / run", except:
# When used with no arguments nor terminating '/',
# (i.e. is last CLI arg) it turns into "run"
case $# in
0) set -- run ;;
*)
VerifyIsNotRunning || exit
first=true
replace=true
for arg
do
$first && set -- clear add; first=false
if [ "$arg" = / ] && $replace
then set -- "$@" / run; replace=false
else set -- "$@" "$arg"
fi
done
$replace && set -- "$@" / run
esac
;;
# Manipulate the behaviour of a running instance
re|rep|repeat|repl|replay)
VerifyIsRunning || exit
{ cat "$cursongfile"
sed '1{/./!d}' "$playlist"
} | sponge "$playlist"
;;
sk|skip)
read ppid < "$playerpidfile"
if [ $ppid ]
then
VerifyIsRunning || exit # Sets $pid
kill -USR1 $pid
else
Say 'media player not running'
fi
;;
sd|skd|skipdelay)
read spid < "$sleeppidfile"
if [ $spid ]
then
VerifyIsRunning || exit
kill $spid
else
Say 'not sleeping'
fi
;;
s|st|stop)
# Stop when current song finishes
VerifyIsRunning || exit
if [ -e "$looper" ]
then set -- pause "$@"
else
if PLLineOneEmpty
then Say 'already about to stop'
else echo | cat - "$playlist" | sponge "$playlist"
fi
fi
;;
ns|nost|nostop|ds|donst|dontstop)
# Reverse a stop command
VerifyIsRunning || exit
PLLineOneEmpty || exit
sed 1d "$playlist" | sponge "$playlist"
;;
n|ne|nx|nxt|next)
# While looping, move to next song when current finishes
VerifyIsRunning || exit
test -e "$looper" || {
Complain 'not looping'
exit 1
}
>> "$nexter"
;;
k|kill)
VerifyIsRunning || exit
if PLLineOneEmpty
then set -- skip "$@"
else set -- stop skip "$@"
fi
;;
help|-h|-help|--help) Usage ;;
*)
Complain 'unrecognized command'
exit 1
esac done