package views import ( "fmt" "time" "git.dayanhub.com/sagi/subsonic-tui/internal/client" "github.com/delucks/go-subsonic" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) var _ View = &player{} type player struct { client *client.Client view tview.Primitive grid *tview.Grid artwork *tview.Image progress *tview.Flex songInfo *tview.TextView song *subsonic.Child } func NewPlayer(client *client.Client) *player { grid := tview.NewGrid().SetColumns(9, 0) //album art art := tview.NewImage() grid.AddItem(art, 0, 0, 1, 1, 4, 4, false) // Progress progress := tview.NewFlex() // Song songInfo songInfo := tview.NewTextView() songInfo.SetDynamicColors(true) // Info + Progress songProg := tview.NewFlex().SetDirection(tview.FlexRow) songProg.AddItem(songInfo, 0, 4, false) songProg.AddItem(progress, 0, 1, false) grid.AddItem(songProg, 0, 1, 1, 1, 0, 0, false) player := &player{ client: client, view: grid, artwork: art, songInfo: songInfo, grid: grid, progress: progress, } player.SetSongInfo(&subsonic.Child{ Title: "Subsonic TUI", Album: "MaVeZe", Artist: "ZeGoomba", CoverArt: "", Duration: 0, }) player.UpdateProgress(time.Duration(0)) return player } func (p *player) SetSongInfo(song *subsonic.Child) { info := fmt.Sprintf("[yellow]Title:[white] %s\n[yellow]Album:[white] %s\n[yellow]Atrists:[white] %s", song.Title, song.Album, song.Artist) p.songInfo.SetText(info) p.song = song p.LoadAlbumArt(song.CoverArt) } func (p *player) LoadAlbumArt(ID string) { i, _ := p.client.GetCoverArt(ID) p.artwork.SetImage(i) } func (p *player) UpdateProgress(elapsed time.Duration) { if p.song.Duration == 0 { // Startup... Show version number versionInfo := tview.NewTextView().SetText("Version: 0.1") versionInfo.SetBackgroundColor(tcell.ColorDarkGray) versionInfo.SetTextColor(tcell.ColorDarkRed) p.progress.AddItem(versionInfo, 0, 1, false) return } songDuration := time.Duration(p.song.Duration) * time.Second overlappedBox := tview.NewTextView() overlappedBox.SetBackgroundColor(tcell.ColorDarkRed) overlappedBox.SetTextColor(tcell.ColorDarkGray) overlappedBox.SetText(songDuration.String()) remainingBox := tview.NewTextView() remainingBox.SetBackgroundColor(tcell.ColorDarkGray) remainingBox.SetTextColor(tcell.ColorDarkRed) remainingBox.SetTextAlign(tview.AlignRight) rm := time.Duration(songDuration.Seconds()-elapsed.Seconds()) * time.Second remaining := fmt.Sprintf("-%s", rm.String()) remainingBox.SetText(remaining) p.progress.Clear() p.progress.AddItem(overlappedBox, 0, int(elapsed.Seconds()), false) p.progress.AddItem(remainingBox, 0, int(songDuration.Seconds())-int(elapsed.Seconds()), false) } func (p *player) GetView() tview.Primitive { return p.view } func (p *player) Update() { //p.UpdateProgress("00:00", 50) }