Skip to content

Commit

Permalink
Merge pull request #3 from ZeusWPI/screen
Browse files Browse the repository at this point in the history
Screen
  • Loading branch information
Topvennie authored Mar 2, 2024
2 parents 30b82db + 7c3fef3 commit 3eddef2
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 38 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.22.0
26 changes: 12 additions & 14 deletions api/endpoints.go → api/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"scc/screen"
"slices"

gin "github.com/gin-gonic/gin"
Expand All @@ -22,58 +23,55 @@ var blockedNames = []string{"Paul-Henri Spaak"}
var blockedIps = []string{}
var maxMessageLength = 200

func getMessage(c *gin.Context) {
func getMessage(app *screen.ScreenApp, c *gin.Context) {
c.JSON(200, gin.H{"messages": messages})
}

func postMessage(c *gin.Context) {
func postMessage(app *screen.ScreenApp, c *gin.Context) {
// Get structs
header := &header{}
message := &message{}

// Check Header
if err := c.ShouldBindHeader(header); err != nil {
fmt.Println("Here")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// Check Data
if err := c.ShouldBindJSON(message); err != nil {
fmt.Println("Other")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// Max message length
if len(message.Message) > maxMessageLength {
c.JSON(http.StatusBadRequest, gin.H{"error": "Message too long, maximum " + string(maxMessageLength)})
c.JSON(http.StatusBadRequest, gin.H{"error": "Message too long, maximum " + fmt.Sprint(maxMessageLength)})
return
}

// Check if sender is blocked
sender := ""
// Check if sender is blocked and construct message
var newMessage string
if header.Name != "" {
if slices.Contains(blockedNames, header.Name) {
c.JSON(http.StatusOK, gin.H{"message": "Message received"})
return
}
sender = header.Name
newMessage = fmt.Sprintf("[%s] %s", header.Name, message.Message)
} else if header.Ip != "" {
if slices.Contains(blockedIps, header.Ip) {
c.JSON(http.StatusOK, gin.H{"message": "Message received"})
return
}
sender = header.Ip

newMessage = fmt.Sprintf("<%s> %s", header.Ip, message.Message)
} else {
newMessage = message.Message
}

// Send message to tty

fmt.Println("Message received from", sender, ":", message.Message)

// Increment messages
messages++

app.Cammie.Update(newMessage)

c.JSON(http.StatusOK, gin.H{"message": "Message received"})
}
15 changes: 12 additions & 3 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@ package api

import (
"io"
"scc/screen"

"github.com/gin-gonic/gin"
)

func Start() {
func handlerWrapper(app *screen.ScreenApp, callback func(*screen.ScreenApp, *gin.Context)) func(*gin.Context) {
return func(ctx *gin.Context) {
callback(app, ctx)
}
}

func Start(screenApp *screen.ScreenApp) {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard

r := gin.Default()

r.GET("/message", getMessage)
r.POST("/message", postMessage)
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))
}
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
)

func main() {
go api.Start()
screenApp := screen.NewScreenApp()

screen.InitApp()
go api.Start(screenApp)

screen.Start(screenApp)
}
59 changes: 59 additions & 0 deletions screen/cammie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package screen

import (
"scc/utils"
"time"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

// Initial value, gets adjusted once it's known how much space is available
var maxMessages = 20

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

queue *utils.Queue[string]
text string
buffer string
}

func NewCammie(screenApp *ScreenApp) *Cammie {
cammie := Cammie{
screenApp: screenApp,
view: tview.NewTextView().SetWrap(true).SetWordWrap(true).SetText("pls"),

queue: utils.NewQueue[string](maxMessages),
}

cammie.view.SetTitle(" Cammie ")
cammie.view.SetBorder(true)
cammie.view.SetTextColor(tcell.ColorOrange)
cammie.view.SetBorderColor(tcell.ColorOrange)
cammie.view.SetTitleColor(tcell.ColorOrange)

return &cammie
}

func (cammie *Cammie) Run() {
time.Sleep(5 * time.Second)

_, _, _, h := cammie.view.GetInnerRect()
cammie.queue.SetMaxSize(h)
}

func (cammie *Cammie) Update(message string) {
cammie.queue.Enqueue(message)

cammie.screenApp.execute(func() {
cammie.screenApp.app.QueueUpdateDraw(func() {
cammie.view.Clear()

for _, message := range cammie.queue.Get() {
cammie.view.Write([]byte(message + "\n"))
}
})
})
}
23 changes: 23 additions & 0 deletions screen/graph1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package screen

import "github.com/rivo/tview"

type Graph1 struct {
ScreenApp *ScreenApp
view *tview.Box
}

func NewGraph1(screenApp *ScreenApp) *Graph1 {
graph1 := Graph1{
ScreenApp: screenApp,
view: tview.NewBox().SetBorder(true).SetTitle("Graph 1"),
}

return &graph1
}

func (graph1 *Graph1) Run() {
}

func (graph1 *Graph1) Update(text string) {
}
23 changes: 23 additions & 0 deletions screen/graph2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package screen

import "github.com/rivo/tview"

type Graph2 struct {
ScreenApp *ScreenApp
view *tview.Box
}

func NewGraph2(screenApp *ScreenApp) *Graph2 {
graph2 := Graph2{
ScreenApp: screenApp,
view: tview.NewBox().SetBorder(true).SetTitle("Graph 1"),
}

return &graph2
}

func (graph1 *Graph2) Run() {
}

func (graph1 *Graph2) Update(text string) {
}
61 changes: 61 additions & 0 deletions screen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package screen

import (
"sync"

"github.com/rivo/tview"
)

type ScreenApp struct {
mu sync.Mutex
app *tview.Application

Spotify *Spotify
Cammie *Cammie
Graph1 *Graph1
Graph2 *Graph2
}

type s_cammie struct {
cammie *tview.TextView
}

func (screenApp *ScreenApp) execute(f func()) {
screenApp.mu.Lock()
defer screenApp.mu.Unlock()
f()
}

func NewScreenApp() *ScreenApp {
screen := ScreenApp{
app: tview.NewApplication(),
}

screen.Spotify = NewSpotify(&screen)
screen.Cammie = NewCammie(&screen)
screen.Graph1 = NewGraph1(&screen)
screen.Graph2 = NewGraph2(&screen)

screen.app.SetRoot(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(screen.Spotify.view, 3, 2, false).
AddItem(tview.NewFlex().
AddItem(screen.Cammie.view, 0, 5, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(screen.Graph1.view, 0, 1, false).
AddItem(screen.Graph2.view, 0, 1, false), 0, 4, false), 0, 13, false), true).
EnableMouse(true)

return &screen
}

func Start(screen *ScreenApp) {

go screen.Spotify.Run()
go screen.Cammie.Run()
go screen.Graph1.Run()
go screen.Graph2.Run()

if err := screen.app.Run(); err != nil {
panic(err)
}
}
19 changes: 0 additions & 19 deletions screen/screen.go

This file was deleted.

Loading

0 comments on commit 3eddef2

Please sign in to comment.