Compare commits

..

No commits in common. "d92095d8dbc7c92fde2084f002a761788ba320d4" and "43491a22096580f688f3f61ef34159d06406530e" have entirely different histories.

12 changed files with 697 additions and 328 deletions

20
cmd/playlists.go Normal file
View File

@ -0,0 +1,20 @@
package cmd
import (
"gospt/internal/tui"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(playListsCmd)
}
var playListsCmd = &cobra.Command{
Use: "playlists",
Short: "Uses tui to show users playlists",
Long: `Opens tui showing all users playlists`,
Run: func(cmd *cobra.Command, args []string) {
tui.DisplayPlaylists(ctx, client)
},
}

View File

@ -15,6 +15,6 @@ var setDeviceCmd = &cobra.Command{
Short: "Shows tui to pick active device", Short: "Shows tui to pick active device",
Long: `Allows setting or changing the active spotify device, shown in a tui`, Long: `Allows setting or changing the active spotify device, shown in a tui`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
tui.StartTea(ctx, client, "devices") tui.DisplayDevices(ctx, client)
}, },
} }

View File

@ -1,9 +1,6 @@
package cmd package cmd
import ( import (
"os"
"path/filepath"
"gospt/internal/tui" "gospt/internal/tui"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -17,11 +14,7 @@ var tracksCmd = &cobra.Command{
Use: "tracks", Use: "tracks",
Short: "Opens saved tracks", Short: "Opens saved tracks",
Long: `Uses TUI to open a list of saved tracks`, Long: `Uses TUI to open a list of saved tracks`,
RunE: func(cmd *cobra.Command, args []string) error { Run: func(cmd *cobra.Command, args []string) {
configDir, _ := os.UserConfigDir() tui.DisplayList(ctx, client)
if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err != nil {
return tui.StartTea(ctx, client, "devices")
}
return tui.StartTea(ctx, client, "tracks")
}, },
} }

View File

@ -20,8 +20,8 @@ var tuiCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
configDir, _ := os.UserConfigDir() configDir, _ := os.UserConfigDir()
if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err != nil { if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err != nil {
return tui.StartTea(ctx, client, "devices") return tui.StartTea(ctx, client)
} }
return tui.StartTea(ctx, client, "main") return tui.DisplayMain(ctx, client)
}, },
} }

View File

View File

@ -489,6 +489,7 @@ func SetDevice(ctx *gctx.Context, client *spotify.Client, device spotify.PlayerD
if err != nil { if err != nil {
return err return err
} }
fmt.Println("Your device has been set to: ", device.Name)
return nil return nil
} }

105
internal/tui/device.go Normal file
View File

@ -0,0 +1,105 @@
package tui
import (
"fmt"
"os"
"gospt/internal/commands"
"gospt/internal/gctx"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/zmb3/spotify/v2"
)
var deviceDocStyle = lipgloss.NewStyle().Margin(1, 2)
type deviceItem struct {
spotify.PlayerDevice
}
func (i deviceItem) Title() string { return i.Name }
func (i deviceItem) Description() string {
return fmt.Sprintf("%s - active: %t", i.ID, i.Active)
}
func (i deviceItem) FilterValue() string { return i.Title() }
type deviceModel struct {
list list.Model
page int
ctx *gctx.Context
client *spotify.Client
}
func (m deviceModel) Init() tea.Cmd {
return nil
}
func (m deviceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if msg.String() == "enter" {
device := m.list.SelectedItem()
var err error
err = commands.SetDevice(m.ctx, m.client, device.(deviceItem).PlayerDevice)
if err != nil {
m.ctx.Printf(err.Error())
}
err = DisplayMain(m.ctx, m.client)
if err != nil {
return m, tea.Quit
}
return m, tea.Quit
}
case tea.MouseMsg:
if msg.Type == 5 {
m.list.CursorUp()
}
if msg.Type == 6 {
m.list.CursorDown()
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m deviceModel) View() string {
return docStyle.Render(m.list.View())
}
func DisplayDevices(ctx *gctx.Context, client *spotify.Client) error {
items := []list.Item{}
devices, err := client.PlayerDevices(ctx)
if err != nil {
return err
}
for _, device := range devices {
items = append(items, deviceItem{
device,
})
}
if err != nil {
return err
}
m := deviceModel{list: list.New(items, list.NewDefaultDelegate(), 0, 0), page: 1, ctx: ctx, client: client}
m.list.Title = "Saved Tracks"
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
fmt.Println("DEVICE SET AND SAVED")
return nil
}

197
internal/tui/list.go Normal file
View File

@ -0,0 +1,197 @@
package tui
import (
"fmt"
"os"
"time"
"gospt/internal/commands"
"gospt/internal/gctx"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/zmb3/spotify/v2"
)
var (
track_updates chan *model
docStyle = lipgloss.NewStyle().Margin(1, 2)
)
type item struct {
Name string
Duration string
Artist spotify.SimpleArtist
ID spotify.ID
spotify.SavedTrack
}
func (i item) Title() string { return i.Name }
func (i item) Description() string {
return fmt.Sprint(i.Duration, " by ", i.Artist.Name)
}
func (i item) FilterValue() string { return i.Title() + i.Artist.Name }
type model struct {
list list.Model
page int
ctx *gctx.Context
client *spotify.Client
}
func (m model) Init() tea.Cmd {
track_updates = make(chan *model)
return nil
}
func (m *model) LoadMoreItems() {
tracks, err := commands.TrackList(m.ctx, m.client, (m.page + 1))
if err != nil {
return
}
m.page++
items := []list.Item{}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Name,
Artist: track.Artists[0],
Duration: track.TimeDuration().Round(time.Second).String(),
ID: track.ID,
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
track_updates <- m
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
select {
case msg := <-track_updates:
m.list.SetItems(msg.list.Items())
default:
}
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 {
// if last request was still full request more
if len(m.list.Items())%50 == 0 {
go m.LoadMoreItems()
}
}
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "backspace" || msg.String() == "q" || msg.String() == "esc" {
m, err := InitMain(m.ctx, m.client)
if err != nil {
fmt.Println("UH OH")
}
P = tea.NewProgram(m, tea.WithAltScreen())
if err := P.Start(); err != nil {
return m, tea.Quit
}
}
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if msg.String() == "ctrl+r" {
track := m.list.SelectedItem()
err := commands.RadioGivenSong(m.ctx, m.client, track.(item).ID, 0)
if err != nil {
return m, tea.Quit
}
}
if msg.String() == "enter" {
var err error
err = commands.PlayLikedSongs(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
if err != nil {
m.ctx.Printf(err.Error())
}
}
case tea.MouseMsg:
if msg.Type == 5 {
m.list.CursorUp()
}
if msg.Type == 6 {
m.list.CursorDown()
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model) View() string {
return docStyle.Render(m.list.View())
}
func DisplayList(ctx *gctx.Context, client *spotify.Client) error {
items := []list.Item{}
tracks, err := commands.TrackList(ctx, client, 1)
if err != nil {
return err
}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Name,
Artist: track.Artists[0],
Duration: track.TimeDuration().Round(time.Second).String(),
ID: track.ID,
})
}
m := model{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx,
client: client,
}
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("ctrl", "r"), key.WithHelp("ctrl+r", "start radio")),
}
}
m.list.Title = "Saved Tracks"
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
return nil
}
func InitSavedTracks(ctx *gctx.Context, client *spotify.Client) (tea.Model, error) {
items := []list.Item{}
tracks, err := commands.TrackList(ctx, client, 1)
if err != nil {
return nil, err
}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Name,
Artist: track.Artists[0],
Duration: track.TimeDuration().Round(time.Second).String(),
ID: track.ID,
})
}
m := model{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx,
client: client,
}
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("ctrl", "r"), key.WithHelp("ctrl+r", "start radio")),
}
}
m.list.Title = "Saved Tracks"
return m, nil
}

View File

@ -3,35 +3,21 @@ package tui
import ( import (
"fmt" "fmt"
"os" "os"
"time"
"gospt/internal/commands" "gospt/internal/commands"
"gospt/internal/gctx" "gospt/internal/gctx"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
) )
var ( var main_updates chan *mainModel
currentlyPlaying string
main_updates chan *mainModel
page = 1
docStyle = lipgloss.NewStyle().Margin(1, 2)
)
type mainItem struct { type mainItem struct {
Name string Name string
Duration string
Artist spotify.SimpleArtist
ID spotify.ID
Desc string Desc string
SpotifyItem any SpotifyItem any
Device spotify.PlayerDevice
spotify.SavedTrack
} }
func (i mainItem) Title() string { return i.Name } func (i mainItem) Title() string { return i.Name }
@ -39,135 +25,43 @@ func (i mainItem) Description() string { return i.Desc }
func (i mainItem) FilterValue() string { return i.Title() + i.Desc } func (i mainItem) FilterValue() string { return i.Title() + i.Desc }
type mainModel struct { type mainModel struct {
list list.Model list list.Model
ctx *gctx.Context page int
client *spotify.Client ctx *gctx.Context
mode string client *spotify.Client
playlist spotify.SimplePlaylist
} }
func (m mainModel) Init() tea.Cmd { func (m mainModel) Init() tea.Cmd {
main_updates = make(chan *mainModel)
return nil return nil
} }
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)
currentlyPlaying = "Now playing " + playing.Item.Name + " by " + playing.Item.Artists[0].Name
case <-quit:
ticker.Stop()
return
}
}
}()
}
func HandlePlay(ctx *gctx.Context, client *spotify.Client, uri *spotify.URI, pos int) {
var err error
err = commands.PlaySongInPlaylist(ctx, client, uri, pos)
if err != nil {
fmt.Println()
os.Exit(1)
}
}
func HandleRadio(ctx *gctx.Context, client *spotify.Client, id spotify.ID) {
err := commands.RadioGivenSong(ctx, client, id, 0)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func (m *mainModel) LoadMoreItems() { func (m *mainModel) LoadMoreItems() {
switch m.mode { playlists, err := commands.Playlists(m.ctx, m.client, (m.page + 1))
case "main":
playlists, err := commands.Playlists(m.ctx, m.client, (page + 1))
page++
if err != nil {
return
}
items := []list.Item{}
for _, playlist := range playlists.Playlists {
items = append(items, mainItem{
Name: playlist.Name,
Desc: playlist.Description,
SpotifyItem: playlist,
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
main_updates <- m
return
case "playlist":
tracks, err := commands.PlaylistTracks(m.ctx, m.client, m.playlist.ID, (page + 1))
page++
if err != nil {
return
}
items := []mainItem{}
for _, track := range tracks.Tracks {
items = append(items, mainItem{
Name: track.Track.Name,
Artist: track.Track.Artists[0],
Duration: track.Track.TimeDuration().Round(time.Second).String(),
ID: track.Track.ID,
Desc: track.Track.Artists[0].Name + " - " + track.Track.TimeDuration().Round(time.Second).String(),
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
main_updates <- m
return
case "tracks":
tracks, err := commands.TrackList(m.ctx, m.client, (page + 1))
page++
if err != nil {
return
}
page++
items := []list.Item{}
for _, track := range tracks.Tracks {
items = append(items, mainItem{
Name: track.Name,
Artist: track.Artists[0],
Duration: track.TimeDuration().Round(time.Second).String(),
ID: track.ID,
Desc: track.Artists[0].Name + " - " + track.TimeDuration().Round(time.Second).String(),
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
main_updates <- m
return
}
}
func HandlePlayLikedSong(ctx *gctx.Context, client *spotify.Client, position int) {
err := commands.PlayLikedSongs(ctx, client, position)
if err != nil { if err != nil {
fmt.Println(err.Error()) return
os.Exit(1)
} }
m.page++
items := []list.Item{}
for _, playlist := range playlists.Playlists {
items = append(items, mainItem{
Name: playlist.Name,
Desc: playlist.Description,
SpotifyItem: playlist,
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
main_updates <- m
} }
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.list.NewStatusMessage(currentlyPlaying)
select { select {
case update := <-main_updates: case msg := <-main_updates:
m.list.SetItems(update.list.Items()) m.list.SetItems(msg.list.Items())
default: default:
} }
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 && m.list.Cursor() == 0 { if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 {
// if last request was still full request more // if last request was still full request more
if len(m.list.Items())%50 == 0 { if len(m.list.Items())%50 == 0 {
go m.LoadMoreItems() go m.LoadMoreItems()
@ -175,100 +69,33 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
if msg.String() == "d" { if msg.String() == "ctrl+c" || msg.String() == "q" || msg.String() == "esc" {
m.mode = "devices"
new_items, err := DeviceView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
m.list.SetItems(new_items)
m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to devices")
}
if msg.String() == "backspace" {
if m.mode == "playlist" || m.mode == "tracks" || m.mode == "devices" {
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)
} else {
return m, tea.Quit
}
m.list.ResetSelected()
}
if msg.String() == "ctrl+c" {
return m, tea.Quit return m, tea.Quit
} }
if msg.String() == "enter" || msg.String() == "spacebar" { if msg.String() == "enter" {
switch m.mode { switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
case "main": case spotify.SimplePlaylist:
switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) { playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist)
case spotify.SimplePlaylist: p, err := InitPlaylists(m.ctx, m.client, playlist)
m.mode = "playlist"
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())
os.Exit(1)
}
m.list.SetItems(new_items)
m.list.ResetSelected()
case *spotify.SavedTrackPage:
m.mode = "tracks"
m.list.NewStatusMessage("Setting view to saved tracks")
new_items, err := SavedTracksView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
m.list.SetItems(new_items)
m.list.ResetSelected()
m.list.NewStatusMessage("Setting view to tracks")
}
case "playlist":
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))
case "tracks":
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))
case "devices":
go HandleSetDevice(m.ctx, m.client, m.list.SelectedItem().(mainItem).Device)
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)
if err != nil { if err != nil {
fmt.Println(err.Error()) return m, tea.Quit
} }
m.list.SetItems(new_items) play := tea.NewProgram(p, tea.WithAltScreen(), tea.WithMouseCellMotion())
} if _, err := play.Run(); err != nil {
} return m, tea.Quit
if msg.String() == "ctrl+r" {
switch m.mode {
case "main":
switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) {
case spotify.SimplePlaylist:
m.list.NewStatusMessage("Not implemented yet")
case *spotify.SavedTrackPage:
m.list.NewStatusMessage("Not implemented yet")
} }
case "playlist": case *spotify.SavedTrackPage:
currentlyPlaying = m.list.SelectedItem().FilterValue() p, err := InitSavedTracks(m.ctx, m.client)
m.list.NewStatusMessage("Starting radio for " + currentlyPlaying) if err != nil {
go HandleRadio(m.ctx, m.client, m.list.SelectedItem().(mainItem).ID) return m, tea.Quit
case "tracks": }
currentlyPlaying = m.list.SelectedItem().FilterValue() play := tea.NewProgram(p, tea.WithAltScreen(), tea.WithMouseCellMotion())
m.list.NewStatusMessage("Playing " + currentlyPlaying) if _, err := play.Run(); err != nil {
go HandleRadio(m.ctx, m.client, m.list.SelectedItem().(mainItem).ID) return m, tea.Quit
}
return m, tea.Quit
} }
return m, tea.Quit
} }
case tea.MouseMsg: case tea.MouseMsg:
if msg.Type == 5 { if msg.Type == 5 {
@ -312,12 +139,11 @@ func DisplayMain(ctx *gctx.Context, client *spotify.Client) error {
} }
m := mainModel{ m := mainModel{
list: list.New(items, list.NewDefaultDelegate(), 0, 0), list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx, ctx: ctx,
client: client, client: client,
mode: "main",
} }
m.list.Title = "GOSPT" m.list.Title = "GOSPT"
go m.Tick()
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion()) p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
@ -328,44 +154,7 @@ func DisplayMain(ctx *gctx.Context, client *spotify.Client) error {
return nil return nil
} }
func PlaylistView(ctx *gctx.Context, client *spotify.Client, playlist spotify.SimplePlaylist) ([]list.Item, error) { func InitMain(ctx *gctx.Context, client *spotify.Client) (tea.Model, error) {
items := []list.Item{}
tracks, err := commands.PlaylistTracks(ctx, client, playlist.ID, 1)
if err != nil {
return nil, err
}
for _, track := range tracks.Tracks {
items = append(items, mainItem{
Name: track.Track.Name,
Artist: track.Track.Artists[0],
Duration: track.Track.TimeDuration().Round(time.Second).String(),
ID: track.Track.ID,
Desc: track.Track.Artists[0].Name + " - " + track.Track.TimeDuration().Round(time.Second).String(),
})
}
return items, nil
}
func SavedTracksView(ctx *gctx.Context, client *spotify.Client) ([]list.Item, error) {
items := []list.Item{}
tracks, err := commands.TrackList(ctx, client, 1)
if err != nil {
return nil, err
}
for _, track := range tracks.Tracks {
items = append(items, mainItem{
Name: track.Name,
Artist: track.Artists[0],
Duration: track.TimeDuration().Round(time.Second).String(),
ID: track.ID,
Desc: track.Artists[0].Name + " - " + track.TimeDuration().Round(time.Second).String(),
})
}
return items, err
}
func MainView(ctx *gctx.Context, client *spotify.Client) ([]list.Item, error) {
items := []list.Item{} items := []list.Item{}
saved_items, err := commands.TrackList(ctx, client, 1) saved_items, err := commands.TrackList(ctx, client, 1)
items = append(items, mainItem{ items = append(items, mainItem{
@ -384,68 +173,12 @@ func MainView(ctx *gctx.Context, client *spotify.Client) ([]list.Item, error) {
SpotifyItem: playlist, SpotifyItem: playlist,
}) })
} }
return items, nil
}
func InitMain(ctx *gctx.Context, client *spotify.Client, mode string) (tea.Model, error) {
items := []list.Item{}
var err error
switch mode {
case "main":
items, err = MainView(ctx, client)
if err != nil {
return nil, err
}
case "devices":
items, err = DeviceView(ctx, client)
if err != nil {
return nil, err
}
case "tracks":
items, err = SavedTracksView(ctx, client)
if err != nil {
return nil, err
}
}
m := mainModel{ m := mainModel{
list: list.New(items, list.NewDefaultDelegate(), 0, 0), list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx, ctx: ctx,
client: client, client: client,
mode: mode,
} }
m.list.Title = "GOSPT" m.list.Title = "GOSPT"
go m.Tick()
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("ctrl"+"r"), key.WithHelp("ctrl+r", "start radio")),
key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "select device")),
}
}
return m, nil return m, nil
} }
func DeviceView(ctx *gctx.Context, client *spotify.Client) ([]list.Item, error) {
items := []list.Item{}
devices, err := client.PlayerDevices(ctx)
if err != nil {
return nil, err
}
for _, device := range devices {
items = append(items, mainItem{
Name: device.Name,
Desc: fmt.Sprintf("%s - active: %t", device.ID, device.Active),
Device: device,
})
}
return items, nil
}
func HandleSetDevice(ctx *gctx.Context, client *spotify.Client, player spotify.PlayerDevice) {
fmt.Println("WHOA")
var err error
err = commands.SetDevice(ctx, client, player)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}

123
internal/tui/playlists.go Normal file
View File

@ -0,0 +1,123 @@
package tui
import (
"fmt"
"os"
"gospt/internal/commands"
"gospt/internal/gctx"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/zmb3/spotify/v2"
)
var playlistsDocStyle = lipgloss.NewStyle().Margin(1, 2)
type playlistItem struct {
Name string
Desc string
ID spotify.ID
spotify.SimplePlaylist
}
func (i playlistItem) Title() string { return i.Name }
func (i playlistItem) Description() string { return i.Desc }
func (i playlistItem) FilterValue() string { return i.Title() + i.Desc }
type playlistModel struct {
list list.Model
page int
ctx *gctx.Context
client *spotify.Client
}
func (m playlistModel) Init() tea.Cmd {
return nil
}
func (m playlistModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.list.Paginator.OnLastPage() {
// if the last request was not full
if len(m.list.Items())%50 == 0 {
playlists, err := commands.Playlists(m.ctx, m.client, (m.page + 1))
if err != nil {
return m, tea.Quit
}
m.page++
items := []list.Item{}
for _, playlist := range playlists.Playlists {
items = append(items, playlistItem{
Name: playlist.Name,
Desc: playlist.Description,
ID: playlist.ID,
SimplePlaylist: playlist,
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
}
}
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if msg.String() == "enter" {
playlist := m.list.SelectedItem().(playlistItem).SimplePlaylist
PlaylistTracks(m.ctx, m.client, playlist)
return m, tea.Quit
}
case tea.MouseMsg:
if msg.Type == 5 {
m.list.CursorUp()
}
if msg.Type == 6 {
m.list.CursorDown()
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m playlistModel) View() string {
return docStyle.Render(m.list.View())
}
func DisplayPlaylists(ctx *gctx.Context, client *spotify.Client) error {
items := []list.Item{}
playlists, err := commands.Playlists(ctx, client, 1)
if err != nil {
return err
}
for _, playlist := range playlists.Playlists {
items = append(items, playlistItem{
Name: playlist.Name,
Desc: playlist.Description,
ID: playlist.ID,
SimplePlaylist: playlist,
})
}
m := playlistModel{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx,
client: client,
}
m.list.Title = "Saved Tracks"
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
return nil
}

View File

@ -0,0 +1,197 @@
package tui
import (
"fmt"
"os"
"time"
"gospt/internal/commands"
"gospt/internal/gctx"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/zmb3/spotify/v2"
)
var list_updates chan *playlistTracksModel
type track struct {
Name string
Duration string
Artist spotify.SimpleArtist
ID spotify.ID
spotify.SavedTrack
}
func (i track) Title() string { return i.Name }
func (i track) Description() string {
return fmt.Sprint(i.Duration, " by ", i.Artist.Name)
}
func (i track) FilterValue() string { return i.Title() + i.Artist.Name }
type playlistTracksModel struct {
list list.Model
page int
ctx *gctx.Context
client *spotify.Client
playlist spotify.SimplePlaylist
}
func (m playlistTracksModel) Init() tea.Cmd {
list_updates = make(chan *playlistTracksModel)
return nil
}
func (m playlistTracksModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
select {
case msg := <-list_updates:
m.list.SetItems(msg.list.Items())
default:
}
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 {
// if last request was still full request more
if len(m.list.Items())%50 == 0 {
go m.LoadMoreItems()
}
}
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "backspace" || msg.String() == "q" || msg.String() == "esc" {
m, err := InitMain(m.ctx, m.client)
if err != nil {
fmt.Println("UH OH")
}
P = tea.NewProgram(m, tea.WithAltScreen())
if err := P.Start(); err != nil {
return m, tea.Quit
}
}
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if msg.String() == "ctrl+r" {
track := m.list.SelectedItem()
err := commands.RadioGivenSong(m.ctx, m.client, track.(item).ID, 0)
if err != nil {
return m, tea.Quit
}
}
if msg.String() == "enter" {
var err error
err = commands.PlaySongInPlaylist(m.ctx, m.client, &m.playlist.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
if err != nil {
m.ctx.Printf(err.Error())
}
}
case tea.MouseMsg:
if msg.Type == 5 {
m.list.CursorUp()
}
if msg.Type == 6 {
m.list.CursorDown()
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
m.View()
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m playlistTracksModel) View() string {
return docStyle.Render(m.list.View())
}
func (m *playlistTracksModel) LoadMoreItems() {
tracks, err := commands.PlaylistTracks(m.ctx, m.client, m.playlist.ID, (m.page + 1))
if err != nil {
return
}
m.page++
items := []list.Item{}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Track.Name,
Artist: track.Track.Artists[0],
Duration: track.Track.TimeDuration().Round(time.Second).String(),
ID: track.Track.ID,
})
}
for _, item := range items {
m.list.InsertItem(len(m.list.Items())+1, item)
}
list_updates <- m
}
func PlaylistTracks(ctx *gctx.Context, client *spotify.Client, playlist spotify.SimplePlaylist) error {
items := []list.Item{}
tracks, err := commands.PlaylistTracks(ctx, client, playlist.ID, 1)
if err != nil {
return err
}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Track.Name,
Artist: track.Track.Artists[0],
Duration: track.Track.TimeDuration().Round(time.Second).String(),
ID: track.Track.ID,
})
}
m := playlistTracksModel{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx,
client: client,
playlist: playlist,
}
m.list.Title = playlist.Name
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("ctrl", "r"), key.WithHelp("ctrl+r", "start radio")),
}
}
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
return nil
}
func InitPlaylists(ctx *gctx.Context, client *spotify.Client, playlist spotify.SimplePlaylist) (tea.Model, error) {
items := []list.Item{}
tracks, err := commands.PlaylistTracks(ctx, client, playlist.ID, 1)
if err != nil {
return nil, err
}
for _, track := range tracks.Tracks {
items = append(items, item{
Name: track.Track.Name,
Artist: track.Track.Artists[0],
Duration: track.Track.TimeDuration().Round(time.Second).String(),
ID: track.Track.ID,
})
}
m := playlistTracksModel{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx,
client: client,
playlist: playlist,
}
m.list.Title = playlist.Name
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("ctrl", "r"), key.WithHelp("ctrl+r", "start radio")),
}
}
m.View()
return m, err
}

View File

@ -11,7 +11,7 @@ import (
) )
// StartTea the entry point for the UI. Initializes the model. // StartTea the entry point for the UI. Initializes the model.
func StartTea(ctx *gctx.Context, client *spotify.Client, mode string) error { func StartTea(ctx *gctx.Context, client *spotify.Client) error {
if f, err := tea.LogToFile("debug.log", "help"); err != nil { if f, err := tea.LogToFile("debug.log", "help"); err != nil {
return err return err
} else { } else {
@ -22,7 +22,7 @@ func StartTea(ctx *gctx.Context, client *spotify.Client, mode string) error {
} }
}() }()
} }
m, err := InitMain(ctx, client, mode) m, err := InitMain(ctx, client)
if err != nil { if err != nil {
fmt.Println("UH OH") fmt.Println("UH OH")
} }