progress bar and now playing updates
This commit is contained in:
parent
3180c156b2
commit
6e9d50d5ca
3
go.mod
3
go.mod
@ -3,6 +3,7 @@ module gospt
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/atotto/clipboard v0.1.4
|
||||||
github.com/charmbracelet/bubbles v0.14.0
|
github.com/charmbracelet/bubbles v0.14.0
|
||||||
github.com/charmbracelet/bubbletea v0.23.1
|
github.com/charmbracelet/bubbletea v0.23.1
|
||||||
github.com/charmbracelet/lipgloss v0.6.0
|
github.com/charmbracelet/lipgloss v0.6.0
|
||||||
@ -14,8 +15,8 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/atotto/clipboard v0.1.4 // indirect
|
|
||||||
github.com/aymanbagabas/go-osc52 v1.0.3 // 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/containerd/console v1.0.3 // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
1
go.sum
1
go.sum
@ -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.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
|
||||||
github.com/charmbracelet/bubbletea v0.23.1 h1:CYdteX1wCiCzKNUlwm25ZHBIc1GXlYFyUIte8WPvhck=
|
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/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/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.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
|
||||||
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
|
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/atotto/clipboard"
|
"github.com/atotto/clipboard"
|
||||||
"github.com/charmbracelet/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
"github.com/charmbracelet/bubbles/list"
|
"github.com/charmbracelet/bubbles/list"
|
||||||
|
"github.com/charmbracelet/bubbles/progress"
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
@ -77,6 +78,8 @@ type mainModel struct {
|
|||||||
artist spotify.SimpleArtist
|
artist spotify.SimpleArtist
|
||||||
album spotify.SimpleAlbum
|
album spotify.SimpleAlbum
|
||||||
searchResults *SearchResults
|
searchResults *SearchResults
|
||||||
|
progress progress.Model
|
||||||
|
playing *spotify.CurrentlyPlaying
|
||||||
search string
|
search string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,26 +416,15 @@ func (m *mainModel) SelectItem() error {
|
|||||||
|
|
||||||
func (m mainModel) Init() tea.Cmd {
|
func (m mainModel) Init() tea.Cmd {
|
||||||
main_updates = make(chan *mainModel)
|
main_updates = make(chan *mainModel)
|
||||||
return nil
|
return Tick()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mainModel) Tick() {
|
type tickMsg time.Time
|
||||||
ticker := time.NewTicker(5 * time.Second)
|
|
||||||
quit := make(chan struct{})
|
func Tick() tea.Cmd {
|
||||||
go func() {
|
return tea.Tick(time.Second*1, func(t time.Time) tea.Msg {
|
||||||
for {
|
return tickMsg(t)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m mainModel) View() string {
|
func (m mainModel) View() string {
|
||||||
@ -485,6 +477,22 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
// Handle user input
|
// Handle user input
|
||||||
switch msg := msg.(type) {
|
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:
|
case tea.KeyMsg:
|
||||||
// quit
|
// quit
|
||||||
if msg.String() == "ctrl+c" {
|
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) {
|
func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model, error) {
|
||||||
|
prog := progress.New(progress.WithColorProfile(2), progress.WithoutPercentage())
|
||||||
var err error
|
var err error
|
||||||
lipgloss.SetColorProfile(2)
|
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{}
|
items := []list.Item{}
|
||||||
switch mode {
|
switch mode {
|
||||||
case Main:
|
case Main:
|
||||||
@ -604,13 +606,14 @@ func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
m := mainModel{
|
m := mainModel{
|
||||||
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
|
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
client: client,
|
client: client,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
|
progress: prog,
|
||||||
}
|
}
|
||||||
m.list.Title = "GOSPT"
|
m.list.Title = "GOSPT"
|
||||||
go m.Tick()
|
Tick()
|
||||||
m.list.DisableQuitKeybindings()
|
m.list.DisableQuitKeybindings()
|
||||||
m.list.AdditionalShortHelpKeys = func() []key.Binding {
|
m.list.AdditionalShortHelpKeys = func() []key.Binding {
|
||||||
return []key.Binding{
|
return []key.Binding{
|
||||||
|
@ -99,6 +99,7 @@ func SearchView(ctx *gctx.Context, client *spotify.Client, search string) ([]lis
|
|||||||
Desc: "Search results",
|
Desc: "Search results",
|
||||||
SpotifyItem: result.Artists,
|
SpotifyItem: result.Artists,
|
||||||
})
|
})
|
||||||
|
|
||||||
items = append(items, mainItem{
|
items = append(items, mainItem{
|
||||||
Name: "Playlists",
|
Name: "Playlists",
|
||||||
Desc: "Search results",
|
Desc: "Search results",
|
||||||
|
Loading…
Reference in New Issue
Block a user