Sagi Dayan
92e3c4ea7d
All checks were successful
ci / check for spelling errors (pull_request) Successful in 21s
ci / code quality (lint/tests) (pull_request) Successful in 2m29s
ci / Make sure build does not fail (pull_request) Successful in 2m25s
ci / notify-fail (pull_request) Has been skipped
ci / check for spelling errors (push) Successful in 22s
ci / code quality (lint/tests) (push) Successful in 1m53s
ci / Make sure build does not fail (push) Successful in 2m55s
ci / notify-fail (push) Has been skipped
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
84 lines
2 KiB
Go
84 lines
2 KiB
Go
package views
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/config"
|
|
"github.com/delucks/go-subsonic"
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
type queue struct {
|
|
view *tview.Table
|
|
playFunc func(index int)
|
|
currentSong int
|
|
songQueue []*subsonic.Child
|
|
}
|
|
|
|
func NewQueue() *queue {
|
|
table := tview.NewTable()
|
|
table.SetBackgroundColor(config.ColorBackground)
|
|
table.SetTitle("Queue [4]")
|
|
table.SetBorder(true)
|
|
table.SetFocusFunc(func() {
|
|
table.SetBorderColor(config.ColorSelectedBoarder)
|
|
})
|
|
table.SetBlurFunc(func() {
|
|
table.SetBorderColor(config.ColorBluredBoarder)
|
|
})
|
|
|
|
return &queue{
|
|
view: table,
|
|
currentSong: 0,
|
|
}
|
|
}
|
|
|
|
func (q *queue) SetPlayFunc(f func(index int)) {
|
|
q.playFunc = f
|
|
}
|
|
|
|
func (q *queue) drawQueue() {
|
|
q.view.Clear()
|
|
list := q.view
|
|
list.SetWrapSelection(true, false)
|
|
list.SetSelectable(true, false)
|
|
|
|
for i, song := range q.songQueue {
|
|
isCurrentSong := q.currentSong == i
|
|
isPlayed := i < q.currentSong
|
|
bgColor := config.ColorBackground
|
|
|
|
if isCurrentSong {
|
|
bgColor = config.ColorQueuePlayingBg
|
|
} else if isPlayed {
|
|
bgColor = config.ColorQueuePlayedBg
|
|
}
|
|
|
|
num := tview.NewTableCell(strconv.Itoa(i + 1)).SetTextColor(config.ColorTextAccent).SetBackgroundColor(bgColor)
|
|
title := tview.NewTableCell(song.Title).SetMaxWidth(15).SetExpansion(2).SetBackgroundColor(bgColor)
|
|
artist := tview.NewTableCell(song.Artist).SetMaxWidth(15).SetExpansion(1).SetBackgroundColor(bgColor)
|
|
d := time.Second * time.Duration(song.Duration)
|
|
duration := tview.NewTableCell(d.String()).SetAlign(tview.AlignRight).SetExpansion(1).SetBackgroundColor(bgColor)
|
|
|
|
list.SetCell(i, 0, num)
|
|
list.SetCell(i, 1, title)
|
|
list.SetCell(i, 2, artist)
|
|
list.SetCell(i, 3, duration)
|
|
}
|
|
|
|
list.SetSelectedFunc(func(row, column int) {
|
|
q.currentSong = row
|
|
q.playFunc(row)
|
|
})
|
|
}
|
|
|
|
func (q *queue) Update(songs []*subsonic.Child, currentSong int) {
|
|
q.songQueue = songs
|
|
q.currentSong = currentSong
|
|
q.drawQueue()
|
|
}
|
|
|
|
func (q *queue) GetView() tview.Primitive {
|
|
return q.view
|
|
}
|