Blame


1 79ed446f 2022-05-09 op # amused-monitor
2 79ed446f 2022-05-09 op
3 79ed446f 2022-05-09 op This is a small interface for the amused music player
4 79ed446f 2022-05-09 op
5 79ed446f 2022-05-09 op => https://projects.omarpolo.com/amused.html amused
6 79ed446f 2022-05-09 op
7 79ed446f 2022-05-09 op #!/usr/bin/env rc
8 79ed446f 2022-05-09 op
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.
11 79ed446f 2022-05-09 op
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.
15 79ed446f 2022-05-09 op
16 79ed446f 2022-05-09 op fn input {
17 79ed446f 2022-05-09 op ifs=()
18 79ed446f 2022-05-09 op
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.
21 79ed446f 2022-05-09 op
22 79ed446f 2022-05-09 op run=1
23 79ed446f 2022-05-09 op while (~ $run 1) {
24 79ed446f 2022-05-09 op
25 79ed446f 2022-05-09 op rc lacks a break statement, so I'm using a flag variable to quit
26 79ed446f 2022-05-09 op looping.
27 79ed446f 2022-05-09 op
28 79ed446f 2022-05-09 op stty raw
29 79ed446f 2022-05-09 op t=`{dd bs=1 count=1 status=none}
30 79ed446f 2022-05-09 op stty cooked
31 79ed446f 2022-05-09 op
32 79ed446f 2022-05-09 op This is just a trick to read without echoing back the characters.
33 79ed446f 2022-05-09 op
34 79ed446f 2022-05-09 op switch ($"t) {
35 79ed446f 2022-05-09 op case n
36 79ed446f 2022-05-09 op amused next
37 79ed446f 2022-05-09 op case p
38 79ed446f 2022-05-09 op amused prev
39 79ed446f 2022-05-09 op case ' '
40 79ed446f 2022-05-09 op amused toggle
41 79ed446f 2022-05-09 op case s
42 79ed446f 2022-05-09 op amused stop
43 79ed446f 2022-05-09 op case q
44 79ed446f 2022-05-09 op run=0
45 79ed446f 2022-05-09 op case *
46 0e4b4d70 2022-05-09 op printf ' unknown keybinding >'$t'<\r'
47 79ed446f 2022-05-09 op }
48 79ed446f 2022-05-09 op }
49 79ed446f 2022-05-09 op }
50 79ed446f 2022-05-09 op
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.
54 79ed446f 2022-05-09 op
55 79ed446f 2022-05-09 op fn ui {
56 79ed446f 2022-05-09 op clear
57 79ed446f 2022-05-09 op amused show -p | grep -A3 -B3 '^>' | awk '{printf "%s\r\n", $0}'
58 79ed446f 2022-05-09 op
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.
62 79ed446f 2022-05-09 op
63 79ed446f 2022-05-09 op }
64 79ed446f 2022-05-09 op
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
67 79ed446f 2022-05-09 op
68 79ed446f 2022-05-09 op fn uiloop {
69 fed82e2b 2022-05-14 op amused monitor next,prev,jump | {
70 79ed446f 2022-05-09 op while (read) {
71 79ed446f 2022-05-09 op ui
72 79ed446f 2022-05-09 op }
73 79ed446f 2022-05-09 op
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.)
76 79ed446f 2022-05-09 op
77 79ed446f 2022-05-09 op }
78 79ed446f 2022-05-09 op }
79 79ed446f 2022-05-09 op
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!
82 79ed446f 2022-05-09 op
83 79ed446f 2022-05-09 op ui
84 79ed446f 2022-05-09 op uiloop &
85 79ed446f 2022-05-09 op uipid=$apid
86 79ed446f 2022-05-09 op input
87 79ed446f 2022-05-09 op kill -INT -$uipid
88 79ed446f 2022-05-09 op
89 79ed446f 2022-05-09 op
90 79ed446f 2022-05-09 op
91 79ed446f 2022-05-09 op