2024-01-16 15:21:35 +00:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/client"
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/common"
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/config"
|
|
|
|
"github.com/delucks/go-subsonic"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ View = &main{}
|
|
|
|
|
|
|
|
type mainviewmode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
mainModeAlbum mainviewmode = iota
|
|
|
|
mainModePlaylist mainviewmode = iota
|
|
|
|
mainModeArtist mainviewmode = iota
|
|
|
|
mainModeSearch mainviewmode = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type main struct {
|
|
|
|
view *tview.Flex
|
|
|
|
client *client.Client
|
|
|
|
mode mainviewmode
|
|
|
|
query string
|
|
|
|
miniView bool
|
|
|
|
album *subsonic.AlbumID3
|
|
|
|
searchResult *subsonic.SearchResult3
|
|
|
|
playlist *subsonic.Playlist
|
|
|
|
songList *tview.Table
|
|
|
|
artist *subsonic.ArtistID3
|
|
|
|
artistInfo *subsonic.ArtistInfo2
|
|
|
|
playAllFunc func(song ...*subsonic.Child)
|
|
|
|
addSongsFunc func(song ...*subsonic.Child)
|
|
|
|
log func(format string, a ...any)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMainView(client *client.Client, log func(format string, a ...any)) *main {
|
|
|
|
playlistAlbum := &main{
|
|
|
|
client: client,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
|
|
|
|
flex := tview.NewFlex().SetDirection(tview.FlexRow)
|
|
|
|
|
|
|
|
flex.SetBackgroundColor(config.ColorBackground)
|
|
|
|
flex.SetTitle("Subsonic TUI [`]")
|
|
|
|
flex.SetBorder(true)
|
|
|
|
flex.SetFocusFunc(func() {
|
|
|
|
flex.SetBorderColor(config.ColorSelectedBoarder)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if playlistAlbum.songList != nil {
|
2024-12-18 15:29:16 +00:00
|
|
|
// playlistAlbum.view.Blur()
|
2024-01-16 15:21:35 +00:00
|
|
|
playlistAlbum.songList.Focus(nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
flex.SetBlurFunc(func() {
|
|
|
|
flex.SetBorderColor(config.ColorBluredBoarder)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if playlistAlbum.songList != nil {
|
|
|
|
playlistAlbum.songList.Blur()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
playlistAlbum.view = flex
|
|
|
|
// Empty Box for starters...
|
|
|
|
playlistAlbum.view.AddItem(EmptyBox, 0, 1, false)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return playlistAlbum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) SetMiniView(mini bool) {
|
|
|
|
m.miniView = mini
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) SetAlbum(album *subsonic.AlbumID3) {
|
|
|
|
m.mode = mainModeAlbum
|
|
|
|
m.album = album
|
|
|
|
m.Update()
|
|
|
|
m.log("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) SetPlaylist(playlist *subsonic.Playlist) {
|
|
|
|
m.mode = mainModePlaylist
|
|
|
|
m.playlist = playlist
|
|
|
|
m.Update()
|
|
|
|
m.log("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) SetArtist(artist *subsonic.ArtistID3) {
|
|
|
|
m.mode = mainModeArtist
|
|
|
|
m.artist = artist
|
|
|
|
info, _ := m.client.GetArtistInfo(artist.ID)
|
|
|
|
m.artistInfo = info
|
|
|
|
m.Update()
|
|
|
|
m.log("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) SetSearch(result *subsonic.SearchResult3, query string) {
|
|
|
|
m.mode = mainModeSearch
|
|
|
|
m.searchResult = result
|
|
|
|
m.query = query
|
|
|
|
m.Update()
|
|
|
|
m.log("Found #%d artists, #%d albums and #%d songs",
|
|
|
|
len(result.Artist),
|
|
|
|
len(result.Album),
|
|
|
|
len(result.Song),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) drawPlaylist() {
|
2024-12-18 15:29:16 +00:00
|
|
|
subtitle := fmt.Sprintf("%s\n\nCreated by: %s | %s", m.playlist.Comment,
|
|
|
|
m.playlist.Owner, time.Duration(m.playlist.Duration*int(time.Second)).String())
|
2024-01-16 15:21:35 +00:00
|
|
|
m.populateHeader(m.playlist.Name, subtitle, m.playlist.Duration, m.playlist.CoverArt)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
playBtn := m.drawPlaylistAlbumButtons(m.playlist.Entry)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
m.populateSongs(m.playlist.Entry, playBtn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) drawAlbum() {
|
2024-12-18 15:29:16 +00:00
|
|
|
subtitle := fmt.Sprintf("%s\n\n%d | %s", m.album.Artist, m.album.Year,
|
|
|
|
time.Duration(m.album.Duration*int(time.Second)).String())
|
2024-01-16 15:21:35 +00:00
|
|
|
m.populateHeader(m.album.Name, subtitle, m.album.Duration, m.album.CoverArt)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
playBtn := m.drawPlaylistAlbumButtons(m.album.Song)
|
|
|
|
|
2024-12-18 15:29:16 +00:00
|
|
|
m.populateSongs(m.album.Song, playBtn)
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) drawArtist() {
|
|
|
|
m.populateHeader(m.artist.Name, m.artistInfo.Biography, 0, m.artist.CoverArt)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
btn := m.drawArtistButtons()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
m.populateAlbums(btn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) drawSearch() {
|
2024-12-18 15:29:16 +00:00
|
|
|
sub := "Query: " + m.query
|
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
m.populateHeader("Search Results", sub, 0, "")
|
|
|
|
m.populateSearchResults()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) Update() {
|
|
|
|
m.view.Clear()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
switch m.mode {
|
|
|
|
case mainModeAlbum:
|
|
|
|
m.drawAlbum()
|
|
|
|
case mainModePlaylist:
|
|
|
|
m.drawPlaylist()
|
|
|
|
case mainModeArtist:
|
|
|
|
m.drawArtist()
|
|
|
|
case mainModeSearch:
|
|
|
|
m.drawSearch()
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
m.songList.Focus(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) drawArtistButtons() *tview.Button {
|
|
|
|
// Add buttons: Radio
|
2024-12-18 15:29:16 +00:00
|
|
|
topSongs := 10
|
|
|
|
songs, _ := m.client.GetTopSongs(m.artist.Name, topSongs)
|
2024-01-16 15:21:35 +00:00
|
|
|
f := tview.NewFlex()
|
|
|
|
f.SetBackgroundColor(config.ColorBackground)
|
|
|
|
f.SetBorderPadding(0, 0, 2, 2)
|
|
|
|
// Buttons
|
|
|
|
radio := NewButton("Radio")
|
|
|
|
top10 := NewButton("Top 10")
|
|
|
|
// Button callbacks
|
|
|
|
top10.SetSelectedFunc(func() {
|
|
|
|
top10.Blur()
|
|
|
|
m.log("Playing %s's top 10 songs", m.artist.Name)
|
|
|
|
m.playAllFunc(songs...)
|
|
|
|
m.songList.Focus(nil)
|
|
|
|
})
|
|
|
|
radio.SetSelectedFunc(func() {
|
|
|
|
radio.Blur()
|
|
|
|
m.log("Generating %s's radio", m.artist.Name)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
go func() {
|
|
|
|
radioSongs := []*subsonic.Child{}
|
|
|
|
if config.ExperimentalRadioAlgo() {
|
|
|
|
radioSongs, _ = m.client.GetExperimentalArtistRadio(m.artist, m.artistInfo, config.MaxRadioSongs())
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if len(radioSongs) == 0 {
|
|
|
|
radioSongs, _ = m.client.GetSimilarSongs(m.artist.ID, config.MaxRadioSongs())
|
|
|
|
}
|
|
|
|
|
|
|
|
common.ShuffleSlice(radioSongs)
|
|
|
|
m.playAllFunc(radioSongs...)
|
|
|
|
}()
|
|
|
|
m.songList.Focus(nil)
|
|
|
|
})
|
|
|
|
radio.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyDown:
|
|
|
|
radio.Blur()
|
|
|
|
m.songList.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyRight:
|
|
|
|
radio.Blur()
|
|
|
|
top10.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
2024-12-18 15:29:16 +00:00
|
|
|
default:
|
|
|
|
return event
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
top10.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyDown:
|
|
|
|
top10.Blur()
|
|
|
|
m.songList.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
top10.Blur()
|
|
|
|
radio.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
2024-12-18 15:29:16 +00:00
|
|
|
default:
|
|
|
|
return event
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
f.AddItem(radio, 0, 1, false)
|
|
|
|
f.AddItem(EmptyBox, 0, 1, false)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if len(songs) > 0 {
|
|
|
|
f.AddItem(top10, 0, 1, false)
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
f.AddItem(EmptyBox, 0, 1, false)
|
|
|
|
|
|
|
|
// Add the buttons to the view
|
|
|
|
m.view.AddItem(f, 1, 0, false)
|
|
|
|
// Margin bottom of 1 line
|
|
|
|
m.view.AddItem(EmptyBox, 1, 0, false)
|
|
|
|
|
|
|
|
return radio
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) populateAlbums(btn *tview.Button) {
|
|
|
|
table := tview.NewTable()
|
|
|
|
table.SetBackgroundColor(config.ColorBackground)
|
|
|
|
table.SetWrapSelection(true, false)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
for i, album := range m.artist.Album {
|
2024-12-18 15:29:16 +00:00
|
|
|
year := tview.NewTableCell(strconv.Itoa(album.Year)).SetTextColor(config.ColorTextAccent)
|
2024-01-16 15:21:35 +00:00
|
|
|
name := tview.NewTableCell(album.Name).SetExpansion(2).SetAlign(tview.AlignCenter)
|
|
|
|
d := time.Second * time.Duration(album.Duration)
|
|
|
|
duration := tview.NewTableCell(d.String()).SetAlign(tview.AlignRight).SetExpansion(1)
|
|
|
|
|
|
|
|
table.SetCell(i, 0, year)
|
|
|
|
table.SetCell(i, 1, name)
|
|
|
|
table.SetCell(i, 2, duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
table.SetFocusFunc(func() {
|
|
|
|
m.view.SetBorderColor(config.ColorSelectedBoarder)
|
|
|
|
table.SetSelectable(true, false)
|
|
|
|
})
|
|
|
|
table.SetBlurFunc(func() {
|
|
|
|
m.view.SetBorderColor(config.ColorBluredBoarder)
|
|
|
|
table.SetSelectable(false, false)
|
|
|
|
})
|
|
|
|
table.SetSelectedFunc(func(row, column int) {
|
|
|
|
alb, _ := m.client.GetAlbum(m.artist.Album[row].ID)
|
|
|
|
m.SetAlbum(alb)
|
|
|
|
m.view.Focus(nil)
|
|
|
|
})
|
|
|
|
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
row, _ := m.songList.GetSelection()
|
|
|
|
if row == 0 && event.Key() == tcell.KeyUp {
|
|
|
|
table.Blur()
|
|
|
|
m.view.SetBorderColor(config.ColorSelectedBoarder)
|
|
|
|
btn.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return event
|
|
|
|
})
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
m.songList = table
|
|
|
|
|
|
|
|
m.view.AddItem(table, 0, 1, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) drawPlaylistAlbumButtons(songs []*subsonic.Child) *tview.Button {
|
|
|
|
// Add buttons: Play | Shuffle | Add to queue
|
|
|
|
f := tview.NewFlex()
|
|
|
|
f.SetBackgroundColor(config.ColorBackground)
|
|
|
|
f.SetBorderPadding(0, 0, 2, 2)
|
|
|
|
// Buttons
|
|
|
|
play := NewButton("Play")
|
|
|
|
shuffle := NewButton("Shuffle")
|
|
|
|
queue := NewButton("Queue")
|
|
|
|
artist := NewButton("Artist")
|
|
|
|
// Button callbacks
|
|
|
|
play.SetSelectedFunc(func() {
|
|
|
|
play.Blur()
|
|
|
|
m.playAllFunc(songs...)
|
|
|
|
m.songList.Focus(nil)
|
|
|
|
})
|
|
|
|
play.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
play.Blur()
|
|
|
|
artist.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyRight:
|
|
|
|
play.Blur()
|
|
|
|
shuffle.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyDown:
|
|
|
|
play.Blur()
|
|
|
|
m.songList.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
2024-12-18 15:29:16 +00:00
|
|
|
default:
|
|
|
|
return event
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
shuffle.SetSelectedFunc(func() {
|
|
|
|
shuffle.Blur()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
cpy := make([]*subsonic.Child, len(songs))
|
|
|
|
copy(cpy, songs)
|
|
|
|
common.ShuffleSlice(cpy)
|
|
|
|
m.playAllFunc(cpy...)
|
|
|
|
m.songList.Focus(nil)
|
|
|
|
})
|
|
|
|
shuffle.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
shuffle.Blur()
|
|
|
|
play.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyRight:
|
|
|
|
shuffle.Blur()
|
|
|
|
queue.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyDown:
|
|
|
|
shuffle.Blur()
|
|
|
|
m.songList.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
2024-12-18 15:29:16 +00:00
|
|
|
default:
|
|
|
|
return event
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
queue.SetSelectedFunc(func() {
|
|
|
|
queue.Blur()
|
|
|
|
m.addSongsFunc(songs...)
|
|
|
|
m.songList.Focus(nil)
|
|
|
|
})
|
|
|
|
queue.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
queue.Blur()
|
|
|
|
shuffle.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyRight:
|
|
|
|
queue.Blur()
|
|
|
|
artist.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyDown:
|
|
|
|
queue.Blur()
|
|
|
|
m.songList.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
2024-12-18 15:29:16 +00:00
|
|
|
default:
|
|
|
|
return event
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
artist.SetSelectedFunc(func() {
|
|
|
|
artist.Blur()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
ar, _ := m.client.GetArtist(m.album.ArtistID)
|
|
|
|
m.SetArtist(ar)
|
|
|
|
m.songList.Focus(nil)
|
|
|
|
})
|
|
|
|
artist.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
artist.Blur()
|
|
|
|
queue.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyRight:
|
|
|
|
artist.Blur()
|
|
|
|
play.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
case tcell.KeyDown:
|
|
|
|
artist.Blur()
|
|
|
|
m.songList.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return event
|
|
|
|
})
|
|
|
|
|
|
|
|
f.AddItem(play, 0, 1, true)
|
|
|
|
f.AddItem(EmptyBox, 0, 1, false)
|
|
|
|
f.AddItem(shuffle, 0, 1, false)
|
|
|
|
f.AddItem(EmptyBox, 0, 1, false)
|
|
|
|
f.AddItem(queue, 0, 1, false)
|
|
|
|
f.AddItem(EmptyBox, 0, 1, false)
|
|
|
|
f.AddItem(artist, 0, 1, false)
|
|
|
|
|
|
|
|
// Add the buttons to the view
|
|
|
|
m.view.AddItem(f, 1, 0, false)
|
|
|
|
// Margin bottom of 1 line
|
|
|
|
m.view.AddItem(EmptyBox, 1, 0, false)
|
|
|
|
|
|
|
|
return play
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) populateSongs(songs []*subsonic.Child, play *tview.Button) {
|
|
|
|
table := tview.NewTable()
|
|
|
|
table.SetBackgroundColor(config.ColorBackground)
|
|
|
|
table.SetWrapSelection(true, false)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
for i, song := range songs {
|
2024-12-18 15:29:16 +00:00
|
|
|
num := tview.NewTableCell(strconv.Itoa(i + 1)).SetTextColor(config.ColorTextAccent)
|
2024-01-16 15:21:35 +00:00
|
|
|
title := tview.NewTableCell(song.Title).SetMaxWidth(15).SetExpansion(2)
|
|
|
|
artist := tview.NewTableCell(song.Artist).SetMaxWidth(15).SetExpansion(1)
|
|
|
|
d := time.Second * time.Duration(song.Duration)
|
|
|
|
duration := tview.NewTableCell(d.String()).SetAlign(tview.AlignRight).SetExpansion(1)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
table.SetCell(i, 0, num)
|
|
|
|
table.SetCell(i, 1, title)
|
|
|
|
table.SetCell(i, 2, artist)
|
|
|
|
table.SetCell(i, 3, duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.songList = table
|
|
|
|
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
// on first line and pressing up -> play button
|
|
|
|
row, _ := table.GetSelection()
|
|
|
|
if row == 0 && event.Key() == tcell.KeyUp {
|
|
|
|
table.Blur()
|
|
|
|
m.view.SetBorderColor(config.ColorSelectedBoarder)
|
|
|
|
play.Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
} else if event.Rune() == 'r' {
|
|
|
|
song := songs[row]
|
|
|
|
m.log("Generating song (%s) radio....", song.Title)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
go func() {
|
|
|
|
radioSongs, _ := m.client.GetSimilarSongs(song.ID, config.MaxRadioSongs())
|
|
|
|
m.playAllFunc(radioSongs...)
|
|
|
|
}()
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return event
|
|
|
|
})
|
|
|
|
table.SetFocusFunc(func() {
|
|
|
|
m.view.SetBorderColor(config.ColorSelectedBoarder)
|
|
|
|
table.SetSelectable(true, false)
|
|
|
|
})
|
|
|
|
table.SetBlurFunc(func() {
|
|
|
|
m.view.SetBorderColor(config.ColorBluredBoarder)
|
|
|
|
table.SetSelectable(false, false)
|
|
|
|
})
|
|
|
|
m.songList.SetSelectedFunc(func(row, column int) {
|
|
|
|
m.addSongsFunc(songs[row])
|
|
|
|
})
|
|
|
|
|
|
|
|
m.view.AddItem(table, 0, 1, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) populateSearchResults() {
|
|
|
|
table := tview.NewTable()
|
|
|
|
table.SetBackgroundColor(config.ColorBackground)
|
|
|
|
table.SetWrapSelection(true, false)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
row := 0
|
|
|
|
lastArtist := 0
|
|
|
|
lastAlbum := 0
|
|
|
|
|
|
|
|
// Artists
|
|
|
|
if len(m.searchResult.Artist) > 0 {
|
|
|
|
// Header
|
|
|
|
header := tview.NewTableCell("Artists").SetSelectable(false)
|
|
|
|
table.SetCell(row, 0, header)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
row++
|
2024-12-18 15:29:16 +00:00
|
|
|
// List
|
2024-01-16 15:21:35 +00:00
|
|
|
for i, artist := range m.searchResult.Artist {
|
2024-12-18 15:29:16 +00:00
|
|
|
index := tview.NewTableCell(strconv.Itoa(i + 1)).
|
2024-01-16 15:21:35 +00:00
|
|
|
SetTextColor(config.ColorTextAccent)
|
|
|
|
a := tview.NewTableCell(artist.Name).SetExpansion(2).SetMaxWidth(15)
|
2024-12-18 15:29:16 +00:00
|
|
|
count := tview.NewTableCell(fmt.Sprintf("%d Albums", artist.AlbumCount)).
|
2024-01-16 15:21:35 +00:00
|
|
|
SetExpansion(1).SetAlign(tview.AlignRight).SetMaxWidth(15)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
table.SetCell(row, 0, index)
|
|
|
|
table.SetCell(row, 1, a)
|
2024-12-18 15:29:16 +00:00
|
|
|
table.SetCell(row, 2, count)
|
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
row++
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
lastArtist = row
|
|
|
|
}
|
|
|
|
|
|
|
|
// Albums
|
|
|
|
|
|
|
|
if len(m.searchResult.Album) > 0 {
|
|
|
|
// Header
|
|
|
|
header := tview.NewTableCell("Albums").SetSelectable(false)
|
|
|
|
table.SetCell(row, 0, header)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
row++
|
2024-12-18 15:29:16 +00:00
|
|
|
// List
|
2024-01-16 15:21:35 +00:00
|
|
|
for i, album := range m.searchResult.Album {
|
2024-12-18 15:29:16 +00:00
|
|
|
index := tview.NewTableCell(strconv.Itoa(i + 1)).
|
2024-01-16 15:21:35 +00:00
|
|
|
SetTextColor(config.ColorTextAccent)
|
|
|
|
title := tview.NewTableCell(album.Name).SetExpansion(2).SetMaxWidth(15)
|
|
|
|
artist := tview.NewTableCell(album.Artist).
|
|
|
|
SetExpansion(1).SetAlign(tview.AlignRight).SetMaxWidth(15)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
table.SetCell(row, 0, index)
|
|
|
|
table.SetCell(row, 1, title)
|
|
|
|
table.SetCell(row, 2, artist)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
row++
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
lastAlbum = row
|
|
|
|
}
|
|
|
|
// Songs
|
|
|
|
if len(m.searchResult.Song) > 0 {
|
|
|
|
// Header
|
|
|
|
header := tview.NewTableCell("Songs").SetSelectable(false)
|
|
|
|
table.SetCell(row, 0, header)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
row++
|
2024-12-18 15:29:16 +00:00
|
|
|
// List
|
2024-01-16 15:21:35 +00:00
|
|
|
for i, song := range m.searchResult.Song {
|
2024-12-18 15:29:16 +00:00
|
|
|
index := tview.NewTableCell(strconv.Itoa(i + 1)).
|
2024-01-16 15:21:35 +00:00
|
|
|
SetTextColor(config.ColorTextAccent)
|
|
|
|
title := tview.NewTableCell(song.Title).SetExpansion(2).SetMaxWidth(15)
|
|
|
|
artist := tview.NewTableCell(song.Artist).
|
|
|
|
SetExpansion(1).SetAlign(tview.AlignRight).SetMaxWidth(15)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
table.SetCell(row, 0, index)
|
|
|
|
table.SetCell(row, 1, title)
|
|
|
|
table.SetCell(row, 2, artist)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
row++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.songList = table
|
|
|
|
table.SetFocusFunc(func() {
|
|
|
|
m.view.SetBorderColor(config.ColorSelectedBoarder)
|
|
|
|
table.SetSelectable(true, false)
|
|
|
|
})
|
|
|
|
table.SetBlurFunc(func() {
|
|
|
|
m.view.SetBorderColor(config.ColorBluredBoarder)
|
|
|
|
table.SetSelectable(false, false)
|
|
|
|
})
|
|
|
|
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
row, _ := table.GetSelection()
|
|
|
|
if row <= lastAlbum {
|
|
|
|
return event
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if event.Rune() != 'r' {
|
|
|
|
return event
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
cell := table.GetCell(row, 0)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
index, err := strconv.Atoi(cell.Text)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
song := m.searchResult.Song[index-1]
|
|
|
|
m.log("Generating song (%s) radio....", song.Title)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
go func() {
|
|
|
|
radioSongs, _ := m.client.GetSimilarSongs(song.ID, config.MaxRadioSongs())
|
|
|
|
m.playAllFunc(radioSongs...)
|
|
|
|
}()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
m.songList.SetSelectedFunc(func(row, column int) {
|
|
|
|
cell := table.GetCell(row, 0)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
index, err := strconv.Atoi(cell.Text)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if row <= lastArtist {
|
|
|
|
artist, _ := m.client.GetArtist(m.searchResult.Artist[index-1].ID)
|
|
|
|
m.SetArtist(artist)
|
|
|
|
} else if lastArtist < row && row <= lastAlbum {
|
|
|
|
album, _ := m.client.GetAlbum(m.searchResult.Album[index-1].ID)
|
|
|
|
m.SetAlbum(album)
|
|
|
|
} else {
|
|
|
|
m.addSongsFunc(m.searchResult.Song[index-1])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
m.view.AddItem(table, 0, 1, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) populateHeader(title, subtitle string,
|
2024-12-18 15:29:16 +00:00
|
|
|
duration int, coverArtID string,
|
|
|
|
) {
|
|
|
|
_ = duration
|
2024-01-16 15:21:35 +00:00
|
|
|
header := tview.NewFlex()
|
|
|
|
header.SetBackgroundColor(config.ColorBackground)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
art := tview.NewImage()
|
|
|
|
art.SetBackgroundColor(config.ColorBackground)
|
|
|
|
|
|
|
|
img, _ := m.client.GetCoverArt(coverArtID)
|
|
|
|
art.SetImage(img)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
t := tview.NewTextView().
|
|
|
|
SetTextColor(config.ColorTextAccent).
|
|
|
|
SetTextAlign(tview.AlignCenter).
|
|
|
|
SetText(title)
|
|
|
|
t.SetBackgroundColor(config.ColorBackground)
|
|
|
|
s := tview.NewTextView().
|
|
|
|
SetTextColor(config.ColorText).
|
|
|
|
SetTextAlign(tview.AlignCenter).
|
|
|
|
SetText(subtitle).SetWordWrap(true)
|
|
|
|
s.SetBackgroundColor(config.ColorBackground)
|
|
|
|
|
|
|
|
g := tview.NewFlex().SetDirection(tview.FlexRow)
|
|
|
|
g.SetBackgroundColor(config.ColorBackground)
|
|
|
|
g.AddItem(EmptyBox, 1, 1, false)
|
|
|
|
g.AddItem(t, 1, 1, false)
|
|
|
|
g.AddItem(s, 0, 1, false)
|
|
|
|
|
|
|
|
header.AddItem(art, 0, 1, false)
|
|
|
|
header.AddItem(g, 0, 3, false)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
size := 6
|
|
|
|
if m.miniView {
|
|
|
|
size = 4
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
m.view.AddItem(header, size, 1, false)
|
|
|
|
|
|
|
|
// Margin bottom of 1 line
|
|
|
|
if !m.miniView {
|
|
|
|
m.view.AddItem(EmptyBox, 1, 0, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) SetPlayAllFunc(f func(song ...*subsonic.Child)) {
|
|
|
|
m.playAllFunc = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) SetPlayAddSongFunc(f func(song ...*subsonic.Child)) {
|
|
|
|
m.addSongsFunc = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *main) GetView() tview.Primitive {
|
|
|
|
return m.view
|
|
|
|
}
|