1 79ed446f 2022-05-09 op # amused-monitor
3 79ed446f 2022-05-09 op This is a small interface for the amused music player
5 79ed446f 2022-05-09 op => https://projects.omarpolo.com/amused.html amused
7 79ed446f 2022-05-09 op #!/usr/bin/env rc
9 79ed446f 2022-05-09 op It's written in rc because I found easier to manage subprocesses in it
10 79ed446f 2022-05-09 op rather than in sh.
12 79ed446f 2022-05-09 op There are two main processes involved: input and ui. The input
13 79ed446f 2022-05-09 op process handles the keybindings and controls amused, the ui input
14 79ed446f 2022-05-09 op waits for some events and redraws the interface.
19 79ed446f 2022-05-09 op $ifs needs to be set to the empty list (or empty string), otherwise
20 79ed446f 2022-05-09 op we're not able to read spaces as keybindings.
23 79ed446f 2022-05-09 op while (~ $run 1) {
25 79ed446f 2022-05-09 op rc lacks a break statement, so I'm using a flag variable to quit
29 79ed446f 2022-05-09 op t=`{dd bs=1 count=1 status=none}
32 79ed446f 2022-05-09 op This is just a trick to read without echoing back the characters.
34 79ed446f 2022-05-09 op switch ($"t) {
46 0e4b4d70 2022-05-09 op printf ' unknown keybinding >'$t'<\r'
51 79ed446f 2022-05-09 op The ui functions render the interface. I'd like to keep a small xterm
52 79ed446f 2022-05-09 op open, that's why I'm narrowing (with grep -A -B) only to the songs
53 79ed446f 2022-05-09 op around the currently played one.
57 79ed446f 2022-05-09 op amused show -p | grep -A3 -B3 '^>' | awk '{printf "%s\r\n", $0}'
59 79ed446f 2022-05-09 op The awk trick deserves an explanation. Since the input function is
60 79ed446f 2022-05-09 op very likely running, the terminal is in "raw" mode: we need \r to
61 79ed446f 2022-05-09 op reposition the character at the start of the line.
65 79ed446f 2022-05-09 op The uiloop function is the driver for the ui process: it listens to
66 79ed446f 2022-05-09 op interesting events and redraws the ui when they happen
69 fed82e2b 2022-05-14 op amused monitor next,prev,jump | {
70 79ed446f 2022-05-09 op while (read) {
74 79ed446f 2022-05-09 op A small note of merit: unlike other shells read is not a built in in
75 79ed446f 2022-05-09 op rc bur rather an external command (/usr/local/plan9/bin/read here.)
80 79ed446f 2022-05-09 op And that's all. I only need to launch the functions in the correct
81 79ed446f 2022-05-09 op order and do one render at the start and amused-monitor is done!
87 79ed446f 2022-05-09 op kill -INT -$uipid