package ui import ( "context" "time" "github.com/gdamore/tcell/v2" "github.com/jordanorelli/astro-domu/internal/exit" "github.com/jordanorelli/astro-domu/internal/wire" "github.com/jordanorelli/blammo" ) type UI struct { *blammo.Log screen tcell.Screen mode Mode client *wire.Client } func (ui *UI) Run() { ui.client = &wire.Client{ Log: ui.Child("client"), Host: "127.0.0.1", Port: 12805, } _, err := ui.client.Dial() if err != nil { ui.Error("unable to dial server: %v", err) return } ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer func() { ui.Debug("canceling client context") cancel() time.Sleep(time.Second) }() screen, err := tcell.NewScreen() if err != nil { exit.WithMessage(1, "unable to create a screen: %v", err) } ui.screen = screen ui.Debug("sceen created") if err := screen.Init(); err != nil { exit.WithMessage(1, "unable to initialize screen: %v", err) } ui.Debug("screen initialized") width, height := screen.Size() ui.Debug("screen width: %d", width) ui.Debug("screen height: %d", height) ui.Debug("screen colors: %v", screen.Colors()) ui.Debug("screen has mouse: %v", screen.HasMouse()) defer func() { ui.Debug("finalizing screen") screen.Fini() }() ui.mode = &boxWalker{ width: 10, height: 6, } ui.menu() ui.client.Close() ui.Debug("clearing screen") screen.Clear() } // writeString writes a string in the given style from left to right beginning // at the location (x, y). Writing of the screen just fails silently so don't // do that. func (ui *UI) writeString(x, y int, s string, style tcell.Style) { width, height := ui.screen.Size() if y > height { return } for i, r := range []rune(s) { x := x + i if x > width { return } ui.screen.SetContent(x, y, r, nil, style) } } func (ui *UI) menu() { ui.screen.Clear() _, height := ui.screen.Size() ui.writeString(0, height-1, "fart", tcell.StyleDefault) ui.screen.Sync() for { e := ui.screen.PollEvent() if e == nil { break } switch v := e.(type) { case *tcell.EventKey: key := v.Key() if key == tcell.KeyCtrlC { return } } ui.mode.handleEvent(ui, e) ui.screen.Clear() ui.mode.draw(ui) ui.screen.Show() } }