forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
page_queue.go
477 lines (413 loc) · 12.4 KB
/
page_queue.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2023 The STMPS Authors
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"bytes"
_ "embed"
"errors"
"fmt"
"image"
"image/png"
"os"
"text/template"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spezifisch/stmps/logger"
"github.com/spezifisch/stmps/mpvplayer"
"github.com/spezifisch/stmps/subsonic"
)
// TODO show total # of entries somewhere (top?)
// columns: star, title, artist, duration
const queueDataColumns = 4
const starIcon = "♥"
// data for rendering queue table
type queueData struct {
tview.TableContentReadOnly
// our copy of the queue
playerQueue mpvplayer.PlayerQueue
// we also need to know which elements are starred
starIdList map[string]struct{}
}
var _ tview.TableContent = (*queueData)(nil)
type QueuePage struct {
Root *tview.Flex
queueList *tview.Table
queueData queueData
songInfo *tview.TextView
coverArt *tview.Image
// external refs
ui *Ui
logger logger.LoggerInterface
songInfoTemplate *template.Template
}
var STMPS_LOGO image.Image
// init sets up the default image used for songs for which the server provides
// no cover art.
func init() {
var err error
STMPS_LOGO, err = png.Decode(bytes.NewReader(_stmps_logo))
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
}
}
func (ui *Ui) createQueuePage() *QueuePage {
tmpl := template.New("song info").Funcs(template.FuncMap{
"formatTime": func(i int) string {
return (time.Duration(i) * time.Second).String()
},
})
songInfoTemplate, err := tmpl.Parse(songInfoTemplateString)
if err != nil {
ui.logger.PrintError("createQueuePage", err)
}
queuePage := QueuePage{
ui: ui,
logger: ui.logger,
songInfoTemplate: songInfoTemplate,
}
// main table
queuePage.queueList = tview.NewTable().
SetSelectable(true, false). // rows selectable
SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorLightGray).Foreground(tcell.ColorBlack))
queuePage.queueList.Box.
SetTitle(" queue ").
SetTitleAlign(tview.AlignLeft).
SetBorder(true)
queuePage.queueList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyDelete || event.Rune() == 'd' {
queuePage.handleDeleteFromQueue()
} else {
switch event.Rune() {
case 'y':
queuePage.handleToggleStar()
case 'j':
queuePage.moveSongDown()
case 'k':
queuePage.moveSongUp()
case 's':
if len(queuePage.queueData.playerQueue) == 0 {
queuePage.logger.Print("no items in queue to save")
return nil
}
queuePage.ui.ShowSelectPlaylist()
case 'S':
queuePage.shuffle()
case 'l':
go func() {
ssr, err := queuePage.ui.connection.LoadPlayQueue()
if err != nil {
queuePage.logger.Printf("unable to load play queue from server: %s", err)
return
}
queuePage.queueList.Clear()
queuePage.queueData.Clear()
if ssr.PlayQueue.Entries != nil {
for _, ent := range ssr.PlayQueue.Entries {
ui.addSongToQueue(&ent)
}
ui.queuePage.UpdateQueue()
if err := ui.player.Play(); err != nil {
queuePage.logger.Printf("error playing: %s", err)
}
if err = ui.player.Seek(ssr.PlayQueue.Position); err != nil {
queuePage.logger.Printf("unable to seek to position %s: %s", time.Duration(ssr.PlayQueue.Position)*time.Second, err)
}
_ = ui.player.Pause()
}
}()
default:
return event
}
}
return nil
})
// Song info
queuePage.songInfo = tview.NewTextView()
queuePage.songInfo.SetDynamicColors(true).SetScrollable(true)
queuePage.songInfo.SetMouseCapture(func(action tview.MouseAction, event *tcell.EventMouse) (tview.MouseAction, *tcell.EventMouse) {
return action, nil
})
queuePage.queueList.SetSelectionChangedFunc(queuePage.changeSelection)
queuePage.coverArt = tview.NewImage()
queuePage.coverArt.SetImage(STMPS_LOGO)
infoFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(queuePage.songInfo, 0, 1, false).
AddItem(queuePage.coverArt, 0, 1, false)
infoFlex.SetBorder(true)
infoFlex.SetTitle(" song info ")
// flex wrapper
queuePage.Root = tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(queuePage.queueList, 0, 2, true).
AddItem(infoFlex, 0, 1, false)
// private data
queuePage.queueData = queueData{
starIdList: ui.starIdList,
}
return &queuePage
}
func (q *QueuePage) changeSelection(row, column int) {
q.songInfo.Clear()
if row >= len(q.queueData.playerQueue) || row < 0 || column < 0 {
q.coverArt.SetImage(STMPS_LOGO)
return
}
currentSong := q.queueData.playerQueue[row]
art := STMPS_LOGO
if currentSong.CoverArtId != "" {
if nart, err := q.ui.connection.GetCoverArt(currentSong.CoverArtId); err == nil {
if nart != nil {
art = nart
} else {
q.logger.Printf("%q cover art %s was unexpectedly nil", currentSong.Title, currentSong.CoverArtId)
}
} else {
q.logger.Printf("error fetching cover art for %s: %v", currentSong.Title, err)
}
}
q.coverArt.SetImage(art)
_ = q.songInfoTemplate.Execute(q.songInfo, currentSong)
}
func (q *QueuePage) UpdateQueue() {
q.updateQueue()
}
func (q *QueuePage) getSelectedItem() (index int, err error) {
index, _ = q.queueList.GetSelection()
if index < 0 {
err = errors.New("invalid index")
return
}
return
}
// button handler
func (q *QueuePage) handleDeleteFromQueue() {
currentIndex, err := q.getSelectedItem()
if err != nil {
return
}
// remove the item from the queue
q.ui.player.DeleteQueueItem(currentIndex)
q.updateQueue()
}
// button handler
func (q *QueuePage) handleToggleStar() {
starIdList := q.queueData.starIdList
currentIndex, err := q.getSelectedItem()
if err != nil {
q.logger.PrintError("handleToggleStar", err)
return
}
entity, err := q.ui.player.GetQueueItem(currentIndex)
if err != nil {
q.logger.PrintError("handleToggleStar", err)
return
}
// If the song is already in the star list, remove it
_, remove := starIdList[entity.Id]
// update on server
if _, err = q.ui.connection.ToggleStar(entity.Id, starIdList); err != nil {
q.ui.showMessageBox("ToggleStar failed")
return // fail, assume not toggled
}
if remove {
delete(starIdList, entity.Id)
} else {
starIdList[entity.Id] = struct{}{}
}
q.ui.browserPage.UpdateStars()
}
// re-read queue data from mpvplayer which is the authoritative source for the queue
func (q *QueuePage) updateQueue() {
queueWasEmpty := len(q.queueData.playerQueue) == 0
// tell tview table to update its data
q.queueData.playerQueue = q.ui.player.GetQueueCopy()
q.queueList.SetContent(&q.queueData)
// by default we're scrolled down after initially adding rows, fix this
if queueWasEmpty {
q.queueList.ScrollToBeginning()
}
r, c := q.queueList.GetSelection()
q.changeSelection(r, c)
}
// moveSongUp moves the currently selected song up in the queue
// If the selected song isn't the third or higher, this is a NOP
// and no error is reported.
func (q *QueuePage) moveSongUp() {
if len(q.queueData.playerQueue) == 0 {
return
}
currentIndex, column := q.queueList.GetSelection()
if currentIndex < 0 || column < 0 {
q.logger.Printf("moveSongUp: invalid selection (%d, %d)", currentIndex, column)
return
}
if currentIndex == 0 {
return
}
if currentIndex == 1 {
// An error here won't affect re-arranging the queue.
_ = q.ui.player.Stop()
}
// remove the item from the queue
q.ui.player.MoveSongUp(currentIndex)
q.queueList.Select(currentIndex-1, column)
q.updateQueue()
}
// moveSongUp moves the currently selected song up in the queue
// If the selected song is not the second-to-the-last or lower, this is a NOP,
// and no error is reported
func (q *QueuePage) moveSongDown() {
queueLen := len(q.queueData.playerQueue)
if queueLen == 0 {
return
}
currentIndex, column := q.queueList.GetSelection()
if currentIndex < 0 || column < 0 {
q.logger.Printf("moveSongDown: invalid selection (%d, %d)", currentIndex, column)
return
}
if currentIndex == 0 {
// An error here won't affect re-arranging the queue.
_ = q.ui.player.Stop()
}
if currentIndex > queueLen-2 {
q.logger.Printf("moveSongDown: can't move last song")
return
}
// remove the item from the queue
q.ui.player.MoveSongDown(currentIndex)
q.queueList.Select(currentIndex+1, column)
q.updateQueue()
}
// saveQueue persists the current queue as a playlist. It presents the user
// with a way of choosing the playlist name, and if a playlist with the
// same name already exists it requires the user to confirm that they
// want to overwrite the existing playlist.
//
// Errors are reported to the user and require confirmation to dismiss,
// and logged.
func (q *QueuePage) saveQueue(playlistName string) {
// When updating an existing playlist, there are two options:
// updatePlaylist, and createPlaylist. createPlaylist on an
// existing playlist is a replace function.
//
// updatePlaylist is more surgical: it can selectively add and
// remove songs, and update playlist attributes. It is more
// network efficient than using createPlaylist to change an
// existing playlist. However, using it here would require
// a more complex diffing algorithm, and much more code.
// Consequently, this version of save() uses the more simple
// brute-force approach of always using createPlaylist().
songIds := make([]string, len(q.queueData.playerQueue))
for i, it := range q.queueData.playerQueue {
songIds[i] = it.Id
}
var playlistId string
for _, p := range q.ui.playlists {
if p.Name == playlistName {
playlistId = string(p.Id)
break
}
}
var response *subsonic.SubsonicResponse
var err error
if playlistId == "" {
q.logger.Printf("Saving %d items to playlist %s", len(q.queueData.playerQueue), playlistName)
response, err = q.ui.connection.CreatePlaylist("", playlistName, songIds)
} else {
q.logger.Printf("Replacing playlist %s with %d", playlistId, len(q.queueData.playerQueue))
response, err = q.ui.connection.CreatePlaylist(playlistId, "", songIds)
}
if err != nil {
message := fmt.Sprintf("Error saving queue: %s", err)
q.ui.showMessageBox(message)
q.logger.Print(message)
} else {
if playlistId != "" {
for i, pl := range q.ui.playlists {
if string(pl.Id) == playlistId {
q.ui.playlists[i] = response.Playlist
break
}
}
} else {
q.ui.playlistPage.addPlaylist(response.Playlist)
q.ui.playlists = append(q.ui.playlists, response.Playlist)
}
q.ui.playlistPage.handlePlaylistSelected(response.Playlist)
}
}
// shuffle randomly shuffles entries in the queue, updates it, and moves
// the selected-item to the new first entry.
func (q *QueuePage) shuffle() {
if len(q.queueData.playerQueue) == 0 {
return
}
// An error here won't affect re-arranging the queue.
_ = q.ui.player.Stop()
q.ui.player.Shuffle()
q.queueList.Select(0, 0)
q.updateQueue()
}
// queueData methods, used by tview to lazily render the table
func (q *queueData) GetCell(row, column int) *tview.TableCell {
if row >= len(q.playerQueue) || column >= queueDataColumns || row < 0 || column < 0 {
return nil
}
song := q.playerQueue[row]
switch column {
case 0: // star
text := " "
color := tcell.ColorDefault
if _, starred := q.starIdList[song.Id]; starred {
text = starIcon
color = tcell.ColorRed
}
return &tview.TableCell{
Text: text,
Color: color,
Expansion: 0,
MaxWidth: 1,
Transparent: true,
}
case 1: // title
return &tview.TableCell{
Text: tview.Escape(song.Title),
Expansion: 1,
Transparent: true,
}
case 2: // artist
return &tview.TableCell{
Text: tview.Escape(song.Artist),
Expansion: 1,
Transparent: true,
}
case 3: // duration
min, sec := iSecondsToMinAndSec(song.Duration)
text := fmt.Sprintf("%3d:%02d", min, sec)
return &tview.TableCell{
Text: text,
Align: tview.AlignRight,
Expansion: 0,
MaxWidth: 6,
Transparent: true,
}
}
return nil
}
// Return the total number of rows in the table.
func (q *queueData) GetRowCount() int {
return len(q.playerQueue)
}
// Return the total number of columns in the table.
func (q *queueData) GetColumnCount() int {
return queueDataColumns
}
var songInfoTemplateString = `[blue::b]Title:[-:-:-:-] [green::i]{{.Title}}[-:-:-:-] [yellow::i]({{formatTime .Duration}})[-:-:-:-]
[blue::b]Artist:[-:-:-:-] [::i]{{.Artist}}[-:-:-:-]
[blue::b]Album:[-:-:-:-] [::i]{{.GetAlbum}}[-:-:-:-]
[blue::b]Disc:[-:-:-:-] [::i]{{.GetDiscNumber}}[-:-:-:-] [blue::b]Track:[-:-:-:-] [::i]{{.GetTrackNumber}}[-:-:-:-]
[blue::b]Year:[-:-:-:-] [::i]{{.GetYear}}[-:-:-:-]
`
//go:embed docs/stmps_logo.png
var _stmps_logo []byte