From 682219efe7b5fc02ca5f1523af9391c960f38d71 Mon Sep 17 00:00:00 2001 From: Sacha Ligthert Date: Sun, 5 Nov 2023 23:09:24 +0100 Subject: [PATCH] Playing around a bit and getting the hang of tcell. --- snake.go | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 3 deletions(-) diff --git a/snake.go b/snake.go index 4abd908..5bd1159 100644 --- a/snake.go +++ b/snake.go @@ -1,8 +1,115 @@ package main -import "fmt" +import ( + "github.com/gdamore/tcell/v2" + "log" +) -func main() { - fmt.Println("Hello world!") +func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) { + row := y1 + col := x1 + for _, r := range []rune(text) { + s.SetContent(col, row, r, nil, style) + col++ + if col >= x2 { + row++ + col = x1 + } + if row > y2 { + break + } + } } +func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) { + if y2 < y1 { + y1, y2 = y2, y1 + } + if x2 < x1 { + x1, x2 = x2, x1 + } + + // Fill background + for row := y1; row <= y2; row++ { + for col := x1; col <= x2; col++ { + s.SetContent(col, row, ' ', nil, style) + } + } + + // Draw borders + for col := x1; col <= x2; col++ { + s.SetContent(col, y1, '═', nil, style) + s.SetContent(col, y2, '═', nil, style) + } + for row := y1 + 1; row < y2; row++ { + s.SetContent(x1, row, '║', nil, style) + s.SetContent(x2, row, '║', nil, style) + } + + // Only draw corners if necessary + if y1 != y2 && x1 != x2 { + s.SetContent(x1, y1, '╔', nil, style) + s.SetContent(x2, y1, '╗', nil, style) + s.SetContent(x1, y2, '╚', nil, style) + s.SetContent(x2, y2, '╝', nil, style) + } + + drawText(s, x1+1, y1+1, x2-1, y2-1, style, text) +} + +func main() { + // info, err := tcell.LookupTerminfo(os.Getenv("TERM")) + defStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset) + //boxStyle := tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorPurple) + + var err error + + screen, err := tcell.NewScreen() + if err != nil { + log.Println("Error creating screen: ", err) + } + err = screen.Init() + if err != nil { + log.Fatalln("Error initializing screen: ", err) + } + + x, y := screen.Size() + + screen.Clear() + + drawBox(screen, 0, 0, x-1, y-1, defStyle, "There should be worms here!") + + quit := func() { + // You have to catch panics in a defer, clean up, and + // re-raise them - otherwise your application can + // die without leaving any diagnostic trace. + maybePanic := recover() + screen.Fini() + if maybePanic != nil { + panic(maybePanic) + } + } + defer quit() + + for { + screen.Show() + ev := screen.PollEvent() + switch ev := ev.(type) { + case *tcell.EventResize: + x, y := screen.Size() + drawBox(screen, 0, 0, x-1, y-1, defStyle, "There should be worms here!") + screen.Sync() + case *tcell.EventKey: + //fmt.Println("Key: ",ev.Rune()) + if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC { + return + } else if ev.Rune() == 'q' { + return + } else if ev.Key() == tcell.KeyCtrlL { + screen.Sync() + } else if ev.Rune() == 'C' || ev.Rune() == 'c' { + screen.Clear() + } + } + } +}