tui rewrite
This commit is contained in:
parent
7313061532
commit
d92095d8db
@ -1,197 +0,0 @@
|
||||
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, "main")
|
||||
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
|
||||
}
|
@ -1,123 +0,0 @@
|
||||
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
|
||||
}
|
@ -1,207 +0,0 @@
|
||||
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 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 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, "main")
|
||||
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" {
|
||||
go HandleRadio(m.ctx, m.client, m.list.SelectedItem().(item).ID)
|
||||
}
|
||||
if msg.String() == "enter" {
|
||||
currentlyPlaying = "FUCK YOU"
|
||||
go HandlePlay(m.ctx, m.client, &m.playlist.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
|
||||
}
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user