gospt/src/tui/main.go

732 lines
20 KiB
Go
Raw Normal View History

2023-01-09 04:42:02 +00:00
package tui
import (
"fmt"
"strings"
2023-01-11 17:45:29 +00:00
"time"
2023-01-09 04:42:02 +00:00
2023-03-10 07:48:02 +00:00
"git.asdf.cafe/abs3nt/gospt/src/commands"
"git.asdf.cafe/abs3nt/gospt/src/gctx"
2023-01-09 04:42:02 +00:00
2023-01-27 06:28:57 +00:00
"github.com/atotto/clipboard"
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-30 02:30:33 +00:00
"github.com/charmbracelet/bubbles/progress"
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
2023-01-31 22:27:36 +00:00
DocStyle = lipgloss.NewStyle().Margin(0, 2).Border(lipgloss.DoubleBorder(), true, true, true, true)
currentlyPlaying *spotify.CurrentlyPlaying
playbackContext string
2023-01-11 17:45:29 +00:00
main_updates chan *mainModel
page = 1
loading = false
2023-01-11 17:45:29 +00:00
)
2023-01-11 04:46:15 +00:00
2023-01-14 01:41:52 +00:00
type Mode string
const (
2023-01-14 20:20:41 +00:00
Album Mode = "album"
2023-04-07 01:11:06 +00:00
ArtistAlbum Mode = "artistalbum"
Artist Mode = "artist"
Artists Mode = "artists"
2023-04-16 07:05:23 +00:00
Queue Mode = "queue"
2023-04-07 01:11:06 +00:00
Tracks Mode = "tracks"
Albums Mode = "albums"
Main Mode = "main"
Playlists Mode = "playlists"
Playlist Mode = "playlist"
Devices Mode = "devices"
Search Mode = "search"
SearchAlbums Mode = "searchalbums"
SearchAlbum Mode = "searchalbum"
SearchArtists Mode = "searchartists"
SearchArtist Mode = "searchartist"
SearchArtistAlbum Mode = "searchartistalbum"
SearchTracks Mode = "searchtracks"
SearchPlaylists Mode = "searchplaylsits"
SearchPlaylist Mode = "searchplaylist"
2023-01-14 01:41:52 +00:00
)
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
}
2023-01-14 20:20:41 +00:00
type SearchResults struct {
Tracks *spotify.FullTrackPage
Artists *spotify.FullArtistPage
Playlists *spotify.SimplePlaylistPage
Albums *spotify.SimpleAlbumPage
}
2023-01-09 04:42:02 +00:00
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 {
list list.Model
input textinput.Model
ctx *gctx.Context
2023-02-17 22:08:25 +00:00
commands *commands.Commands
mode Mode
playlist spotify.SimplePlaylist
artist spotify.SimpleArtist
album spotify.SimpleAlbum
searchResults *SearchResults
progress progress.Model
playing *spotify.CurrentlyPlaying
playbackContext string
search string
2023-01-14 20:20:41 +00:00
}
func (m *mainModel) PlayRadio() {
m.list.NewStatusMessage("Starting radio for " + m.list.SelectedItem().(mainItem).Title())
2023-01-14 20:20:41 +00:00
selectedItem := m.list.SelectedItem().(mainItem).SpotifyItem
2023-04-07 01:11:06 +00:00
switch item := selectedItem.(type) {
2023-01-14 20:20:41 +00:00
case spotify.SimplePlaylist:
2023-04-07 01:11:06 +00:00
go HandlePlaylistRadio(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
return
case *spotify.SavedTrackPage:
2023-02-17 22:08:25 +00:00
go HandleLibraryRadio(m.ctx, m.commands)
2023-01-14 20:20:41 +00:00
return
case spotify.SimpleAlbum:
2023-04-07 01:11:06 +00:00
go HandleAlbumRadio(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
return
case spotify.FullAlbum:
2023-04-07 01:11:06 +00:00
go HandleAlbumRadio(m.ctx, m.commands, item.SimpleAlbum)
2023-01-14 20:20:41 +00:00
return
case spotify.SimpleArtist:
2023-04-07 01:11:06 +00:00
go HandleArtistRadio(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
return
case spotify.FullArtist:
2023-04-07 01:11:06 +00:00
go HandleArtistRadio(m.ctx, m.commands, item.SimpleArtist)
2023-01-14 20:20:41 +00:00
return
case spotify.SimpleTrack:
2023-04-07 01:11:06 +00:00
go HandleRadio(m.ctx, m.commands, item)
return
case spotify.FullTrack:
2023-04-07 01:11:06 +00:00
go HandleRadio(m.ctx, m.commands, item.SimpleTrack)
return
case spotify.PlaylistTrack:
2023-04-07 01:11:06 +00:00
go HandleRadio(m.ctx, m.commands, item.Track.SimpleTrack)
return
case spotify.SavedTrack:
2023-04-07 01:11:06 +00:00
go HandleRadio(m.ctx, m.commands, item.SimpleTrack)
2023-01-14 20:20:41 +00:00
return
}
2023-01-09 04:42:02 +00:00
}
2023-01-14 01:41:52 +00:00
func (m *mainModel) GoBack() (tea.Cmd, error) {
page = 1
2023-01-14 01:41:52 +00:00
switch m.mode {
case Main:
return tea.Quit, nil
2023-04-16 07:05:23 +00:00
case Albums, Artists, Tracks, Playlist, Devices, Search, Queue:
2023-01-14 01:41:52 +00:00
m.mode = Main
2023-02-17 22:08:25 +00:00
new_items, err := MainView(m.ctx, m.commands)
2023-01-14 01:41:52 +00:00
if err != nil {
2023-04-07 01:11:06 +00:00
return nil, err
2023-01-14 01:41:52 +00:00
}
m.list.SetItems(new_items)
case Album:
m.mode = Albums
2023-02-17 22:08:25 +00:00
new_items, err := AlbumsView(m.ctx, m.commands)
2023-01-14 01:41:52 +00:00
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
case Artist:
m.mode = Artists
2023-02-17 22:08:25 +00:00
new_items, err := ArtistsView(m.ctx, m.commands)
2023-01-14 01:41:52 +00:00
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
case ArtistAlbum:
m.mode = Artist
2023-02-17 22:08:25 +00:00
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.commands)
2023-01-14 01:41:52 +00:00
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
case SearchArtists, SearchTracks, SearchAlbums, SearchPlaylists:
m.mode = Search
2023-02-17 22:08:25 +00:00
items, result, err := SearchView(m.ctx, m.commands, m.search)
2023-01-14 01:41:52 +00:00
if err != nil {
return nil, err
}
2023-01-14 20:20:41 +00:00
m.searchResults = result
2023-01-14 01:41:52 +00:00
m.list.SetItems(items)
case SearchArtist:
m.mode = SearchArtists
2023-02-17 22:08:25 +00:00
new_items, err := SearchArtistsView(m.ctx, m.commands, m.searchResults.Artists)
2023-01-14 20:20:41 +00:00
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
case SearchArtistAlbum:
m.mode = SearchArtist
2023-02-17 22:08:25 +00:00
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
2023-01-14 01:41:52 +00:00
case SearchAlbum:
m.mode = SearchAlbums
2023-02-17 22:08:25 +00:00
new_items, err := SearchAlbumsView(m.ctx, m.commands, m.searchResults.Albums)
2023-01-14 20:20:41 +00:00
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
2023-01-14 01:41:52 +00:00
case SearchPlaylist:
m.mode = SearchPlaylists
2023-02-17 22:08:25 +00:00
new_items, err := SearchPlaylistsView(m.ctx, m.commands, m.searchResults.Playlists)
2023-01-14 20:20:41 +00:00
if err != nil {
return nil, err
}
m.list.SetItems(new_items)
2023-01-14 01:41:52 +00:00
default:
page = 0
}
return nil, nil
}
2023-01-27 06:28:57 +00:00
type SpotifyUrl struct {
ExternalURLs map[string]string
}
func (m *mainModel) CopyToClipboard() error {
item := m.list.SelectedItem().(mainItem).SpotifyItem
2023-04-07 01:11:06 +00:00
switch converted := item.(type) {
2023-01-27 06:28:57 +00:00
case spotify.SimplePlaylist:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case *spotify.FullPlaylist:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case spotify.SimpleAlbum:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case *spotify.FullAlbum:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case spotify.SimpleArtist:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case *spotify.FullArtist:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case spotify.SimpleTrack:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case spotify.PlaylistTrack:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.Track.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case spotify.SavedTrack:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
case spotify.FullTrack:
2023-04-07 01:11:06 +00:00
clipboard.WriteAll(converted.ExternalURLs["spotify"])
m.list.NewStatusMessage("Copying link to " + m.list.SelectedItem().(mainItem).Title())
2023-01-27 06:28:57 +00:00
}
return nil
}
2023-01-14 20:20:41 +00:00
func (m *mainModel) SelectItem() error {
switch m.mode {
2023-04-16 07:05:23 +00:00
case Queue:
page = 1
go func() {
HandleSkipWithinContext(m.ctx, m.commands, m.list.Index())
new_items, err := QueueView(m.ctx, m.commands)
if err != nil {
return
}
m.list.SetItems(new_items)
m.list.ResetSelected()
}()
2023-01-14 20:20:41 +00:00
case Search:
page = 1
2023-04-07 01:11:06 +00:00
switch item := m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
2023-01-14 20:20:41 +00:00
case *spotify.FullArtistPage:
m.mode = SearchArtists
2023-04-07 01:11:06 +00:00
new_items, err := SearchArtistsView(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SimpleAlbumPage:
m.mode = SearchAlbums
2023-04-07 01:11:06 +00:00
new_items, err := SearchAlbumsView(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SimplePlaylistPage:
m.mode = SearchPlaylists
2023-04-07 01:11:06 +00:00
new_items, err := SearchPlaylistsView(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.FullTrackPage:
m.mode = SearchTracks
2023-04-07 01:11:06 +00:00
new_items, err := SearchTracksView(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
}
case SearchArtists:
page = 1
2023-01-14 20:20:41 +00:00
m.mode = SearchArtist
m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist)
2023-02-17 22:08:25 +00:00
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case SearchArtist:
page = 1
2023-01-14 20:20:41 +00:00
m.mode = SearchArtistAlbum
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
2023-02-17 22:08:25 +00:00
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case SearchAlbums:
page = 1
2023-01-14 20:20:41 +00:00
m.mode = SearchAlbum
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
2023-02-17 22:08:25 +00:00
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case SearchPlaylists:
page = 1
2023-01-14 20:20:41 +00:00
m.mode = SearchPlaylist
playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist)
m.playlist = playlist
2023-02-17 22:08:25 +00:00
new_items, err := PlaylistView(m.ctx, m.commands, playlist)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case Main:
page = 1
2023-04-07 01:11:06 +00:00
switch item := m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
2023-04-16 07:05:23 +00:00
case spotify.Queue:
m.mode = Queue
new_items, err := QueueView(m.ctx, m.commands)
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
2023-01-14 20:20:41 +00:00
case *spotify.FullArtistCursorPage:
m.mode = Artists
2023-02-17 22:08:25 +00:00
new_items, err := ArtistsView(m.ctx, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SavedAlbumPage:
m.mode = Albums
2023-02-17 22:08:25 +00:00
new_items, err := AlbumsView(m.ctx, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case spotify.SimplePlaylist:
m.mode = Playlist
2023-04-07 01:11:06 +00:00
m.playlist = item
new_items, err := PlaylistView(m.ctx, m.commands, item)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SavedTrackPage:
m.mode = Tracks
2023-02-17 22:08:25 +00:00
new_items, err := SavedTracksView(m.ctx, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
}
case Albums:
page = 1
2023-01-14 20:20:41 +00:00
m.mode = Album
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
2023-02-17 22:08:25 +00:00
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case Artist:
m.mode = ArtistAlbum
m.album = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleAlbum)
2023-02-17 22:08:25 +00:00
new_items, err := AlbumTracksView(m.ctx, m.album.ID, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case Artists:
m.mode = Artist
m.artist = m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimpleArtist)
2023-02-17 22:08:25 +00:00
new_items, err := ArtistAlbumsView(m.ctx, m.artist.ID, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return err
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case Album, ArtistAlbum, SearchArtistAlbum, SearchAlbum:
2023-02-17 22:08:25 +00:00
go HandlePlayWithContext(m.ctx, m.commands, &m.album.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.TotalPages))
2023-01-14 20:20:41 +00:00
case Playlist, SearchPlaylist:
2023-02-17 22:08:25 +00:00
go HandlePlayWithContext(m.ctx, m.commands, &m.playlist.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
2023-01-14 20:20:41 +00:00
case Tracks:
2023-02-17 22:08:25 +00:00
go HandlePlayLikedSong(m.ctx, m.commands, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
2023-01-14 20:20:41 +00:00
case SearchTracks:
2023-02-17 22:08:25 +00:00
go HandlePlayTrack(m.ctx, m.commands, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.FullTrack).ID)
2023-01-14 20:20:41 +00:00
case Devices:
2023-02-17 22:08:25 +00:00
go HandleSetDevice(m.ctx, m.commands, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlayerDevice))
2023-01-14 20:20:41 +00:00
m.list.NewStatusMessage("Setting device to " + m.list.SelectedItem().FilterValue())
m.mode = "main"
m.list.NewStatusMessage("Setting view to main")
2023-02-17 22:08:25 +00:00
new_items, err := MainView(m.ctx, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
2023-02-01 05:37:10 +00:00
return err
2023-01-14 20:20:41 +00:00
}
m.list.SetItems(new_items)
}
return nil
}
2023-04-07 01:11:06 +00:00
func (m *mainModel) Init() tea.Cmd {
2023-01-11 17:45:29 +00:00
main_updates = make(chan *mainModel)
2023-01-30 02:30:33 +00:00
return Tick()
2023-01-09 04:42:02 +00:00
}
2023-01-30 02:30:33 +00:00
type tickMsg time.Time
func Tick() tea.Cmd {
return tea.Tick(time.Second*1, func(t time.Time) tea.Msg {
return tickMsg(t)
})
2023-01-11 17:45:29 +00:00
}
func (m *mainModel) TickPlayback() {
2023-02-17 22:08:25 +00:00
playing, _ := m.commands.Client().PlayerCurrentlyPlaying(m.ctx)
if playing != nil && playing.Playing && playing.Item != nil {
currentlyPlaying = playing
playbackContext, _ = m.getContext(playing)
}
ticker := time.NewTicker(1 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
2023-02-17 22:08:25 +00:00
playing, _ := m.commands.Client().PlayerCurrentlyPlaying(m.ctx)
if playing != nil && playing.Playing && playing.Item != nil {
currentlyPlaying = playing
playbackContext, _ = m.getContext(playing)
}
case <-quit:
ticker.Stop()
return
}
}
}()
}
2023-04-07 01:11:06 +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" {
2023-02-17 22:08:25 +00:00
items, result, err := SearchView(m.ctx, m.commands, m.input.Value())
2023-01-14 00:35:42 +00:00
if err != nil {
return false, tea.Quit
}
2023-01-14 20:20:41 +00:00
m.searchResults = result
2023-01-14 00:35:42 +00:00
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
}
func (m *mainModel) getContext(playing *spotify.CurrentlyPlaying) (string, error) {
context := playing.PlaybackContext
uri_split := strings.Split(string(context.URI), ":")
if len(uri_split) < 3 {
return "", fmt.Errorf("NO URI")
}
id := strings.Split(string(context.URI), ":")[2]
switch context.Type {
case "album":
2023-02-17 22:08:25 +00:00
album, err := m.commands.Client().GetAlbum(m.ctx, spotify.ID(id))
if err != nil {
return "", err
}
return album.Name, nil
case "playlist":
2023-02-17 22:08:25 +00:00
playlist, err := m.commands.Client().GetPlaylist(m.ctx, spotify.ID(id))
if err != nil {
return "", err
}
return playlist.Name, nil
case "artist":
2023-02-17 22:08:25 +00:00
artist, err := m.commands.Client().GetArtist(m.ctx, spotify.ID(id))
if err != nil {
return "", err
}
return artist.Name, nil
}
return "", nil
}
2023-04-07 01:11:06 +00:00
func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2023-01-14 20:20:41 +00:00
// Update list items from LoadMore
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-14 20:20:41 +00:00
// Call for more items if needed
if m.list.Paginator.Page == m.list.Paginator.TotalPages-1 && m.list.Cursor() == 0 && !loading {
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
}
}
2023-01-14 20:20:41 +00:00
// Handle user input
2023-01-09 04:42:02 +00:00
switch msg := msg.(type) {
2023-01-30 02:30:33 +00:00
case tickMsg:
playing := currentlyPlaying
2023-01-30 02:30:33 +00:00
if playing != nil && playing.Playing && playing.Item != nil {
cmd := m.progress.SetPercent(float64(playing.Progress) / float64(playing.Item.Duration))
m.playing = playing
m.playbackContext = playbackContext
2023-04-16 07:05:23 +00:00
if m.mode == Queue {
if m.list.Items()[0].(mainItem).SpotifyItem.(spotify.FullTrack).Name != playing.Item.Name {
go func() {
new_items, err := QueueView(m.ctx, m.commands)
if err != nil {
return
}
m.list.SetItems(new_items)
}()
}
}
2023-01-30 02:30:33 +00:00
return m, tea.Batch(Tick(), cmd)
}
return m, Tick()
2023-01-30 02:30:33 +00:00
case progress.FrameMsg:
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(progress.Model)
m.list.NewStatusMessage(
fmt.Sprintf("Now playing %s by %s - %s %s/%s : %s",
m.playing.Item.Name,
m.playing.Item.Artists[0].Name,
m.progress.View(),
(time.Duration(m.playing.Progress) * time.Millisecond).Round(time.Second),
(time.Duration(m.playing.Item.Duration) * time.Millisecond).Round(time.Second),
m.playbackContext),
)
2023-01-30 02:30:33 +00:00
return m, cmd
2023-01-09 04:42:02 +00:00
case tea.KeyMsg:
2023-01-14 20:20:41 +00:00
// quit
2023-01-14 00:35:42 +00:00
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
2023-01-27 06:28:57 +00:00
if msg.String() == "c" {
err := m.CopyToClipboard()
if err != nil {
return m, tea.Quit
}
}
if msg.String() == ">" {
2023-02-17 22:08:25 +00:00
go HandleSeek(m.ctx, m.commands, true)
}
if msg.String() == "<" {
2023-02-17 22:08:25 +00:00
go HandleSeek(m.ctx, m.commands, false)
}
if msg.String() == "+" {
2023-02-17 22:08:25 +00:00
go HandleVolume(m.ctx, m.commands, true)
}
if msg.String() == "-" {
2023-02-17 22:08:25 +00:00
go HandleVolume(m.ctx, m.commands, false)
}
2023-01-14 20:20:41 +00:00
// search input
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
}
2023-01-14 20:20:41 +00:00
// start search
if msg.String() == "s" || msg.String() == "/" {
2023-01-13 09:12:31 +00:00
m.input.Focus()
}
2023-01-14 20:20:41 +00:00
// enter device selection
2023-01-11 17:45:29 +00:00
if msg.String() == "d" {
2023-01-14 20:20:41 +00:00
m.mode = Devices
2023-02-17 22:08:25 +00:00
new_items, err := DeviceView(m.ctx, m.commands)
2023-01-14 20:20:41 +00:00
if err != nil {
return m, tea.Quit
2023-01-11 17:45:29 +00:00
}
2023-01-14 20:20:41 +00:00
m.list.SetItems(new_items)
m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to devices")
2023-01-09 04:42:02 +00:00
}
2023-01-14 20:20:41 +00:00
// go back
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 {
2023-02-01 05:37:10 +00:00
return m, tea.Quit
2023-01-11 17:45:29 +00:00
}
2023-01-14 21:19:01 +00:00
m.list.ResetSelected()
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-14 20:20:41 +00:00
// select item
2023-01-11 17:45:29 +00:00
if msg.String() == "enter" || msg.String() == "spacebar" {
2023-01-14 20:20:41 +00:00
err := m.SelectItem()
if err != nil {
return m, tea.Quit
2023-01-11 17:45:29 +00:00
}
}
2023-01-14 20:20:41 +00:00
// start radio
2023-01-11 17:45:29 +00:00
if msg.String() == "ctrl+r" {
2023-01-14 20:20:41 +00:00
m.PlayRadio()
2023-01-09 04:42:02 +00:00
}
2023-01-14 20:20:41 +00:00
// handle mouse
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()
}
2023-01-14 20:20:41 +00:00
// window size -1 to handle search bar
2023-01-09 04:42:02 +00:00
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-14 20:20:41 +00:00
// return
2023-01-09 04:42:02 +00:00
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
2023-02-17 22:08:25 +00:00
func InitMain(ctx *gctx.Context, c *commands.Commands, mode Mode) (tea.Model, error) {
2023-01-30 02:30:33 +00:00
prog := progress.New(progress.WithColorProfile(2), progress.WithoutPercentage())
2023-01-15 20:35:49 +00:00
var err error
2023-01-15 12:48:12 +00:00
lipgloss.SetColorProfile(2)
2023-01-11 17:45:29 +00:00
items := []list.Item{}
switch mode {
2023-01-14 01:41:52 +00:00
case Main:
2023-02-17 22:08:25 +00:00
items, err = MainView(ctx, c)
2023-01-11 17:45:29 +00:00
if err != nil {
return nil, err
}
2023-01-14 01:41:52 +00:00
case Devices:
2023-02-17 22:08:25 +00:00
items, err = DeviceView(ctx, c)
2023-01-11 17:45:29 +00:00
if err != nil {
return nil, err
}
2023-01-14 01:41:52 +00:00
case Tracks:
2023-02-17 22:08:25 +00:00
items, err = SavedTracksView(ctx, c)
2023-01-11 17:45:29 +00:00
if err != nil {
return nil, err
}
}
2023-04-07 01:11:06 +00:00
m := &mainModel{
2023-01-30 02:30:33 +00:00
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
ctx: ctx,
2023-02-17 22:08:25 +00:00
commands: c,
2023-01-30 02:30:33 +00:00
mode: mode,
progress: prog,
2023-01-09 18:54:21 +00:00
}
m.list.Title = "GOSPT"
go m.TickPlayback()
2023-01-30 02:30:33 +00:00
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")),
2023-01-27 06:28:57 +00:00
key.NewBinding(key.WithKeys("ctrl"+"shift"+"c"), key.WithHelp("ctrl+shift+c", "copy url")),
2023-01-11 22:28:34 +00:00
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-15 12:55:55 +00:00
key.NewBinding(key.WithKeys(">"), key.WithHelp(">", "seek forward")),
key.NewBinding(key.WithKeys("<"), key.WithHelp("<", "seek backward")),
key.NewBinding(key.WithKeys("+"), key.WithHelp("+", "volume up")),
key.NewBinding(key.WithKeys("-"), key.WithHelp("-", "volume down")),
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")),
2023-01-27 06:28:57 +00:00
key.NewBinding(key.WithKeys("ctrl"+"shift"+"c"), key.WithHelp("ctrl+shift+c", "copy url")),
2023-01-11 17:45:29 +00:00
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
}