progress bar and now playing updates
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
abs3nt 2023-01-29 18:30:33 -08:00
parent 3180c156b2
commit 6e9d50d5ca
4 changed files with 37 additions and 31 deletions

3
go.mod
View File

@ -3,6 +3,7 @@ module gospt
go 1.19
require (
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/bubbles v0.14.0
github.com/charmbracelet/bubbletea v0.23.1
github.com/charmbracelet/lipgloss v0.6.0
@ -14,8 +15,8 @@ require (
)
require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect

1
go.sum
View File

@ -43,6 +43,7 @@ github.com/charmbracelet/bubbles v0.14.0/go.mod h1:bbeTiXwPww4M031aGi8UK2HT9RDWo
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/bubbletea v0.23.1 h1:CYdteX1wCiCzKNUlwm25ZHBIc1GXlYFyUIte8WPvhck=
github.com/charmbracelet/bubbletea v0.23.1/go.mod h1:JAfGK/3/pPKHTnAS8JIE2u9f61BjWTQY57RbT25aMXU=
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=

View File

@ -9,6 +9,7 @@ import (
"github.com/atotto/clipboard"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
@ -77,6 +78,8 @@ type mainModel struct {
artist spotify.SimpleArtist
album spotify.SimpleAlbum
searchResults *SearchResults
progress progress.Model
playing *spotify.CurrentlyPlaying
search string
}
@ -413,26 +416,15 @@ func (m *mainModel) SelectItem() error {
func (m mainModel) Init() tea.Cmd {
main_updates = make(chan *mainModel)
return nil
return Tick()
}
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)
if playing != nil && playing.Playing && playing.Item != nil {
currentlyPlaying = "Now playing " + playing.Item.Name + " by " + playing.Item.Artists[0].Name
}
case <-quit:
ticker.Stop()
return
}
}
}()
type tickMsg time.Time
func Tick() tea.Cmd {
return tea.Tick(time.Second*1, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
func (m mainModel) View() string {
@ -485,6 +477,22 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
// Handle user input
switch msg := msg.(type) {
case tickMsg:
playing, err := m.client.PlayerCurrentlyPlaying(m.ctx)
if err != nil {
return nil, nil
}
if playing != nil && playing.Playing && playing.Item != nil {
cmd := m.progress.SetPercent(float64(playing.Progress) / float64(playing.Item.Duration))
m.playing = playing
return m, tea.Batch(Tick(), cmd)
}
return m, Tick()
case progress.FrameMsg:
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(progress.Model)
currentlyPlaying = fmt.Sprintf("Now playing %s by %s - %s %s/%s ", m.playing.Item.Name, m.playing.Item.Artists[0].Name, m.progress.View(), (time.Duration(m.playing.Progress) * time.Millisecond).Round(time.Second), (time.Duration(m.playing.Item.Duration) * time.Millisecond).Round(time.Second))
return m, cmd
case tea.KeyMsg:
// quit
if msg.String() == "ctrl+c" {
@ -576,15 +584,9 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model, error) {
prog := progress.New(progress.WithColorProfile(2), progress.WithoutPercentage())
var err error
lipgloss.SetColorProfile(2)
playing, err := client.PlayerCurrentlyPlaying(ctx)
if err != nil {
return nil, err
}
if playing != nil && playing.Playing && playing.Item != nil {
currentlyPlaying = "Now playing " + playing.Item.Name + " by " + playing.Item.Artists[0].Name
}
items := []list.Item{}
switch mode {
case Main:
@ -608,9 +610,10 @@ func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model,
ctx: ctx,
client: client,
mode: mode,
progress: prog,
}
m.list.Title = "GOSPT"
go m.Tick()
Tick()
m.list.DisableQuitKeybindings()
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{

View File

@ -99,6 +99,7 @@ func SearchView(ctx *gctx.Context, client *spotify.Client, search string) ([]lis
Desc: "Search results",
SpotifyItem: result.Artists,
})
items = append(items, mainItem{
Name: "Playlists",
Desc: "Search results",