Blob


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