forked from lwilletts/mpvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpvc-tui
executable file
·396 lines (349 loc) · 12.3 KB
/
mpvc-tui
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env sh
#
# @file mpvc-tui
# @description mpvc tui, mpv terminal user interface
# @author gmt4 <gmt4 at github.com> (c) Copyright 2022 GPLv2+
# @url github.com/gmt4/mpvc
# SPDX-License-Identifier: GPL-2.0-or-later
#
PROGNAME=${0##*/}
PROGVERSION="v1.6"
PROGAUTHOR=gmt4
PROGURL="https://github.com/gmt4/mpvc"
SITEURL="https://gmt4.github.io/mpvc"
set -uf # -euf # TBD
equiet() { "$@" 2> /dev/null; }
oquiet() { "$@" 1> /dev/null; }
quiet() { "$@" > /dev/null 2>&1; }
# Set TUI defaults
mpvctui_defaults()
{
MPVC_TUI_CBLACK='\033[0;30m'
MPVC_TUI_CRED='\033[0;31m'
MPVC_TUI_CGREEN='\033[0;32m'
MPVC_TUI_CGREENL='\033[1;32m'
MPVC_TUI_CYELLOW='\033[0;33m'
MPVC_TUI_CYELLOWL='\033[1;33m'
MPVC_TUI_CBLUE='\033[0;34m'
MPVC_TUI_CBLUEL='\033[1;34m'
MPVC_TUI_CPURPLE='\033[0;35m'
MPVC_TUI_CPURPLEL='\033[1;35m'
MPVC_TUI_CCYAN='\033[0;36m'
MPVC_TUI_CCYANL='\033[1;36m'
MPVC_TUI_CWHITE='\033[0;37m'
MPVC_TUI_CRST='\033[0m'
MPVC_TUI_CLEAR='\033c'
MPVC_TUI_CUP='\033[1;1H'
# config defaults
NO_COLOR="${NO_COLOR:-}"
MPVC_TUI_COLOR0="${MPVC_TUI_COLOR0:-$MPVC_TUI_CGREENL}"
MPVC_TUI_COLOR1="${MPVC_TUI_COLOR1:-$MPVC_TUI_CCYAN}"
MPVC_TUI_COLOR2="${MPVC_TUI_COLOR2:-$MPVC_TUI_CYELLOW}"
MPVC_TUI_UPDATEC=${MPVC_TUI_UPDATEC:-2}
MPVC_TUI_CLEARC=${MPVC_TUI_CLEARC:-4}
MPVC_TUI_ICON=${MPVC_TUI_ICON:-}
MPVC_TUI_HEADER0=${MPVC_TUI_HEADER0:-$PROGNAME}
MPVC_TUI_TIPS=${MPVC_TUI_TIPS:-true}
MPVC_TUI_TERM=${MPVC_TUI_TERM:-xterm}
MPVC_TUI_SHELL=$(readlink -f /proc/$$/exe)
MPVC_CMD_STATUS="${MPVC_CMD_STATUS:-status}"
MPVC_CMD_PLAYLIST="${MPVC_CMD_PLAYLIST:-playlist}"
MPVC_CMD_CHAPTERLIST=${MPVC_CMD_CHAPTERLIST:-chapter-list}
export MPVC_TUI_CLINE=${MPVC_TUI_CLINE:-$(( $(mpvc statussz) + 6 ))}
}
# Load user config
mpvctui_config()
{
mpvctui_defaults
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-"$HOME/.config"}
MPVC_CONFIG_DIR="${MPVC_CONFIG_DIR:-$XDG_CONFIG_HOME/mpvc}"
MPVC_CONFIG="${MPVC_CONFIG:-$MPVC_CONFIG_DIR/mpvc.conf}"
MPVC_TUI_HISTORY="${MPVC_TUI_HISTORY:-$XDG_CONFIG_HOME/mpvc/mpvc-tui.history}"
MPVC_TUI_SCROBBLER="${MPVC_TUI_SCROBBLER:-$XDG_CONFIG_HOME/mpvc/mpvc-tui.scb}"
# if not present, create config dir
mkdir -p "$MPVC_CONFIG_DIR"
touch "$MPVC_TUI_HISTORY"
# if present load user config
if [ -r "$MPVC_CONFIG" ]; then
. "$MPVC_CONFIG"
fi
if ! quiet command -v rlwrap; then
mpvctui_warn "$PROGNAME: Warning: No rlwrap, Install to get history, filename & completion support!"
sleep 1
fi
if ! quiet command -v notify-send; then
mpvctui_warn "$PROGNAME: Warning: No notify-send, Install to get mpv desktop notifications"
sleep 1
fi
}
mpvctui_warn() { echo "$@" >&2; }
# Displays "──[ $@ ]─────────────────────────────────────────────────────"
mpvctui_headerc()
{
char="$1"; shift
cstart="$1"; shift
cend="$1"; shift
ccolor="$1"; shift
cols=$(stty size)
cols=${cols##* }
hdr="${char}${char}${cstart}$*${cend}"
rem=$(( cols - ${#hdr} ))
# when SHELL=/bin/dash, ${#hdr} is 4 chars smaller
if [ "${MPVC_TUI_SHELL##*/}" = "dash" ]; then rem=$(( rem + 4 )); fi
for _ in $(seq 1 $rem)
do
hdr="$hdr$char"
done
if [ -n "$NO_COLOR" ]; then
printf "%s" "${hdr}"
else
# not using printf "%s" here as sh displays raw escape sequences, instead of colored output
printf "${ccolor}${hdr}${MPVC_TUI_CRST}"
fi
}
mpvctui_header0() { mpvctui_headerc '═' '[' ']' "$MPVC_TUI_COLOR0" " + $* "; }
mpvctui_header1() { mpvctui_headerc '─' '[' ']' "$MPVC_TUI_COLOR1" " + $* "; }
mpvctui_header2() { mpvctui_headerc '─' '[' ']' "$MPVC_TUI_COLOR2" " + $* "; }
mpvctui_tips()
{
if [ "$MPVC_TUI_TIPS" = "false" ]; then return; fi
tip=$(( mpvctui_count % 80 ))
case $tip in
0|1|2) echo ": type control+c to enter prompt";;
10|11|12) echo ": Check $PROGURL";;
20|21|22) echo ": Check $SITEURL";;
30|31|32) echo ": Config at ${MPVC_CONFIG##$HOME/}";;
40|41|42) echo ": Launch from WM with: $PROGNAME -x";;
50|51|52) echo ": version $PROGNAME $PROGVERSION at $PROGURL";;
60|61|62) echo ": Update $MPVC_TUI_UPDATEC secs refresh $((mpvctui_count % MPVC_TUI_CLEARC))/$MPVC_TUI_CLEARC shell $MPVC_TUI_SHELL";;
esac
}
mpvctui_watcher()
{
c=$(equiet mpvc get playlist-count)
cc=$(equiet mpvc get chapters)
mpvctui_header0 "${MPVC_TUI_HEADER0}$(mpvctui_tips)"
mpvctui_header1 "Status"
mpvc "${MPVC_CMD_STATUS}"
mpvctui_header1 "Playlist"
mpvc "${MPVC_CMD_PLAYLIST}"
if [ "${c:-}" != "" ] && [ "${cc:-}" != "null" ] && [ "$c" -eq 1 -a "$cc" -gt 0 ]
then
mpvctui_header2 "Chapterlist"
mpvc "${MPVC_CMD_CHAPTERLIST}"
fi
mpvctui_header1 "Prompt"
}
mpvctui_input()
{
MPVC_TUI_PROMPT="($PROGNAME) ${PWD##*/}: "
if quiet command -v rlwrap; then
cmd=$(rlwrap -o -c -S "$MPVC_TUI_PROMPT" -H "$MPVC_TUI_HISTORY" -f . -P '' cat)
else
echo "$MPVC_TUI_PROMPT"; read -r cmd
fi
case "$cmd" in
'!!'*) "${SHELL}" -c "${cmd##!!}";;
'!'*) eval "${cmd##!}";;
'mpvc '*) QUIETFLAG=true $cmd;;
'mpvc-tui '*) $cmd;;
'mpvc-fzf '*) $cmd;;
exit|quit|q) exit;;
*) echo 'mpvc: Type commands at "mpvc:" prompt, "q" to quit';;
esac
}
mpvctui_watch()
{
mpvctui_count=0
trap mpvctui_input INT
while :
do
output=$(mpvctui_watcher 2>&1)
if [ $(( mpvctui_count % MPVC_TUI_CLEARC )) -eq 0 ]; then printf "${MPVC_TUI_CLEAR}"; fi
printf "${MPVC_TUI_CUP}"
echo "$output"
mpvctui_count=$(( mpvctui_count + 1 ))
sleep $MPVC_TUI_UPDATEC
done
}
mpvctui_watch_auto()
{
mpvc -q --mpv &
( sleep 2 && if [ "$#" -gt 0 ]; then mpvc --append "$@"; fi )
( sleep 1; pgrep -f "$PROGNAME -n" || setsid "$0" -n ) &
mpvctui_watch
}
mpvctui_mouse()
{
clear
mpvctui_mouse_buttons
while mpvctui_mouse_events
do
clear
mpvctui_mouse_buttons
done
}
mpvctui_mouse_buttons()
{
# groups: flags play chapter seek volume
mpvc_button_mute=$( if [ "$(mpvc get mute)" = "yes" ]; then echo M; else echo m; fi )
mpvc_button_pause=$( if [ "$(mpvc get pause)" = "yes" ]; then echo P; else echo p; fi )
mpvc_button_video=$( if [ "$(mpvc get video)" = "yes" ]; then echo V; else echo v; fi )
mpvc_button_cache=$( if [ "$(mpvc get cache)" = "yes" ]; then echo C; else echo c; fi )
mpvc_button_repeat=$( if [ "$(mpvc get loop-playlist)" = "inf" ]; then echo R; else echo r; fi)
mpvc_button_single=$( if [ "$(mpvc get loop-file)" = "yes" ]; then echo S; else echo s; fi)
mpvc_button_idle=$( if [ "$(mpvc get idle)" = "yes" ]; then echo I; else echo i; fi )
mpvc_status=$(mpvc -f '#%position%/%playlistlength% %=chapter%/%chapters% (%percentage%%%) %volume%%%')
mpvc_buttons="${mpvc_button_mute}${mpvc_button_pause}${mpvc_button_video}${mpvc_button_cache}${mpvc_button_repeat}${mpvc_button_single}${mpvc_button_idle} <t> [c] {s} (v) ${mpvc_status}"
printf "%s" "$mpvc_buttons"
}
mpvctui_mouse_events()
{
# check if $TERM supports mouse
stty -icanon
printf "\e[?1002h"
# f: 1b5b 4d20 2623 0a
# | | |
# | | +----- coord TL 2121 - BR 7b3a: (33,33) - (123,58): (90,25)
# | +---------- btn press 4d20 release 4d23
# +--------------- tag for mouse event?
line=$(xxd -l 6 -g 2 -p)
case "$line" in
1b5b4d20*) # btn1 press
offsetx=0
offsety=0
coords=${line##1b5b4d20}
coordx=${coords%%??}
coordy=${coords##??}
posx=$((0x${coordx} - 0x21))
posy=$((0x${coordy} - 0x21))
#echo "posxy $posx/$(tput cols) $posy/$(tput lines)"
case "$posx,$posy" in
"0,$offsety") mpvc -q --mute;;
"1,$offsety") mpvc -q toggle;;
"2,$offsety") mpvc -q cycle video;;
"3,$offsety") mpvc -q cycle cache;;
"4,$offsety") mpvc -q repeat;;
"5,$offsety") mpvc -q single;;
"6,$offsety") mpvc -q cycle idle;;
"8,$offsety") mpvc -q prev;;
"9,$offsety") mpvc -q replay;;
"10,$offsety") mpvc -q next;;
"12,$offsety") mpvc chapter-prev;;
"13,$offsety") mpvc chapter-replay;;
"14,$offsety") mpvc chapter-next;;
"16,$offsety") mpvc -q seek -30;;
"17,$offsety") mpvc -q time 0;;
"18,$offsety") mpvc -q seek +60;;
"20,$offsety") mpvc -q vol -10;;
"21,$offsety") mpvc -q vol 70;;
"22,$offsety") mpvc -q vol +10;;
esac
;;
1b5b4d23*) # btn1 release
;;
esac
}
mpvctui_notify()
{
# $MPVC_TUI_SCROBBLER: scrobbling file contains one entry per line with tab-separated fields
cmd_scrobble='echo $(mpvc expand-text "$(date +%s)\t\\${=duration}\t\\${path}\t\\${media-title}") >> '"$MPVC_TUI_SCROBBLER"
cmd_notify='notify-send -u normal -i "'"$MPVC_TUI_ICON"'" "mpvc" "$(mpvc)";'
awk -v cmd_scrobble="${1:-$cmd_scrobble}" -v cmd_notify="${2:-$cmd_notify}" '
BEGIN { ot = systime(); do_print = (cmd_notify != "false" && cmd_scrobble != "false"); }
/playback-restart|property-change|metadata-update/ {
#print "notify",$0
t=systime()
if (t - ot > 1)
{
system(cmd_scrobble);
system(cmd_notify);
}
if (do_print) print t, $0, "scrobble";
if (do_print) print t, $0, "notify";
ot=t
}
'
}
mpvctui_notify_loop_cli()
{
scrobler_cli='echo $(date +"T%H:%M") $(mpvc -f "[%status%] #%position%/%playlistlength% %time% %artist% - %title% #%chapter%")'
notify_cli='false'
mpvctui_notify_loop "${1:-$scrobler_cli}" "${2:-$notify_cli}"
}
mpvctui_notify_loop()
{
# loop to prevent mpvc idleloop exits when mpvsocket closes (mpv terminates)
while sleep 1; do mpvc idleloop | mpvctui_notify "$@"; sleep 10; done
}
mpvctui_suggest()
{
awk '
BEGIN {
srand()
}
! /https:/ {
gsub($1,"")
gsub($1,"")
if (length($0) > 0)
{
tracks[n++]=$0
seen[$0]++
}
}
END {
//asort(seen, sorted)
r=int(rand() * n)
t=tracks[r]
print r,seen[t],t
}
' "$MPVC_TUI_SCROBBLER"
}
mpvctui_launch()
{
if quiet command -v $MPVC_TUI_TERM; then $MPVC_TUI_TERM -e "$0" "$@"; fi;
}
mpvctui_usage()
{
cat <<EOF
usage: $PROGNAME opts # @version $PROGVERSION (c) $PROGAUTHOR $PROGURL
-d|dir : Set the WD to the media directory given as argument
-n|notify : Desktop notification using notify on mpvc events (notify-send*)
-s|suggest : Suggest a random media to play based on previous media played
-S|scrobler: Starts the $PROGNAME scrobbler
-H|history : Starts the $PROGNAME history
-t|tui : Starts the $PROGNAME to manage the mpv playlist (rlwrap*)
-T|Tui : Combo that starts $PROGNAME -t -n, and adds media given as args
-x|launch : Starts $PROGNAME in a new $MPVC_TUI_TERM (\$MPVC_TUI_TERM) # combine with <opts>
-v|version : Prints the $PROGNAME version.
*tips: If unsure about where to begin, start with: $PROGNAME -d /path/to/media/ -T
EOF
exit;
}
mpvctui_main()
{
mpvctui_config
if [ $# -eq 0 ]; then mpvctui_usage; fi
for arg in "$@";
do
arg1="${2:-}"
case "$arg" in
-d|dir) shift; MPVC_TUI_DIR="$arg1"; cd "$MPVC_TUI_DIR" || return;;
-h|help) shift; mpvctui_usage ;;
-H|history) shift; cat "$MPVC_TUI_SCROBBLER" ;;
-t|tui) shift; mpvctui_watch ;;
-T|Tui) shift; mpvctui_watch_auto "$@" ;;
-M|Mouse) shift; mpvctui_launch "-m$arg1"; exit ;; # -g 15x1
-m|mouse) shift; mpvctui_mouse "$arg1" ;;
-n|Notify) shift; mpvctui_notify_loop "${1:-}" ;;
-N|notify) shift; mpvctui_notify "$@" ;;
-k|kill) shift; pkill -f "$PROGNAME" ;;
-s|suggest) shift; mpvctui_suggest ;;
-S|scrobler) shift; mpvctui_notify_loop_cli "${1:-}" ;;
-u|updatec) shift; MPVC_TUI_UPDATEC="$arg1" ;;
-x|launch) shift; mpvctui_launch "$@"; exit ;;
*) mpvctui_usage;;
esac
done
}
mpvctui_main "$@"