You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.2 KiB
Go

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{
4 years ago
Log: ui.Child("client"),
Host: "127.0.0.1",
Port: 12805,
4 years ago
}
_, 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
4 years ago
ui.Debug("sceen created")
if err := screen.Init(); err != nil {
exit.WithMessage(1, "unable to initialize screen: %v", err)
}
4 years ago
ui.Debug("screen initialized")
width, height := screen.Size()
4 years ago
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() {
4 years ago
ui.Debug("finalizing screen")
screen.Fini()
}()
4 years ago
ui.mode = &boxWalker{
width: 10,
height: 6,
}
ui.menu()
ui.client.Close()
4 years ago
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
}
}
4 years ago
ui.mode.handleEvent(ui, e)
ui.screen.Clear()
ui.mode.draw(ui)
ui.screen.Show()
}
}