Learn event handling and proper rendering

I'm tired of building crappy and ugly interactive interfaces by hand, I
learned Cobra to build CLI tools so I might as well learn tcell to
create TUI tools.
This commit is contained in:
raul 2024-04-03 09:02:33 +02:00
parent 9c39b1c462
commit d287ad47a9
1 changed files with 22 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"time" "time"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
@ -10,22 +11,38 @@ import (
func main() { func main() {
fmt.Println() fmt.Println()
s, err := tcell.NewScreen() screen, err := tcell.NewScreen()
if err != nil { if err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }
if err := s.Init(); err != nil { if err := screen.Init(); err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }
//defStyle := tcell.StyleDefault.Background //defStyle := tcell.StyleDefault.Background
defStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset) defStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
s.SetStyle(defStyle) screen.SetStyle(defStyle)
go Run(screen, defStyle)
for { for {
switch event := screen.PollEvent().(type) {
case *tcell.EventResize:
screen.Sync()
case *tcell.EventKey:
switch event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyCtrlC {
case true:
screen.Fini()
os.Exit(0)
}
}
}
s.Clear() }
s.SetContent(20, 20, 'A', nil, defStyle)
func Run(screen tcell.Screen, defStyle tcell.Style) {
for {
screen.SetContent(20, 20, 'A', nil, defStyle)
screen.Show()
time.Sleep(time.Second) time.Sleep(time.Second)
} }
} }