gospt/internal/tui/main.go

529 lines
16 KiB
Go
Raw Normal View History

2023-01-09 04:42:02 +00:00
package tui
import (
"fmt"
2023-01-11 17:45:29 +00:00
"time"
2023-01-09 04:42:02 +00:00
"gospt/internal/gctx"
2023-01-11 17:45:29 +00:00
"github.com/charmbracelet/bubbles/key"
2023-01-09 04:42:02 +00:00
"github.com/charmbracelet/bubbles/list"
2023-01-13 09:12:31 +00:00
"github.com/charmbracelet/bubbles/textinput"
2023-01-09 04:42:02 +00:00
tea "github.com/charmbracelet/bubbletea"
2023-01-11 17:45:29 +00:00
"github.com/charmbracelet/lipgloss"
2023-01-09 04:42:02 +00:00
"github.com/zmb3/spotify/v2"
)
2023-01-11 17:45:29 +00:00
var (
2023-01-12 02:45:22 +00:00
P *tea.Program
DocStyle = lipgloss.NewStyle().Margin(0, 2)
2023-01-11 17:45:29 +00:00
currentlyPlaying string
main_updates chan *mainModel
page = 1
)
2023-01-11 04:46:15 +00:00
2023-01-14 01:41:52 +00:00
type Mode string
const (
Album Mode = "album"
ArtistAlbum = "artistalbum"
Artist = "artist"
Artists = "artists"
Tracks = "tracks"
Albums = "albums"
Main = "main"
Playlists = "playlists"
Playlist = "playlist"
Devices = "devices"
Search = "search"
SearchAlbums = "searchalbums"
SearchArtists = "searchartists"
SearchTracks = "searchtracks"
SearchPlaylists = "searchplaylsits"
SearchArtist = "searchartist"
SearchPlaylist = "searchplaylist"
SearchAlbum = "searchalbum"
)
2023-01-09 04:42:02 +00:00
type mainItem struct {
Name string
2023-01-11 17:45:29 +00:00
Duration string
Artist spotify.SimpleArtist
ID spotify.ID
2023-01-09 04:42:02 +00:00
Desc string
SpotifyItem any
}
func (i mainItem) Title() string { return i.Name }
func (i mainItem) Description() string { return i.Desc }
func (i mainItem) FilterValue() string { return i.Title() + i.Desc }
type mainModel struct {
2023-01-13 03:41:54 +00:00
list list.Model
2023-01-13 09:12:31 +00:00
input textinput.Model
2023-01-13 03:41:54 +00:00
ctx *gctx.Context
client *spotify.Client
2023-01-14 01:41:52 +00:00
mode Mode
2023-01-13 03:41:54 +00:00
playlist spotify.SimplePlaylist
artist spotify.SimpleArtist
album spotify.SimpleAlbum
2023-01-13 09:12:31 +00:00
search string
2023-01-13 03:41:54 +00:00
fromArtist bool
2023-01-09 04:42:02 +00:00
}
2023-01-14 01:41:52 +00:00
func (m *mainModel) GoBack() (tea.Cmd, error) {
switch m.mode {
case Main:
return tea.Quit, nil
case Albums, Artists, Tracks, Playlist, Devices, Search:
m.mode = Main
m.list.NewStatusMessage("Setting view to main")
new_items, err := MainView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
}
m.list.SetItems(new_items)
case Album:
m.mode = Albums
m.list.NewStatusMessage("Setting view to albums")
new_items, err := AlbumsView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case Artist:
m.mode = Artists
m.fromArtist = false
m.list.NewStatusMessage("Setting view to artists")
new_items, err := ArtistsView(m.ctx, m.client)
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case ArtistAlbum:
m.mode = Artist
m.fromArtist = true
m.list.NewStatusMessage("Opening " + m.artist.Name)
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client)
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case SearchArtists, SearchTracks, SearchAlbums, SearchPlaylists:
m.mode = Search
m.list.NewStatusMessage("Setting view to search for " + m.input.Value())
items, err := SearchView(m.ctx, m.client, m.search)
if err != nil {
return nil, err
}
m.list.SetItems(items)
case SearchArtist:
m.mode = SearchArtists
case SearchAlbum:
m.mode = SearchAlbums
case SearchPlaylist:
m.mode = SearchPlaylists
default:
m.list.ResetSelected()
page = 0
}
return nil, nil
}
2023-01-09 04:42:02 +00:00
func (m mainModel) Init() tea.Cmd {
2023-01-11 17:45:29 +00:00
main_updates = make(chan *mainModel)
2023-01-09 04:42:02 +00:00
return nil
}
2023-01-11 17:45:29 +00:00
func (m *mainModel) Tick() {
ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
playing, _ := m.client.PlayerCurrentlyPlaying(m.ctx)
2023-01-11 21:30:55 +00:00
if playing.Playing {
currentlyPlaying = "Now playing " + playing.Item.Name + " by " + playing.Item.Artists[0].Name
}
2023-01-11 17:45:29 +00:00
case <-quit:
ticker.Stop()
return
}
}
}()
}
2023-01-13 06:19:43 +00:00
func (m mainModel) View() string {
2023-01-13 09:12:31 +00:00
if m.input.Focused() {
return DocStyle.Render(m.list.View() + "\n" + m.input.View())
}
return DocStyle.Render(m.list.View() + "\n")
2023-01-11 17:45:29 +00:00
}
2023-01-14 00:35:42 +00:00
func (m *mainModel) Typing(msg tea.KeyMsg) (bool, tea.Cmd) {
if msg.String() == "enter" {
m.list.NewStatusMessage("Setting view to search for " + m.input.Value())
items, err := SearchView(m.ctx, m.client, m.input.Value())
if err != nil {
fmt.Println(err.Error())
return false, tea.Quit
}
m.search = m.input.Value()
m.list.SetItems(items)
m.list.ResetSelected()
m.input.SetValue("")
m.input.Blur()
return true, nil
}
if msg.String() == "esc" {
m.input.SetValue("")
m.input.Blur()
return false, nil
}
m.input, _ = m.input.Update(msg)
return false, nil
}
2023-01-09 04:42:02 +00:00
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2023-01-11 17:45:29 +00:00
m.list.NewStatusMessage(currentlyPlaying)
2023-01-11 04:46:15 +00:00
select {
2023-01-11 17:45:29 +00:00
case update := <-main_updates:
m.list.SetItems(update.list.Items())
2023-01-11 04:46:15 +00:00
default:
}
2023-01-11 17:45:29 +00:00
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 && m.list.Cursor() == 0 {
2023-01-11 04:46:15 +00:00
// if last request was still full request more
2023-01-09 04:42:02 +00:00
if len(m.list.Items())%50 == 0 {
2023-01-11 04:46:15 +00:00
go m.LoadMoreItems()
2023-01-09 04:42:02 +00:00
}
}
switch msg := msg.(type) {
case tea.KeyMsg:
2023-01-14 00:35:42 +00:00
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
2023-01-13 09:12:31 +00:00
if m.input.Focused() {
2023-01-14 00:35:42 +00:00
search, cmd := m.Typing(msg)
if search {
m.mode = "search"
2023-01-13 09:12:31 +00:00
}
2023-01-14 00:35:42 +00:00
return m, cmd
2023-01-13 09:12:31 +00:00
}
if msg.String() == "s" {
m.input.Focus()
}
2023-01-11 17:45:29 +00:00
if msg.String() == "d" {
2023-01-13 09:12:31 +00:00
if !m.input.Focused() {
2023-01-14 01:41:52 +00:00
m.mode = Devices
2023-01-13 09:12:31 +00:00
new_items, err := DeviceView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to devices")
2023-01-11 17:45:29 +00:00
}
2023-01-09 04:42:02 +00:00
}
2023-01-11 22:35:14 +00:00
if msg.String() == "backspace" || msg.String() == "esc" || msg.String() == "q" {
2023-01-14 01:41:52 +00:00
msg, err := m.GoBack()
if err != nil {
fmt.Println(err)
2023-01-11 17:45:29 +00:00
}
2023-01-14 01:41:52 +00:00
return m, msg
2023-01-11 17:45:29 +00:00
}
2023-01-14 01:41:52 +00:00
2023-01-11 17:45:29 +00:00
if msg.String() == "enter" || msg.String() == "spacebar" {
switch m.mode {
2023-01-14 01:41:52 +00:00
case SearchArtists:
m.mode = SearchArtist
2023-01-13 19:14:20 +00:00
m.fromArtist = true
m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist)
m.list.NewStatusMessage("Opening " + m.artist.Name)
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
2023-01-14 01:41:52 +00:00
case SearchAlbums:
m.mode = SearchAlbum
2023-01-13 19:14:20 +00:00
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
m.list.NewStatusMessage("Opening " + m.album.Name)
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
2023-01-14 01:41:52 +00:00
case SearchPlaylists:
m.mode = SearchPlaylist
2023-01-13 19:14:20 +00:00
playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist)
m.playlist = playlist
m.list.NewStatusMessage("Setting view to playlist " + playlist.Name)
new_items, err := PlaylistView(m.ctx, m.client, playlist)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
2023-01-14 01:41:52 +00:00
case Search:
2023-01-13 09:12:31 +00:00
switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
case *spotify.FullArtistPage:
2023-01-14 01:41:52 +00:00
m.mode = SearchArtists
2023-01-13 09:12:31 +00:00
m.list.NewStatusMessage("Setting view to artists")
new_items, err := SearchArtistsView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.FullArtistPage))
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SimpleAlbumPage:
2023-01-14 01:41:52 +00:00
m.mode = SearchAlbums
2023-01-13 09:12:31 +00:00
m.list.NewStatusMessage("Setting view to albums")
new_items, err := SearchAlbumsView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.SimpleAlbumPage))
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SimplePlaylistPage:
2023-01-14 01:41:52 +00:00
m.mode = SearchPlaylists
2023-01-13 09:12:31 +00:00
playlists := m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.SimplePlaylistPage)
m.list.NewStatusMessage("Setting view to playlist")
new_items, err := SearchPlaylistsView(m.ctx, m.client, playlists)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.FullTrackPage:
2023-01-14 01:41:52 +00:00
m.mode = SearchTracks
2023-01-13 09:12:31 +00:00
m.list.NewStatusMessage("Setting view to tracks")
new_items, err := SearchTracksView(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(*spotify.FullTrackPage))
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to tracks")
}
2023-01-11 17:45:29 +00:00
case "main":
switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
2023-01-13 02:48:41 +00:00
case *spotify.FullArtistCursorPage:
2023-01-14 01:41:52 +00:00
m.mode = Artists
2023-01-13 02:48:41 +00:00
m.list.NewStatusMessage("Setting view to artists")
new_items, err := ArtistsView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SavedAlbumPage:
2023-01-14 01:41:52 +00:00
m.mode = Albums
2023-01-13 02:48:41 +00:00
m.list.NewStatusMessage("Setting view to albums")
new_items, err := AlbumsView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
2023-01-11 17:45:29 +00:00
case spotify.SimplePlaylist:
2023-01-14 01:41:52 +00:00
m.mode = Playlist
2023-01-11 17:45:29 +00:00
playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist)
m.playlist = playlist
m.list.NewStatusMessage("Setting view to playlist " + playlist.Name)
new_items, err := PlaylistView(m.ctx, m.client, playlist)
if err != nil {
fmt.Println(err.Error())
2023-01-11 21:30:55 +00:00
return m, tea.Quit
2023-01-11 17:45:29 +00:00
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SavedTrackPage:
2023-01-14 01:41:52 +00:00
m.mode = Tracks
2023-01-11 17:45:29 +00:00
m.list.NewStatusMessage("Setting view to saved tracks")
new_items, err := SavedTracksView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
2023-01-11 21:30:55 +00:00
return m, tea.Quit
2023-01-11 17:45:29 +00:00
}
m.list.SetItems(new_items)
m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to tracks")
2023-01-09 18:54:21 +00:00
}
2023-01-14 01:41:52 +00:00
case Albums:
m.mode = Album
2023-01-13 03:27:05 +00:00
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
m.list.NewStatusMessage("Opening " + m.album.Name)
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
2023-01-14 01:41:52 +00:00
case Artist:
m.mode = ArtistAlbum
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
m.list.NewStatusMessage("Opening " + m.album.Name)
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case Artists:
m.mode = Artist
2023-01-13 03:41:54 +00:00
m.fromArtist = true
2023-01-13 03:27:05 +00:00
m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist)
m.list.NewStatusMessage("Opening " + m.artist.Name)
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.client)
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.list.SetItems(new_items)
m.list.ResetSelected()
2023-01-14 01:41:52 +00:00
case Album, ArtistAlbum:
2023-01-13 03:27:05 +00:00
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlay(m.ctx, m.client, &m.album.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.TotalPages))
2023-01-14 01:41:52 +00:00
case Playlist, SearchPlaylist:
2023-01-11 17:45:29 +00:00
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlay(m.ctx, m.client, &m.playlist.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
2023-01-14 01:41:52 +00:00
case Tracks, SearchTracks:
2023-01-11 17:45:29 +00:00
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlayLikedSong(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
2023-01-14 01:41:52 +00:00
case Devices:
2023-01-12 02:45:22 +00:00
go HandleSetDevice(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlayerDevice))
2023-01-11 17:45:29 +00:00
m.list.NewStatusMessage("Setting device to " + m.list.SelectedItem().FilterValue())
m.mode = "main"
m.list.NewStatusMessage("Setting view to main")
new_items, err := MainView(m.ctx, m.client)
2023-01-09 18:54:21 +00:00
if err != nil {
2023-01-11 17:45:29 +00:00
fmt.Println(err.Error())
2023-01-09 18:54:21 +00:00
}
2023-01-11 17:45:29 +00:00
m.list.SetItems(new_items)
}
}
if msg.String() == "ctrl+r" {
2023-01-14 01:41:52 +00:00
switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
case spotify.SimplePlaylist:
2023-01-13 06:19:43 +00:00
currentlyPlaying = m.list.SelectedItem().FilterValue()
2023-01-14 01:41:52 +00:00
m.list.NewStatusMessage("Starting radio for " + currentlyPlaying)
go HandlePlaylistRadio(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist))
case *spotify.SavedTrackPage:
2023-01-11 17:45:29 +00:00
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Starting radio for " + currentlyPlaying)
2023-01-14 01:41:52 +00:00
go HandleLibraryRadio(m.ctx, m.client)
case *spotify.SimpleAlbum, *spotify.FullAlbum:
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Stating radio for" + currentlyPlaying)
go HandleAlbumRadio(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum).ID)
default:
2023-01-11 17:45:29 +00:00
currentlyPlaying = m.list.SelectedItem().FilterValue()
m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandleRadio(m.ctx, m.client, m.list.SelectedItem().(mainItem).ID)
2023-01-09 04:42:02 +00:00
}
}
case tea.MouseMsg:
if msg.Type == 5 {
m.list.CursorUp()
}
if msg.Type == 6 {
m.list.CursorDown()
}
case tea.WindowSizeMsg:
2023-01-12 02:45:22 +00:00
h, v := DocStyle.GetFrameSize()
2023-01-13 09:12:31 +00:00
m.list.SetSize(msg.Width-h, msg.Height-v-1)
}
2023-01-09 04:42:02 +00:00
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
2023-01-14 01:41:52 +00:00
func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model, error) {
2023-01-11 21:34:02 +00:00
playing, _ := client.PlayerCurrentlyPlaying(ctx)
if playing.Playing {
currentlyPlaying = "Now playing " + playing.Item.Name + " by " + playing.Item.Artists[0].Name
}
2023-01-11 17:45:29 +00:00
items := []list.Item{}
var err error
switch mode {
2023-01-14 01:41:52 +00:00
case Main:
2023-01-11 17:45:29 +00:00
items, err = MainView(ctx, client)
if err != nil {
return nil, err
}
2023-01-14 01:41:52 +00:00
case Devices:
2023-01-11 17:45:29 +00:00
items, err = DeviceView(ctx, client)
if err != nil {
return nil, err
}
2023-01-14 01:41:52 +00:00
case Tracks:
2023-01-11 17:45:29 +00:00
items, err = SavedTracksView(ctx, client)
if err != nil {
return nil, err
}
}
2023-01-09 18:54:21 +00:00
m := mainModel{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
ctx: ctx,
client: client,
2023-01-11 17:45:29 +00:00
mode: mode,
2023-01-09 18:54:21 +00:00
}
m.list.Title = "GOSPT"
2023-01-11 17:45:29 +00:00
go m.Tick()
2023-01-11 22:24:31 +00:00
m.list.DisableQuitKeybindings()
2023-01-11 17:45:29 +00:00
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
2023-01-11 22:28:34 +00:00
key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "back")),
2023-01-13 20:09:59 +00:00
key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "search")),
2023-01-11 22:28:34 +00:00
key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "quit")),
key.NewBinding(key.WithKeys("ctrl"+"r"), key.WithHelp("ctrl+r", "start radio")),
key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "select device")),
}
}
m.list.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "back")),
2023-01-13 20:09:59 +00:00
key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "search")),
2023-01-11 22:28:34 +00:00
key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "quit")),
2023-01-11 17:45:29 +00:00
key.NewBinding(key.WithKeys("ctrl"+"r"), key.WithHelp("ctrl+r", "start radio")),
key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "select device")),
}
}
2023-01-13 09:12:31 +00:00
input := textinput.New()
input.Prompt = "$ "
input.Placeholder = "Search..."
input.CharLimit = 250
input.Width = 50
m.input = input
2023-01-09 18:54:21 +00:00
return m, nil
}