From f5f176b8013be24ed36ce39c73ff03b6f2f979d2 Mon Sep 17 00:00:00 2001 From: jjohnstondev Date: Sun, 8 Jan 2023 20:42:02 -0800 Subject: [PATCH] mainpage --- internal/api/api.go | 7 +- internal/tui/list.go | 4 + internal/tui/main.go | 130 +++++++++++++++++++++++++++++++++ internal/tui/playlisttracks.go | 5 +- 4 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 internal/tui/main.go diff --git a/internal/api/api.go b/internal/api/api.go index 27556ce..e654025 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -12,12 +12,7 @@ import ( func Run(ctx *gctx.Context, client *spotify.Client, args []string) error { if len(args) == 0 { - user, err := client.CurrentUser(ctx) - if err != nil { - return fmt.Errorf("Failed to get current user") - } - fmt.Println("The following commands are currently supported:\nplay pause next shuffle\nhave fun", user.DisplayName) - return nil + return tui.DisplayMain(ctx, client) } switch args[0] { case "play": diff --git a/internal/tui/list.go b/internal/tui/list.go index a9ff493..be266c1 100644 --- a/internal/tui/list.go +++ b/internal/tui/list.go @@ -66,6 +66,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } switch msg := msg.(type) { case tea.KeyMsg: + if msg.String() == "backspace" || msg.String() == "q" || msg.String() == "esc" { + DisplayMain(m.ctx, m.client) + return m, tea.Quit + } if msg.String() == "ctrl+c" { return m, tea.Quit } diff --git a/internal/tui/main.go b/internal/tui/main.go new file mode 100644 index 0000000..ed3c04c --- /dev/null +++ b/internal/tui/main.go @@ -0,0 +1,130 @@ +package tui + +import ( + "fmt" + "os" + + "gospt/internal/commands" + "gospt/internal/gctx" + + "github.com/charmbracelet/bubbles/list" + tea "github.com/charmbracelet/bubbletea" + "github.com/zmb3/spotify/v2" +) + +type mainItem struct { + Name string + 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 { + list list.Model + page int + ctx *gctx.Context + client *spotify.Client +} + +func (m mainModel) Init() tea.Cmd { + return nil +} + +func (m mainModel) 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, mainItem{ + Name: playlist.Name, + Desc: playlist.Description, + SpotifyItem: 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" { + switch m.list.SelectedItem().(mainItem).SpotifyItem.(type) { + case spotify.SimplePlaylist: + playlist := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.SimplePlaylist) + PlaylistTracks(m.ctx, m.client, playlist) + return m, tea.Quit + case *spotify.SavedTrackPage: + DisplayList(m.ctx, m.client) + 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 mainModel) View() string { + return docStyle.Render(m.list.View()) +} + +func DisplayMain(ctx *gctx.Context, client *spotify.Client) error { + items := []list.Item{} + saved_items, err := commands.TrackList(ctx, client, 1) + items = append(items, mainItem{ + Name: "Saved Tracks", + Desc: fmt.Sprintf("%d saved songs", saved_items.Total), + SpotifyItem: saved_items, + }) + playlists, err := commands.Playlists(ctx, client, 1) + if err != nil { + return err + } + for _, playlist := range playlists.Playlists { + items = append(items, mainItem{ + Name: playlist.Name, + Desc: playlist.Description, + SpotifyItem: playlist, + }) + } + m := mainModel{ + 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 +} diff --git a/internal/tui/playlisttracks.go b/internal/tui/playlisttracks.go index 01af03f..c2b5d72 100644 --- a/internal/tui/playlisttracks.go +++ b/internal/tui/playlisttracks.go @@ -65,8 +65,9 @@ func (m playlistTracksModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } switch msg := msg.(type) { case tea.KeyMsg: - if msg.String() == "backspace" || msg.String() == "q" { - DisplayPlaylists(m.ctx, m.client) + if msg.String() == "backspace" || msg.String() == "q" || msg.String() == "esc" { + DisplayMain(m.ctx, m.client) + return m, tea.Quit } if msg.String() == "ctrl+c" { return m, tea.Quit