-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
244 lines (226 loc) · 5.61 KB
/
main.go
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
package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
"os"
"time"
)
const mpdAddr = "localhost:6600"
type state struct {
mpdState mpdState // play, stop or pause
// elapsed time of the current song in seconds;
// nil when stopped:
elapsed *float32
// songid of the currently playing or paused song;
// nil when stopped:
songID *int
highlighted int // index of the highlighted song in the queue
queue []song
}
type song struct {
songID int
duration float32
displayName string
}
type event int
const (
updateStateEvent event = iota
playHighlightedEvent
togglePauseEvent
deleteHighlightedEvent
clearEvent
highlightPrevEvent
highlightNextEvent
seekBackwardsEvent
seekForwardsEvent
movePrevEvent
moveNextEvent
redrawEvent
quitEvent
)
func main() {
if len(os.Args) > 1 {
fmt.Println(keyBindingInfo)
os.Exit(0)
}
screen, err := initTcell()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not initialize tcell: %v\n", err)
os.Exit(1)
}
state, err := getState()
if err != nil {
screen.Fini()
fmt.Fprintf(os.Stderr, "Could not get mpd state: %v\n", err)
os.Exit(1)
}
events := make(chan event)
go handleTcellEvents(screen, events)
go handleMpdEvents(events)
if err = runEventLoop(state, screen, events); err != nil {
screen.Fini()
fmt.Fprintf(os.Stderr, "Error during execution: %v\n", err)
os.Exit(1)
}
screen.Fini()
}
func runEventLoop(state state, screen tcell.Screen, events chan event) error {
var err error
updateElapsedTicker := time.NewTicker(time.Second)
for {
select {
case event := <-events:
switch event {
case updateStateEvent:
oldHighlighted := state.highlighted
if state, err = getState(); err != nil {
return fmt.Errorf("could not get mpd state: %v", err)
}
state.highlighted = oldHighlighted
if state.highlighted < 0 {
state.highlighted = 0
}
if state.highlighted >= len(state.queue) {
state.highlighted = len(state.queue) - 1
}
draw(state, screen)
case playHighlightedEvent:
if err = playHighlighted(state); err != nil {
return err
}
case togglePauseEvent:
if err = togglePause(state); err != nil {
return err
}
case deleteHighlightedEvent:
if err = deleteHighlighted(state); err != nil {
return err
}
case clearEvent:
if err = clear(state); err != nil {
return err
}
case highlightPrevEvent:
if state.highlighted > 0 {
state.highlighted -= 1
draw(state, screen)
}
case highlightNextEvent:
if state.highlighted < len(state.queue)-1 {
state.highlighted += 1
draw(state, screen)
}
case movePrevEvent:
if err = moveHighlightedUpwards(&state); err != nil {
return err
}
case moveNextEvent:
if err = moveHighlightedDownwards(&state); err != nil {
return err
}
case seekBackwardsEvent:
if err = seekBackwards(state, 5); err != nil {
return err
}
case seekForwardsEvent:
if err = seekForwards(state, 5); err != nil {
return err
}
case redrawEvent:
draw(state, screen)
case quitEvent:
return nil
}
case <-updateElapsedTicker.C:
if updated := updateElapsed(&state); updated {
draw(state, screen)
}
}
}
}
func updateElapsed(state *state) bool {
if state.mpdState == playMPDState {
*state.elapsed += 1
return true
}
return false
}
/*
How it could look:
Paused at 01:18 / 02:15
03:43 [#4 of Meddle] Pink Floyd - San Tropez
> 02:15 [#5 of Meddle] Pink Floyd - Seamus
23:35 [#6 of Meddle] Pink Floyd - Echoes
*/
func draw(state state, screen tcell.Screen) {
screen.Clear()
emitStr(screen, 0, 0, tcell.StyleDefault, getTopbar(state))
cropQueueIfTooLong(&state, screen)
for i, s := range state.queue {
if state.songID != nil && *state.songID == s.songID {
emitStr(screen, 0, i+1, tcell.StyleDefault, ">")
}
dur := fmt.Sprintf("%02d:%02d ", int(s.duration)/60, int(s.duration)%60)
if i == state.highlighted {
emitStr(screen, 2, i+1, tcell.StyleDefault.Reverse(true), dur+s.displayName)
} else {
emitStr(screen, 2, i+1, tcell.StyleDefault, dur+s.displayName)
}
}
screen.Show()
}
func getTopbar(state state) string {
switch state.mpdState {
case playMPDState:
currentSong, err := getCurrentSong(state)
if err != nil {
return "playing"
}
return fmt.Sprintf("playing at %02d:%02d / %02d:%02d",
int(*state.elapsed)/60,
int(*state.elapsed)%60,
int(currentSong.duration)/60,
int(currentSong.duration)%60)
case pauseMPDState:
currentSong, err := getCurrentSong(state)
if err != nil {
return "paused"
}
return fmt.Sprintf("paused at %02d:%02d / %02d:%02d",
int(*state.elapsed)/60,
int(*state.elapsed)%60,
int(currentSong.duration)/60,
int(currentSong.duration)%60)
}
return "stopped"
}
// cropQueueIfTooLong will remove songs from state.queue, if the screen
// is not high enough to fit all of them. The function ensures that the
// highlighted song is alway visible.
//
// state.highlighted also will be adapted to point to the same song
// after cropping.
func cropQueueIfTooLong(state *state, screen tcell.Screen) {
if _, h := screen.Size(); h <= len(state.queue) {
newQueueLen := h - 1
start := state.highlighted - ((newQueueLen - 1) / 2)
end := start + newQueueLen
if start < 0 {
start = 0
end = newQueueLen
} else if end > len(state.queue) {
end = len(state.queue)
start = end - newQueueLen
}
state.queue = state.queue[start:end]
state.highlighted -= start
}
}
func getCurrentSong(state state) (song, error) {
for _, s := range state.queue {
if s.songID == *state.songID {
return s, nil
}
}
return song{}, fmt.Errorf("currently playing song not in queue")
}