subsonic-tui/internal/tui/views/queue.go
Sagi Dayan 48661005be
initial commit
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-09-01 16:30:56 +03:00

81 lines
2 KiB
Go

package views
import (
"fmt"
"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(fmt.Sprintf("%d", 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
}