gospt/internal/tui/list.goasdg

198 lines
4.4 KiB
Plaintext
Raw Normal View History

2023-01-08 00:03:43 +00:00
package tui
import (
"fmt"
"os"
"time"
2023-01-08 05:22:54 +00:00
"gospt/internal/commands"
2023-01-08 05:26:06 +00:00
"gospt/internal/gctx"
2023-01-08 00:03:43 +00:00
2023-01-09 07:27:53 +00:00
"github.com/charmbracelet/bubbles/key"
2023-01-08 00:03:43 +00:00
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/zmb3/spotify/v2"
)
2023-01-11 04:46:15 +00:00
var (
track_updates chan *model
docStyle = lipgloss.NewStyle().Margin(1, 2)
)
2023-01-08 00:03:43 +00:00
type item struct {
2023-01-08 00:57:51 +00:00
Name string
Duration string
Artist spotify.SimpleArtist
ID spotify.ID
2023-01-08 00:03:43 +00:00
spotify.SavedTrack
}
func (i item) Title() string { return i.Name }
func (i item) Description() string {
2023-01-08 00:57:51 +00:00
return fmt.Sprint(i.Duration, " by ", i.Artist.Name)
2023-01-08 00:03:43 +00:00
}
2023-01-08 00:57:51 +00:00
func (i item) FilterValue() string { return i.Title() + i.Artist.Name }
2023-01-08 00:03:43 +00:00
type model struct {
list list.Model
page int
2023-01-08 05:26:06 +00:00
ctx *gctx.Context
2023-01-08 00:03:43 +00:00
client *spotify.Client
}
func (m model) Init() tea.Cmd {
2023-01-11 04:46:15 +00:00
track_updates = make(chan *model)
2023-01-08 00:03:43 +00:00
return nil
}
2023-01-11 04:46:15 +00:00
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
}
2023-01-08 00:03:43 +00:00
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2023-01-11 04:46:15 +00:00
select {
case msg := <-track_updates:
m.list.SetItems(msg.list.Items())
default:
}
if m.list.Paginator.Page == m.list.Paginator.TotalPages-2 {
2023-01-09 03:44:35 +00:00
// if last request was still full request more
if len(m.list.Items())%50 == 0 {
2023-01-11 04:46:15 +00:00
go m.LoadMoreItems()
2023-01-08 00:57:51 +00:00
}
}
2023-01-08 00:03:43 +00:00
switch msg := msg.(type) {
case tea.KeyMsg:
2023-01-09 04:42:02 +00:00
if msg.String() == "backspace" || msg.String() == "q" || msg.String() == "esc" {
2023-01-11 17:45:29 +00:00
m, err := InitMain(m.ctx, m.client, "main")
2023-01-09 18:54:21 +00:00
if err != nil {
fmt.Println("UH OH")
}
P = tea.NewProgram(m, tea.WithAltScreen())
if err := P.Start(); err != nil {
return m, tea.Quit
}
2023-01-09 04:42:02 +00:00
}
2023-01-08 00:03:43 +00:00
if msg.String() == "ctrl+c" {
2023-01-10 22:04:00 +00:00
return m, tea.Quit
2023-01-08 00:03:43 +00:00
}
2023-01-09 03:44:35 +00:00
if msg.String() == "ctrl+r" {
track := m.list.SelectedItem()
2023-01-09 05:08:51 +00:00
err := commands.RadioGivenSong(m.ctx, m.client, track.(item).ID, 0)
2023-01-09 03:44:35 +00:00
if err != nil {
return m, tea.Quit
}
}
2023-01-08 00:57:51 +00:00
if msg.String() == "enter" {
var err error
2023-01-10 02:03:31 +00:00
err = commands.PlayLikedSongs(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
2023-01-08 00:57:51 +00:00
if err != nil {
m.ctx.Printf(err.Error())
2023-01-08 00:03:43 +00:00
}
2023-01-09 03:44:35 +00:00
}
case tea.MouseMsg:
if msg.Type == 5 {
m.list.CursorUp()
}
if msg.Type == 6 {
m.list.CursorDown()
2023-01-08 00:03:43 +00:00
}
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())
}
2023-01-08 05:26:06 +00:00
func DisplayList(ctx *gctx.Context, client *spotify.Client) error {
2023-01-08 00:57:51 +00:00
items := []list.Item{}
tracks, err := commands.TrackList(ctx, client, 1)
2023-01-08 00:03:43 +00:00
if err != nil {
return err
}
for _, track := range tracks.Tracks {
items = append(items, item{
2023-01-08 00:57:51 +00:00
Name: track.Name,
Artist: track.Artists[0],
Duration: track.TimeDuration().Round(time.Second).String(),
ID: track.ID,
2023-01-08 00:03:43 +00:00
})
}
2023-01-08 00:57:51 +00:00
2023-01-09 03:44:35 +00:00
m := model{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
page: 1,
ctx: ctx,
client: client,
}
2023-01-09 07:27:53 +00:00
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("ctrl", "r"), key.WithHelp("ctrl+r", "start radio")),
}
}
2023-01-08 00:03:43 +00:00
m.list.Title = "Saved Tracks"
2023-01-09 03:44:35 +00:00
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
2023-01-08 00:03:43 +00:00
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
return nil
}
2023-01-09 18:54:21 +00:00
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
}