Skip to content

Commit

Permalink
feat: cammie messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Mar 2, 2024
1 parent f25680c commit 48e0241
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 32 deletions.
41 changes: 22 additions & 19 deletions api/endpoints.go → api/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"fmt"
"net/http"
"scc/screen"
"slices"

gin "github.com/gin-gonic/gin"
)
Expand All @@ -21,11 +23,11 @@ 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{}
Expand All @@ -49,26 +51,27 @@ func postMessage(c *gin.Context) {
}

// Check if sender is blocked and construct message
// tmp commented out
// var newMessage string
// if header.Name != "" {
// if slices.Contains(blockedNames, header.Name) {
// c.JSON(http.StatusOK, gin.H{"message": "Message received"})
// return
// }
// 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
// }
// newMessage = fmt.Sprintf("<%s> %s", header.Ip, message.Message)
// } else {
// newMessage = message.Message
// }
var newMessage string
if header.Name != "" {
if slices.Contains(blockedNames, header.Name) {
c.JSON(http.StatusOK, gin.H{"message": "Message received"})
return
}
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
}
newMessage = fmt.Sprintf("<%s> %s", header.Ip, message.Message)
} else {
newMessage = message.Message
}

// Increment messages
messages++

app.Cammie.Update(newMessage)

c.JSON(http.StatusOK, gin.H{"message": "Message received"})
}
10 changes: 8 additions & 2 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"github.com/gin-gonic/gin"
)

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.Run()
}
46 changes: 38 additions & 8 deletions screen/cammie.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
package screen

import "github.com/rivo/tview"
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
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().SetDynamicColors(true).SetRegions(true).SetWordWrap(true),
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(tview.Styles.PrimaryTextColor)
cammie.view.SetBorderColor(tview.Styles.BorderColor)
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(text string) {
cammie.view.SetText(text)
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"))
}
})
})
}
6 changes: 3 additions & 3 deletions screen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func NewScreenApp() *ScreenApp {
screen.app.SetRoot(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(screen.Spotify.view, 3, 2, false).
AddItem(tview.NewFlex().
AddItem(tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true).SetBorder(true).SetTitle("Cammie"), 0, 5, false).
AddItem(screen.Cammie.view, 0, 5, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Graph 1"), 0, 1, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Graph 2"), 0, 1, false), 0, 4, false), 0, 13, false), true).
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
Expand Down
52 changes: 52 additions & 0 deletions utils/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package utils

type Queue[T any] struct {
maxSize int
Items []T
}

func NewQueue[T any](maxSize int) *Queue[T] {
return &Queue[T]{
maxSize: maxSize,
Items: make([]T, 0, maxSize),
}
}

func (q *Queue[T]) Enqueue(item T) {
if len(q.Items) >= q.maxSize {
q.Items = q.Items[1:]
}
q.Items = append(q.Items, item)
}

func (q *Queue[T]) Dequeue() (T, bool) {
if len(q.Items) == 0 {
var zero T
return zero, false
}

item := q.Items[0]
q.Items = q.Items[1:]
return item, true
}

func (q *Queue[T]) Peek() (T, bool) {
if len(q.Items) == 0 {
var zero T
return zero, false
}

return q.Items[0], true
}

func (q *Queue[T]) Get() []T {
return q.Items
}

func (q *Queue[T]) SetMaxSize(maxSize int) {
q.maxSize = maxSize

if len(q.Items) > maxSize {
q.Items = q.Items[:maxSize]
}
}

0 comments on commit 48e0241

Please sign in to comment.