From 7b9fa3397a0158e403256f00fcebdf1b4e69dae5 Mon Sep 17 00:00:00 2001 From: FKD13 <44001949+FKD13@users.noreply.github.com> Date: Sat, 2 Mar 2024 03:14:49 +0100 Subject: [PATCH] working spotify scroller --- api/main.go | 2 ++ api/spotify.go | 17 +++++++++++++++++ screen/spotify.go | 12 ++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 api/spotify.go diff --git a/api/main.go b/api/main.go index b2ee3b5..6bed835 100644 --- a/api/main.go +++ b/api/main.go @@ -16,5 +16,7 @@ func Start(screenApp *screen.ScreenApp) { r.GET("/message", getMessage) r.POST("/message", postMessage) + r.POST("/spotify", spotifyHandlerWrapper(screenApp)) + r.Run() } diff --git a/api/spotify.go b/api/spotify.go new file mode 100644 index 0000000..89c0190 --- /dev/null +++ b/api/spotify.go @@ -0,0 +1,17 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "scc/screen" +) + +func spotifyHandlerWrapper(app *screen.ScreenApp) func(*gin.Context) { + return func(ctx *gin.Context) { + spotifyHandler(app, ctx) + } +} + +func spotifyHandler(app *screen.ScreenApp, ctx *gin.Context) { + b, _ := ctx.GetRawData() + app.Spotify.Update(string(b)) +} diff --git a/screen/spotify.go b/screen/spotify.go index 04a864e..e3ed8f2 100644 --- a/screen/spotify.go +++ b/screen/spotify.go @@ -4,6 +4,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "strings" + "sync" "time" ) @@ -11,6 +12,7 @@ type Spotify struct { screenApp *ScreenApp view *tview.TextView + mu sync.Mutex text string buffer string } @@ -41,22 +43,28 @@ func (spotify *Spotify) Run() { if w != 0 { + spotify.mu.Lock() + if len(spotify.buffer) != w { spotify.buffer = spotify.text + strings.Repeat(" ", w-len(spotify.text)) } spotify.buffer = spotify.buffer[1:] + string(spotify.buffer[0]) + spotify.mu.Unlock() + spotify.screenApp.app.QueueUpdateDraw(func() { spotify.view.SetText(spotify.buffer) }) - } - time.Sleep(50 * time.Millisecond) } } func (spotify *Spotify) Update(text string) { + spotify.mu.Lock() + defer spotify.mu.Unlock() + spotify.text = text + spotify.buffer = "" }