This commit is contained in:
abs3nt 2023-01-11 18:45:22 -08:00
parent c3ebf8a792
commit 7066d6970a
2 changed files with 26 additions and 81 deletions

View File

@ -1,52 +0,0 @@
package tui
import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/zmb3/spotify"
)
var (
P *tea.Program
Client *spotify.Client
WindowSize tea.WindowSizeMsg
)
var DocStyle = lipgloss.NewStyle().Margin(0, 2)
var HelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
var ErrStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#bd534b")).Render
var AlertStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("62")).Render
type keymap struct {
Radio key.Binding
Enter key.Binding
Rename key.Binding
Delete key.Binding
Back key.Binding
Quit key.Binding
}
var Keymap = keymap{
Radio: key.NewBinding(
key.WithKeys("ctrl+r"),
key.WithHelp("ctrl+r", "start radio"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select"),
),
Back: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "back"),
),
Quit: key.NewBinding(
key.WithKeys("ctrl+c", "q"),
key.WithHelp("ctrl+c/q", "quit"),
),
}

View File

@ -15,11 +15,11 @@ import (
) )
var ( var (
P *tea.Program
DocStyle = lipgloss.NewStyle().Margin(0, 2)
currentlyPlaying string currentlyPlaying string
main_updates chan *mainModel main_updates chan *mainModel
page = 1 page = 1
docStyle = lipgloss.NewStyle().Margin(1, 2)
) )
type mainItem struct { type mainItem struct {
@ -29,8 +29,6 @@ type mainItem struct {
ID spotify.ID 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 }
@ -99,6 +97,24 @@ func HandleLibraryRadio(ctx *gctx.Context, client *spotify.Client) {
} }
} }
func HandlePlayLikedSong(ctx *gctx.Context, client *spotify.Client, position int) {
err := commands.PlayLikedSongs(ctx, client, position)
if err != nil {
fmt.Println(err.Error())
return
}
}
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())
return
}
}
func (m *mainModel) LoadMoreItems() { func (m *mainModel) LoadMoreItems() {
switch m.mode { switch m.mode {
case "main": case "main":
@ -166,14 +182,6 @@ func (m *mainModel) LoadMoreItems() {
} }
} }
func HandlePlayLikedSong(ctx *gctx.Context, client *spotify.Client, position int) {
err := commands.PlayLikedSongs(ctx, client, position)
if err != nil {
fmt.Println(err.Error())
return
}
}
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) m.list.NewStatusMessage(currentlyPlaying)
select { select {
@ -254,7 +262,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.list.NewStatusMessage("Playing " + currentlyPlaying) m.list.NewStatusMessage("Playing " + currentlyPlaying)
go HandlePlayLikedSong(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage)) go HandlePlayLikedSong(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage))
case "devices": case "devices":
go HandleSetDevice(m.ctx, m.client, m.list.SelectedItem().(mainItem).Device) go HandleSetDevice(m.ctx, m.client, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlayerDevice))
m.list.NewStatusMessage("Setting device to " + m.list.SelectedItem().FilterValue()) m.list.NewStatusMessage("Setting device to " + m.list.SelectedItem().FilterValue())
m.mode = "main" m.mode = "main"
m.list.NewStatusMessage("Setting view to main") m.list.NewStatusMessage("Setting view to main")
@ -296,7 +304,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.list.CursorDown() m.list.CursorDown()
} }
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize() h, v := DocStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v) m.list.SetSize(msg.Width-h, msg.Height-v)
} }
@ -306,7 +314,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m mainModel) View() string { func (m mainModel) View() string {
return docStyle.Render(m.list.View()) return DocStyle.Render(m.list.View())
} }
func DisplayMain(ctx *gctx.Context, client *spotify.Client) error { func DisplayMain(ctx *gctx.Context, client *spotify.Client) error {
@ -379,7 +387,6 @@ func SavedTracksView(ctx *gctx.Context, client *spotify.Client) ([]list.Item, er
Desc: track.Artists[0].Name + " - " + track.TimeDuration().Round(time.Second).String(), Desc: track.Artists[0].Name + " - " + track.TimeDuration().Round(time.Second).String(),
}) })
} }
return items, err return items, err
} }
@ -467,18 +474,8 @@ func DeviceView(ctx *gctx.Context, client *spotify.Client) ([]list.Item, error)
items = append(items, mainItem{ items = append(items, mainItem{
Name: device.Name, Name: device.Name,
Desc: fmt.Sprintf("%s - active: %t", device.ID, device.Active), Desc: fmt.Sprintf("%s - active: %t", device.ID, device.Active),
Device: device, SpotifyItem: device,
}) })
} }
return items, nil 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())
return
}
}