From 4681baa9a55078fe0979c99486bea28a72edbe18 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Sat, 2 Mar 2024 02:02:38 +0100 Subject: [PATCH 1/9] refactor: forgot what --- api/endpoints.go | 22 ++++++++++----------- api/main.go | 8 +++++++- main.go | 6 ++++-- screen/cammie.go | 5 +++++ screen/main.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ screen/message.go | 29 +++++++++++++++++++++++++++ screen/screen.go | 19 ------------------ 7 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 screen/cammie.go create mode 100644 screen/main.go create mode 100644 screen/message.go delete mode 100644 screen/screen.go diff --git a/api/endpoints.go b/api/endpoints.go index 86efc71..9c5a9b3 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -33,47 +33,45 @@ func postMessage(c *gin.Context) { // 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++ c.JSON(http.StatusOK, gin.H{"message": "Message received"}) + + // Add message to cammie chat + safeMessageQueue.AddMessage(newMessage) } diff --git a/api/main.go b/api/main.go index b895bc1..858a79a 100644 --- a/api/main.go +++ b/api/main.go @@ -3,10 +3,16 @@ package api import ( "io" + "scc/screen" + "github.com/gin-gonic/gin" ) -func Start() { +var safeMessageQueue *screen.SafeMessageQueue + +func Start(queue *screen.SafeMessageQueue) { + safeMessageQueue = queue + gin.SetMode(gin.ReleaseMode) gin.DefaultWriter = io.Discard diff --git a/main.go b/main.go index 07f29b2..e0f705e 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,9 @@ import ( ) func main() { - go api.Start() + cammieQueue := screen.NewSafeMessageQueue() - screen.InitApp() + go api.Start(cammieQueue) + + screen.Start(cammieQueue) } diff --git a/screen/cammie.go b/screen/cammie.go new file mode 100644 index 0000000..d87ac28 --- /dev/null +++ b/screen/cammie.go @@ -0,0 +1,5 @@ +package screen + +func updateCammie() { + +} \ No newline at end of file diff --git a/screen/main.go b/screen/main.go new file mode 100644 index 0000000..c0213eb --- /dev/null +++ b/screen/main.go @@ -0,0 +1,50 @@ +package screen + +import ( + "sync" + + "github.com/rivo/tview" +) + +type screenApp struct { + mu sync.Mutex + app *tview.Application + spotify *tview.Box + cammie *s_cammie + graph1 *tview.Box + graph2 *tview.Box +} + +type s_cammie struct { + cammie *tview.TextView + queue *SafeMessageQueue +} + +var screen screenApp + +func Start(queue *SafeMessageQueue) { + screen = screenApp{ + app: tview.NewApplication(), + spotify: tview.NewBox().SetBorder(true).SetTitle("Spotify"), + cammie: &s_cammie{ + cammie: tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true), + queue: queue, + }, + graph1: tview.NewBox().SetBorder(true).SetTitle("Graph 1"), + graph2: tview.NewBox().SetBorder(true).SetTitle("Graph 2"), + } + + flex := tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(screen.spotify, 0, 2, false). + AddItem(tview.NewFlex(). + AddItem(screen.cammie.cammie.SetBorder(true).SetTitle("Cammie"), 0, 5, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(screen.graph1, 0, 1, false). + AddItem(screen.graph2, 0, 1, false), 0, 4, false), 0, 13, false) + + go updateCammie() + + if err := screen.app.SetRoot(flex, true).EnableMouse(true).Run(); err != nil { + panic(err) + } +} diff --git a/screen/message.go b/screen/message.go new file mode 100644 index 0000000..39a855f --- /dev/null +++ b/screen/message.go @@ -0,0 +1,29 @@ +package screen + +import ( + "sync" +) + +var maxMessages = 20 + +type SafeMessageQueue struct { + Mu sync.Mutex + Messages []string +} + +func NewSafeMessageQueue() *SafeMessageQueue { + return &SafeMessageQueue{ + Messages: make([]string, 0, maxMessages), + } +} + +func (smq *SafeMessageQueue) AddMessage(message string) { + smq.Mu.Lock() + defer smq.Mu.Unlock() + + if len(smq.Messages) == maxMessages { + smq.Messages = smq.Messages[1:] + } + + smq.Messages = append(smq.Messages, message) +} diff --git a/screen/screen.go b/screen/screen.go deleted file mode 100644 index 3d0a88f..0000000 --- a/screen/screen.go +++ /dev/null @@ -1,19 +0,0 @@ -package screen - -import "github.com/rivo/tview" - -func InitApp() { - app := tview.NewApplication() - - flex := tview.NewFlex().SetDirection(tview.FlexRow). - AddItem(tview.NewBox().SetBorder(true).SetTitle("Spotify"), 0, 2, false). - AddItem(tview.NewFlex(). - AddItem(tview.NewBox().SetBorder(true).SetTitle("Cammie"), 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) - - if err := app.SetRoot(flex, true).EnableMouse(true).Run(); err != nil { - panic(err) - } -} \ No newline at end of file From e9e4ae6cc1a632e5a6aa1f5323ee38bb3e38ff82 Mon Sep 17 00:00:00 2001 From: FKD13 <44001949+FKD13@users.noreply.github.com> Date: Sat, 2 Mar 2024 02:28:07 +0100 Subject: [PATCH 2/9] refactor --- .tool-versions | 1 + api/main.go | 2 +- main.go | 6 ++++-- screen/main.go | 54 +++++++++++++++++++++++++++-------------------- screen/spotify.go | 43 +++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 .tool-versions create mode 100644 screen/spotify.go diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..f526442 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +golang 1.22.0 diff --git a/api/main.go b/api/main.go index 858a79a..deceb5e 100644 --- a/api/main.go +++ b/api/main.go @@ -10,7 +10,7 @@ import ( var safeMessageQueue *screen.SafeMessageQueue -func Start(queue *screen.SafeMessageQueue) { +func Start(screenApp *screen.ScreenApp, queue *screen.SafeMessageQueue) { safeMessageQueue = queue gin.SetMode(gin.ReleaseMode) diff --git a/main.go b/main.go index e0f705e..44e46c6 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,9 @@ import ( func main() { cammieQueue := screen.NewSafeMessageQueue() - go api.Start(cammieQueue) + screenApp := screen.NewScreenApp() - screen.Start(cammieQueue) + go api.Start(screenApp, cammieQueue) + + screen.Start(screenApp, cammieQueue) } diff --git a/screen/main.go b/screen/main.go index c0213eb..51d48d3 100644 --- a/screen/main.go +++ b/screen/main.go @@ -6,13 +6,15 @@ import ( "github.com/rivo/tview" ) -type screenApp struct { - mu sync.Mutex - app *tview.Application - spotify *tview.Box - cammie *s_cammie - graph1 *tview.Box - graph2 *tview.Box +type ScreenApp struct { + mu sync.Mutex + app *tview.Application + + Spotify *Spotify + // spotify *tview.TextView + // cammie *s_cammie + // graph1 *tview.Box + // graph2 *tview.Box } type s_cammie struct { @@ -20,31 +22,37 @@ type s_cammie struct { queue *SafeMessageQueue } -var screen screenApp - -func Start(queue *SafeMessageQueue) { - screen = screenApp{ - app: tview.NewApplication(), - spotify: tview.NewBox().SetBorder(true).SetTitle("Spotify"), - cammie: &s_cammie{ - cammie: tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true), - queue: queue, - }, - graph1: tview.NewBox().SetBorder(true).SetTitle("Graph 1"), - graph2: tview.NewBox().SetBorder(true).SetTitle("Graph 2"), +func NewScreenApp() *ScreenApp { + screen := ScreenApp{ + app: tview.NewApplication(), + // cammie: &s_cammie{ + // cammie: tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true), + // queue: queue, + // }, + // graph1: tview.NewBox().SetBorder(true).SetTitle("Graph 1"), + // graph2: tview.NewBox().SetBorder(true).SetTitle("Graph 2"), } - flex := tview.NewFlex().SetDirection(tview.FlexRow). - AddItem(screen.spotify, 0, 2, false). + screen.Spotify = NewSpotify(&screen) + + screen.app.SetRoot(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(screen.Spotify.view, 3, 2, false). AddItem(tview.NewFlex(). AddItem(screen.cammie.cammie.SetBorder(true).SetTitle("Cammie"), 0, 5, false). AddItem(tview.NewFlex().SetDirection(tview.FlexRow). AddItem(screen.graph1, 0, 1, false). - AddItem(screen.graph2, 0, 1, false), 0, 4, false), 0, 13, false) + AddItem(screen.graph2, 0, 1, false), 0, 4, false), 0, 13, false), true). + EnableMouse(true) + + return &screen +} + +func Start(screen *ScreenApp, queue *SafeMessageQueue) { + go screen.Spotify.Run() go updateCammie() - if err := screen.app.SetRoot(flex, true).EnableMouse(true).Run(); err != nil { + if err := screen.app.Run(); err != nil { panic(err) } } diff --git a/screen/spotify.go b/screen/spotify.go new file mode 100644 index 0000000..364fb24 --- /dev/null +++ b/screen/spotify.go @@ -0,0 +1,43 @@ +package screen + +import ( + "fmt" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" + "time" +) + +type Spotify struct { + screenApp *ScreenApp + app *tview.Application + view *tview.TextView +} + +func NewSpotify(screenApp *ScreenApp) *Spotify { + spotify := Spotify{ + screenApp: screenApp, + view: tview.NewTextView(), + } + + spotify.view.SetTitle(" Spotify ") + spotify.view.SetBorder(true) + spotify.view.SetTextColor(tcell.ColorLimeGreen) + spotify.view.SetBorderColor(tcell.ColorLimeGreen) + + return &spotify +} + +func (spotify *Spotify) Run() { + i := 0 + for { + time.Sleep(1 * time.Second) + spotify.app.QueueUpdateDraw(func() { + spotify.view.SetText(fmt.Sprintf("%d", i)) + }) + i++ + } +} + +func (spotify *Spotify) Update(text string) { + +} From 80f5e65766f7099710084c94f6679a393cbee624 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Sat, 2 Mar 2024 02:34:43 +0100 Subject: [PATCH 3/9] refactor: cammie --- api/endpoints.go | 3 --- api/main.go | 6 +----- main.go | 6 ++---- screen/cammie.go | 30 +++++++++++++++++++++++++++--- screen/main.go | 9 +++++---- screen/message.go | 29 ----------------------------- 6 files changed, 35 insertions(+), 48 deletions(-) delete mode 100644 screen/message.go diff --git a/api/endpoints.go b/api/endpoints.go index 9c5a9b3..026566d 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -71,7 +71,4 @@ func postMessage(c *gin.Context) { messages++ c.JSON(http.StatusOK, gin.H{"message": "Message received"}) - - // Add message to cammie chat - safeMessageQueue.AddMessage(newMessage) } diff --git a/api/main.go b/api/main.go index deceb5e..71437d9 100644 --- a/api/main.go +++ b/api/main.go @@ -8,11 +8,7 @@ import ( "github.com/gin-gonic/gin" ) -var safeMessageQueue *screen.SafeMessageQueue - -func Start(screenApp *screen.ScreenApp, queue *screen.SafeMessageQueue) { - safeMessageQueue = queue - +func Start(screenApp *screen.ScreenApp) { gin.SetMode(gin.ReleaseMode) gin.DefaultWriter = io.Discard diff --git a/main.go b/main.go index 44e46c6..6649361 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,9 @@ import ( ) func main() { - cammieQueue := screen.NewSafeMessageQueue() - screenApp := screen.NewScreenApp() - go api.Start(screenApp, cammieQueue) + go api.Start(screenApp) - screen.Start(screenApp, cammieQueue) + screen.Start(screenApp) } diff --git a/screen/cammie.go b/screen/cammie.go index d87ac28..4d04668 100644 --- a/screen/cammie.go +++ b/screen/cammie.go @@ -1,5 +1,29 @@ package screen -func updateCammie() { - -} \ No newline at end of file +import "github.com/rivo/tview" + +type Cammie struct { + ScreenApp *ScreenApp + view *tview.TextView +} + +func NewCammie(screenApp *ScreenApp) *Cammie { + cammie := Cammie{ + ScreenApp: screenApp, + view: tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true), + } + + cammie.view.SetTitle(" Cammie ") + cammie.view.SetBorder(true) + cammie.view.SetTextColor(tview.Styles.PrimaryTextColor) + cammie.view.SetBorderColor(tview.Styles.BorderColor) + + return &cammie +} + +func (cammie *Cammie) Run() { +} + +func (cammie *Cammie) Update(text string) { + cammie.view.SetText(text) +} diff --git a/screen/main.go b/screen/main.go index 51d48d3..b7bae55 100644 --- a/screen/main.go +++ b/screen/main.go @@ -11,6 +11,7 @@ type ScreenApp struct { app *tview.Application Spotify *Spotify + Cammie *Cammie // spotify *tview.TextView // cammie *s_cammie // graph1 *tview.Box @@ -19,7 +20,6 @@ type ScreenApp struct { type s_cammie struct { cammie *tview.TextView - queue *SafeMessageQueue } func NewScreenApp() *ScreenApp { @@ -34,11 +34,12 @@ func NewScreenApp() *ScreenApp { } screen.Spotify = NewSpotify(&screen) + screen.Cammie = NewCammie(&screen) screen.app.SetRoot(tview.NewFlex().SetDirection(tview.FlexRow). AddItem(screen.Spotify.view, 3, 2, false). AddItem(tview.NewFlex(). - AddItem(screen.cammie.cammie.SetBorder(true).SetTitle("Cammie"), 0, 5, false). + AddItem(screen.Cammie.view, 0, 5, false). AddItem(tview.NewFlex().SetDirection(tview.FlexRow). AddItem(screen.graph1, 0, 1, false). AddItem(screen.graph2, 0, 1, false), 0, 4, false), 0, 13, false), true). @@ -47,10 +48,10 @@ func NewScreenApp() *ScreenApp { return &screen } -func Start(screen *ScreenApp, queue *SafeMessageQueue) { +func Start(screen *ScreenApp) { go screen.Spotify.Run() - go updateCammie() + go screen.Cammie.Run() if err := screen.app.Run(); err != nil { panic(err) diff --git a/screen/message.go b/screen/message.go deleted file mode 100644 index 39a855f..0000000 --- a/screen/message.go +++ /dev/null @@ -1,29 +0,0 @@ -package screen - -import ( - "sync" -) - -var maxMessages = 20 - -type SafeMessageQueue struct { - Mu sync.Mutex - Messages []string -} - -func NewSafeMessageQueue() *SafeMessageQueue { - return &SafeMessageQueue{ - Messages: make([]string, 0, maxMessages), - } -} - -func (smq *SafeMessageQueue) AddMessage(message string) { - smq.Mu.Lock() - defer smq.Mu.Unlock() - - if len(smq.Messages) == maxMessages { - smq.Messages = smq.Messages[1:] - } - - smq.Messages = append(smq.Messages, message) -} From 6d0083c2c21b76f4603dced9a6ecfc3609028cac Mon Sep 17 00:00:00 2001 From: Topvennie Date: Sat, 2 Mar 2024 02:42:40 +0100 Subject: [PATCH 4/9] refactor: graphs --- api/endpoints.go | 34 +++++++++++++++++----------------- api/main.go | 1 - screen/graph1.go | 23 +++++++++++++++++++++++ screen/graph2.go | 23 +++++++++++++++++++++++ screen/main.go | 20 ++++++++------------ 5 files changed, 71 insertions(+), 30 deletions(-) create mode 100644 screen/graph1.go create mode 100644 screen/graph2.go diff --git a/api/endpoints.go b/api/endpoints.go index 026566d..a7ab9c9 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -3,7 +3,6 @@ package api import ( "fmt" "net/http" - "slices" gin "github.com/gin-gonic/gin" ) @@ -50,22 +49,23 @@ func postMessage(c *gin.Context) { } // 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 - } - 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 - } + // 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 + // } // Increment messages messages++ diff --git a/api/main.go b/api/main.go index 71437d9..b2ee3b5 100644 --- a/api/main.go +++ b/api/main.go @@ -2,7 +2,6 @@ package api import ( "io" - "scc/screen" "github.com/gin-gonic/gin" diff --git a/screen/graph1.go b/screen/graph1.go new file mode 100644 index 0000000..c6fb98c --- /dev/null +++ b/screen/graph1.go @@ -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) { +} diff --git a/screen/graph2.go b/screen/graph2.go new file mode 100644 index 0000000..5471e03 --- /dev/null +++ b/screen/graph2.go @@ -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) { +} diff --git a/screen/main.go b/screen/main.go index b7bae55..a69a678 100644 --- a/screen/main.go +++ b/screen/main.go @@ -12,10 +12,8 @@ type ScreenApp struct { Spotify *Spotify Cammie *Cammie - // spotify *tview.TextView - // cammie *s_cammie - // graph1 *tview.Box - // graph2 *tview.Box + Graph1 *Graph1 + Graph2 *Graph2 } type s_cammie struct { @@ -25,24 +23,20 @@ type s_cammie struct { func NewScreenApp() *ScreenApp { screen := ScreenApp{ app: tview.NewApplication(), - // cammie: &s_cammie{ - // cammie: tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true), - // queue: queue, - // }, - // graph1: tview.NewBox().SetBorder(true).SetTitle("Graph 1"), - // graph2: tview.NewBox().SetBorder(true).SetTitle("Graph 2"), } 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, 0, 1, false). - AddItem(screen.graph2, 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 @@ -52,6 +46,8 @@ 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) From fb89b75c23609149e1eeb114c0028f5ca7abef56 Mon Sep 17 00:00:00 2001 From: FKD13 <44001949+FKD13@users.noreply.github.com> Date: Sat, 2 Mar 2024 02:55:01 +0100 Subject: [PATCH 5/9] basic scrolling text --- screen/main.go | 6 +++--- screen/spotify.go | 35 +++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/screen/main.go b/screen/main.go index a69a678..32a8575 100644 --- a/screen/main.go +++ b/screen/main.go @@ -33,10 +33,10 @@ func NewScreenApp() *ScreenApp { 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.NewTextView().SetDynamicColors(true).SetRegions(true).SetWordWrap(true).SetBorder(true).SetTitle("Cammie"), 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). + 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). EnableMouse(true) return &screen diff --git a/screen/spotify.go b/screen/spotify.go index 364fb24..04a864e 100644 --- a/screen/spotify.go +++ b/screen/spotify.go @@ -1,40 +1,59 @@ package screen import ( - "fmt" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" + "strings" "time" ) type Spotify struct { screenApp *ScreenApp - app *tview.Application view *tview.TextView + + text string + buffer string } func NewSpotify(screenApp *ScreenApp) *Spotify { spotify := Spotify{ screenApp: screenApp, view: tview.NewTextView(), + + text: "VERY COOL SONG - Le Artist", + buffer: "", } spotify.view.SetTitle(" Spotify ") spotify.view.SetBorder(true) spotify.view.SetTextColor(tcell.ColorLimeGreen) spotify.view.SetBorderColor(tcell.ColorLimeGreen) + spotify.view.SetTitleColor(tcell.ColorLimeGreen) return &spotify } func (spotify *Spotify) Run() { - i := 0 + time.Sleep(1 * time.Second) + for { - time.Sleep(1 * time.Second) - spotify.app.QueueUpdateDraw(func() { - spotify.view.SetText(fmt.Sprintf("%d", i)) - }) - i++ + _, _, w, _ := spotify.view.GetInnerRect() + + if w != 0 { + + 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.screenApp.app.QueueUpdateDraw(func() { + spotify.view.SetText(spotify.buffer) + }) + + } + + time.Sleep(50 * time.Millisecond) } } From ff25d5238e2e454a0e5fa070504507f9bcb90694 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Sat, 2 Mar 2024 02:57:52 +0100 Subject: [PATCH 6/9] chore: Add ScreenApp function --- screen/main.go | 6 ++++++ screen/spotify.go | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/screen/main.go b/screen/main.go index a69a678..1bf0336 100644 --- a/screen/main.go +++ b/screen/main.go @@ -20,6 +20,12 @@ 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(), diff --git a/screen/spotify.go b/screen/spotify.go index 364fb24..f74266e 100644 --- a/screen/spotify.go +++ b/screen/spotify.go @@ -2,9 +2,10 @@ package screen import ( "fmt" + "time" + "github.com/gdamore/tcell/v2" "github.com/rivo/tview" - "time" ) type Spotify struct { 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 7/9] 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 = "" } From 0b2b809ff8901034d4b8d6f6b3157595e94477b9 Mon Sep 17 00:00:00 2001 From: FKD13 <44001949+FKD13@users.noreply.github.com> Date: Sat, 2 Mar 2024 03:54:04 +0100 Subject: [PATCH 8/9] cut text if too big --- screen/spotify.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/screen/spotify.go b/screen/spotify.go index e3ed8f2..4c00875 100644 --- a/screen/spotify.go +++ b/screen/spotify.go @@ -46,6 +46,10 @@ func (spotify *Spotify) Run() { 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)) } From 48e02413662093e99ca362d7c07a39a53d2665c0 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Sat, 2 Mar 2024 04:08:21 +0100 Subject: [PATCH 9/9] feat: cammie messages --- api/{endpoints.go => cammie.go} | 41 ++++++++++++++------------ api/main.go | 10 +++++-- screen/cammie.go | 46 ++++++++++++++++++++++++----- screen/main.go | 6 ++-- utils/queue.go | 52 +++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 32 deletions(-) rename api/{endpoints.go => cammie.go} (62%) create mode 100644 utils/queue.go diff --git a/api/endpoints.go b/api/cammie.go similarity index 62% rename from api/endpoints.go rename to api/cammie.go index a7ab9c9..7e73d24 100644 --- a/api/endpoints.go +++ b/api/cammie.go @@ -3,6 +3,8 @@ package api import ( "fmt" "net/http" + "scc/screen" + "slices" gin "github.com/gin-gonic/gin" ) @@ -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{} @@ -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"}) } diff --git a/api/main.go b/api/main.go index b2ee3b5..2ff8c7d 100644 --- a/api/main.go +++ b/api/main.go @@ -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() } diff --git a/screen/cammie.go b/screen/cammie.go index 4d04668..fd35e17 100644 --- a/screen/cammie.go +++ b/screen/cammie.go @@ -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")) + } + }) + }) } diff --git a/screen/main.go b/screen/main.go index 94cff03..1bf0336 100644 --- a/screen/main.go +++ b/screen/main.go @@ -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 diff --git a/utils/queue.go b/utils/queue.go new file mode 100644 index 0000000..9a0ef81 --- /dev/null +++ b/utils/queue.go @@ -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] + } +}