package views import ( "fmt" "time" "git.dayanhub.com/sagi/subsonic-tui/internal/client" "git.dayanhub.com/sagi/subsonic-tui/internal/config" "github.com/delucks/go-subsonic" "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) grid.SetBackgroundColor(config.ColorBackground) //album art art := tview.NewImage() art.SetBackgroundColor(config.ColorBackground) grid.AddItem(art, 0, 0, 1, 1, 4, 4, false) // Progress progress := tview.NewFlex() progress.SetBackgroundColor(config.ColorBackground) // Song songInfo songInfo := tview.NewTextView() songInfo.SetDynamicColors(true) songInfo.SetBackgroundColor(config.ColorBackground) // 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(config.ColorPlaybackProgressRemaining) versionInfo.SetTextColor(config.ColorPlaybackProgressElapsed) p.progress.AddItem(versionInfo, 0, 1, false) return } songDuration := time.Duration(p.song.Duration) * time.Second overlappedBox := tview.NewTextView() overlappedBox.SetBackgroundColor(config.ColorPlaybackProgressElapsed) overlappedBox.SetTextColor(config.ColorPlaybackProgressRemaining) overlappedBox.SetText(songDuration.String()) remainingBox := tview.NewTextView() remainingBox.SetBackgroundColor(config.ColorPlaybackProgressRemaining) remainingBox.SetTextColor(config.ColorPlaybackProgressElapsed) 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) }