forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.go
More file actions
402 lines (337 loc) · 10.6 KB
/
gui.go
File metadata and controls
402 lines (337 loc) · 10.6 KB
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
397
398
399
400
401
package main
import (
"fmt"
"math"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/yourok/go-mpv/mpv"
)
type Entity struct {
song *SubsonicSong
album *SubsonicAlbum
}
/// struct contains all the updatable elements of the Ui
type Ui struct {
app *tview.Application
entityList *tview.List
queueList *tview.List
startStopStatus *tview.TextView
playerStatus *tview.TextView
artistIdList []string
connection *SubsonicConnection
player *Player
currentEntities []*Entity
}
func handleArtistSelected(artistId string, ui *Ui) {
// TODO handle error here
response, _ := ui.connection.GetArtist(artistId)
ui.entityList.Clear()
ui.currentEntities = []*Entity{}
for _, album := range response.Artist.Albums {
albumCopy := album
ui.currentEntities = append(ui.currentEntities, &Entity{song: nil, album: &albumCopy})
var title string
var handler func()
title = tview.Escape("[" + album.Title + "]")
handler = makeAlbumHandler(album.Id, ui)
ui.entityList.AddItem(title, "", 0, handler)
}
}
func handleAlbumSelected(albumId string, ui *Ui) {
response, _ := ui.connection.GetAlbum(albumId)
ui.entityList.Clear()
ui.entityList.AddItem(tview.Escape("[..]"), "", 0, makeArtistHandler(response.Album.ArtistId, ui))
ui.currentEntities = []*Entity{}
ui.currentEntities = append(ui.currentEntities, &Entity{nil, nil})
for _, song := range response.Album.Songs {
songCopy := song
ui.currentEntities = append(ui.currentEntities, &Entity{song: &songCopy, album: nil})
var title string
var handler func()
title = song.Title
handler = makeSongHandler(song, title, song.Artist, song.Duration, ui.player, ui.queueList, ui)
ui.entityList.AddItem(title, "", 0, handler)
}
}
func handleDeleteFromQueue(ui *Ui) {
currentIndex := ui.queueList.GetCurrentItem()
queue := ui.player.Queue
if currentIndex < 0 || len(ui.player.Queue) < currentIndex {
return
}
// if the deleted item was the first one, and the player is loaded
// remove the track. Removing the track auto starts the next one
if currentIndex == 0 && ui.player.IsSongLoaded() {
ui.player.Stop()
return
}
// remove the item from the queue
if len(ui.player.Queue) > 1 {
ui.player.Queue = append(queue[:currentIndex], queue[currentIndex+1:]...)
} else {
ui.player.Queue = nil
}
updateQueueList(ui.player, ui.queueList)
}
func handleAddSongAlbumToQueue(ui *Ui) {
currentIndex := ui.entityList.GetCurrentItem()
if currentIndex < 0 || len(ui.currentEntities) < currentIndex {
return
}
entity := ui.currentEntities[currentIndex]
if entity.song == nil && entity.album == nil {
return
}
if entity.album != nil {
addAlbumToQueue(entity.album, ui)
} else if entity.song != nil {
addSongToQueue(entity.song, ui)
}
updateQueueList(ui.player, ui.queueList)
}
func addAlbumToQueue(album *SubsonicAlbum, ui *Ui) {
response, _ := ui.connection.GetAlbum(album.Id)
for _, e := range response.Album.Songs {
addSongToQueue(&e, ui)
}
}
func addSongToQueue(song *SubsonicSong, ui *Ui) {
uri := ui.connection.GetPlayUrl(song)
queueItem := QueueItem{
uri,
song.Title,
song.Artist,
song.Duration,
}
ui.player.Queue = append(ui.player.Queue, queueItem)
}
func makeSongHandler(song SubsonicSong, title string, artist string, duration int, player *Player,
queueList *tview.List, ui *Ui) func() {
return func() {
var uri string = ui.connection.GetPlayUrl(&song)
player.Play(uri, title, artist, duration)
updateQueueList(player, queueList)
}
}
func makeArtistHandler(artistId string, ui *Ui) func() {
return func() {
handleArtistSelected(artistId, ui)
}
}
func makeAlbumHandler(albumId string, ui *Ui) func() {
return func() {
handleAlbumSelected(albumId, ui)
}
}
func InitGui(indexes *[]SubsonicIndex, connection *SubsonicConnection) *Ui {
app := tview.NewApplication()
entityList := tview.NewList().ShowSecondaryText(false).
SetSelectedFocusOnly(true)
queueList := tview.NewList().ShowSecondaryText(false)
startStopStatus := tview.NewTextView().SetText("[::b]stmp: [red]stopped").
SetTextAlign(tview.AlignLeft).
SetDynamicColors(true)
playerStatus := tview.NewTextView().SetText("[::b][100%][0:00/0:00]").
SetTextAlign(tview.AlignRight).
SetDynamicColors(true)
player, err := InitPlayer()
var artistIdList []string
var currentEntities []*Entity
ui := Ui{
app,
entityList,
queueList,
startStopStatus,
playerStatus,
artistIdList,
connection,
player,
currentEntities,
}
if err != nil {
app.Stop()
fmt.Println("Unable to initialize mpv. Is mpv installed?")
}
//title row flex
titleFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(startStopStatus, 0, 1, false).
AddItem(playerStatus, 0, 1, false)
// artist list, used to map the index of
artistList := tview.NewList().ShowSecondaryText(false)
for _, index := range *indexes {
for _, artist := range index.Artists {
artistList.AddItem(artist.Name, "", 0, nil)
artistIdList = append(artistIdList, artist.Id)
}
}
artistFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(artistList, 0, 1, true).
AddItem(entityList, 0, 1, false)
browserFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(titleFlex, 1, 0, false).
AddItem(artistFlex, 0, 1, true)
// going right from the artist list should focus the album/song list
artistList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyRight {
app.SetFocus(entityList)
return nil
}
return event
})
entityList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyLeft {
app.SetFocus(artistList)
return nil
}
if event.Rune() == 'a' {
handleAddSongAlbumToQueue(&ui)
return nil
}
return event
})
queueList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyDelete || event.Rune() == 'd' {
handleDeleteFromQueue(&ui)
return nil
}
return event
})
artistList.SetChangedFunc(func(index int, _ string, _ string, _ rune) {
handleArtistSelected(artistIdList[index], &ui)
})
queueFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(titleFlex, 1, 0, false).
AddItem(queueList, 0, 1, true)
// handle
go handleMpvEvents(&ui)
pages := tview.NewPages().
AddPage("browser", browserFlex, true, true).
AddPage("queue", queueFlex, true, false)
pages.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == '1' {
pages.SwitchToPage("browser")
return nil
}
if event.Rune() == '2' {
pages.SwitchToPage("queue")
return nil
}
if event.Rune() == 'q' {
player.EventChannel <- nil
player.Instance.TerminateDestroy()
app.Stop()
}
if event.Rune() == 'D' {
player.Queue = nil
player.Stop()
updateQueueList(player, queueList)
}
if event.Rune() == 'r' {
addRandomSongsToQueue(&ui)
}
if event.Rune() == 'p' {
status := player.Pause()
if status == PlayerStopped {
startStopStatus.SetText("[::b]stmp: [red]stopped")
} else if status == PlayerPlaying {
startStopStatus.SetText("[::b]stmp: [green]playing " + player.Queue[0].Title)
} else if status == PlayerPaused {
startStopStatus.SetText("[::b]stmp: [yellow]paused")
}
return nil
}
if event.Rune() == '-' {
player.AdjustVolume(-5)
return nil
}
if event.Rune() == '=' {
player.AdjustVolume(5)
return nil
}
return event
})
if err := app.SetRoot(pages, true).SetFocus(pages).EnableMouse(true).Run(); err != nil {
panic(err)
}
return &ui
}
func updateQueueList(player *Player, queueList *tview.List) {
queueList.Clear()
for _, queueItem := range player.Queue {
min, sec := iSecondsToMinAndSec(queueItem.Duration)
queueList.AddItem(fmt.Sprintf("%s - %s - %02d:%02d", queueItem.Title, queueItem.Artist, min, sec), "", 0, nil)
}
}
func addRandomSongsToQueue(ui *Ui) {
response, _ := ui.connection.GetRandomSongs(20)
for _, song := range response.RandomSongs.Songs {
addSongToQueue(&song, ui)
}
updateQueueList(ui.player, ui.queueList)
}
func handleMpvEvents(ui *Ui) {
for {
e := <-ui.player.EventChannel
if e == nil {
break
// we don't want to update anything if we're in the process of replacing the current track
} else if e.Event_Id == mpv.EVENT_END_FILE && !ui.player.ReplaceInProgress {
ui.startStopStatus.SetText("[::b]stmp: [red]stopped")
// TODO it's gross that this is here, need better event handling
if len(ui.player.Queue) > 0 {
ui.player.Queue = ui.player.Queue[1:]
}
updateQueueList(ui.player, ui.queueList)
ui.player.PlayNextTrack()
} else if e.Event_Id == mpv.EVENT_START_FILE {
ui.player.ReplaceInProgress = false
ui.startStopStatus.SetText("[::b]stmp: [green]playing " + ui.player.Queue[0].Title)
updateQueueList(ui.player, ui.queueList)
}
// TODO how to handle mpv errors here?
position, _ := ui.player.Instance.GetProperty("time-pos", mpv.FORMAT_DOUBLE)
// TODO only update these as needed
duration, _ := ui.player.Instance.GetProperty("duration", mpv.FORMAT_DOUBLE)
volume, _ := ui.player.Instance.GetProperty("volume", mpv.FORMAT_INT64)
if position == nil {
position = 0.0
}
if duration == nil {
duration = 0.0
}
if volume == nil {
volume = 0
}
ui.playerStatus.SetText(formatPlayerStatus(volume.(int64), position.(float64), duration.(float64)))
ui.app.Draw()
}
}
func formatPlayerStatus(volume int64, position float64, duration float64) string {
if position < 0 {
position = 0.0
}
if duration < 0 {
duration = 0.0
}
positionMin, positionSec := secondsToMinAndSec(position)
durationMin, durationSec := secondsToMinAndSec(duration)
return fmt.Sprintf("[::b][%d%%][%02d:%02d/%02d:%02d]", volume,
positionMin, positionSec, durationMin, durationSec)
}
func secondsToMinAndSec(seconds float64) (int, int) {
minutes := math.Floor(seconds / 60)
remainingSeconds := int(seconds) % 60
return int(minutes), remainingSeconds
}
func iSecondsToMinAndSec(seconds int) (int, int) {
minutes := seconds / 60
remainingSeconds := seconds % 60
return minutes, remainingSeconds
}
/// if the first argument isn't empty, return it, otherwise return the second
func stringOr(firstChoice string, secondChoice string) string {
if firstChoice != "" {
return firstChoice
}
return secondChoice
}