Skip to content

Commit

Permalink
Merge branch 'screen' of github.com:ZeusWPI/SCC into screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Mar 2, 2024
2 parents 48e0241 + 0b2b809 commit 7c3fef3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ func Start(screenApp *screen.ScreenApp) {
r.GET("/message", handlerWrapper(screenApp, getMessage))
r.POST("/message", handlerWrapper(screenApp, postMessage))

r.POST("/spotify", spotifyHandlerWrapper(screenApp))

r.Run()
}
17 changes: 17 additions & 0 deletions api/spotify.go
Original file line number Diff line number Diff line change
@@ -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))
}
16 changes: 14 additions & 2 deletions screen/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"strings"
"sync"
"time"
)

type Spotify struct {
screenApp *ScreenApp
view *tview.TextView

mu sync.Mutex
text string
buffer string
}
Expand Down Expand Up @@ -41,22 +43,32 @@ func (spotify *Spotify) Run() {

if w != 0 {

spotify.mu.Lock()

if len(spotify.buffer) != w {
if len(spotify.text) > w {
spotify.text = spotify.text[0 : w-4]
spotify.text += "..."
}
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 = ""
}

0 comments on commit 7c3fef3

Please sign in to comment.