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
117 lines
2.2 KiB
Go
4 years ago
|
package ui
|
||
4 years ago
|
|
||
|
import (
|
||
4 years ago
|
"context"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"github.com/gdamore/tcell/v2"
|
||
4 years ago
|
"github.com/jordanorelli/astro-domu/internal/exit"
|
||
4 years ago
|
"github.com/jordanorelli/astro-domu/internal/wire"
|
||
4 years ago
|
"github.com/jordanorelli/blammo"
|
||
|
)
|
||
|
|
||
4 years ago
|
type UI struct {
|
||
4 years ago
|
*blammo.Log
|
||
4 years ago
|
screen tcell.Screen
|
||
4 years ago
|
mode Mode
|
||
4 years ago
|
client *wire.Client
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func (ui *UI) Run() {
|
||
4 years ago
|
ui.client = &wire.Client{
|
||
4 years ago
|
Log: ui.Child("client"),
|
||
4 years ago
|
Host: "127.0.0.1",
|
||
|
Port: 12805,
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
_, err := ui.client.Dial()
|
||
|
if err != nil {
|
||
|
ui.Error("unable to dial server: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
ctx := context.Background()
|
||
|
|
||
|
ctx, cancel := context.WithCancel(ctx)
|
||
|
defer func() {
|
||
|
ui.Debug("canceling client context")
|
||
|
cancel()
|
||
|
time.Sleep(time.Second)
|
||
|
}()
|
||
|
|
||
4 years ago
|
screen, err := tcell.NewScreen()
|
||
|
if err != nil {
|
||
|
exit.WithMessage(1, "unable to create a screen: %v", err)
|
||
|
}
|
||
4 years ago
|
ui.screen = screen
|
||
4 years ago
|
ui.Debug("sceen created")
|
||
4 years ago
|
|
||
|
if err := screen.Init(); err != nil {
|
||
|
exit.WithMessage(1, "unable to initialize screen: %v", err)
|
||
|
}
|
||
4 years ago
|
ui.Debug("screen initialized")
|
||
4 years ago
|
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())
|
||
4 years ago
|
|
||
|
defer func() {
|
||
4 years ago
|
ui.Debug("finalizing screen")
|
||
4 years ago
|
screen.Fini()
|
||
|
}()
|
||
|
|
||
4 years ago
|
ui.mode = &boxWalker{
|
||
|
width: 10,
|
||
|
height: 6,
|
||
|
}
|
||
4 years ago
|
ui.menu()
|
||
4 years ago
|
ui.client.Close()
|
||
4 years ago
|
ui.Debug("clearing screen")
|
||
4 years ago
|
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.
|
||
4 years ago
|
func (ui *UI) writeString(x, y int, s string, style tcell.Style) {
|
||
4 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
func (ui *UI) menu() {
|
||
4 years ago
|
ui.screen.Clear()
|
||
|
_, height := ui.screen.Size()
|
||
|
ui.writeString(0, height-1, "fart", tcell.StyleDefault)
|
||
|
ui.screen.Sync()
|
||
|
|
||
4 years ago
|
for {
|
||
4 years ago
|
e := ui.screen.PollEvent()
|
||
4 years ago
|
if e == nil {
|
||
|
break
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
switch v := e.(type) {
|
||
|
case *tcell.EventKey:
|
||
|
key := v.Key()
|
||
|
if key == tcell.KeyCtrlC {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
ui.mode.handleEvent(ui, e)
|
||
4 years ago
|
ui.screen.Clear()
|
||
|
ui.mode.draw(ui)
|
||
|
ui.screen.Show()
|
||
4 years ago
|
}
|
||
|
}
|