moving the UI into a package

master
Jordan Orelli 4 years ago
parent 9bf04dd6c8
commit 47482be17d

@ -1,13 +1,13 @@
package main
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/wire"
)
type uiMode interface {
handleEvent(*ui, tcell.Event) bool
draw(*ui)
type Mode interface {
handleEvent(*UI, tcell.Event) bool
draw(*UI)
}
type boxWalker struct {
@ -16,7 +16,7 @@ type boxWalker struct {
position point
}
func (m *boxWalker) handleEvent(ui *ui, e tcell.Event) bool {
func (m *boxWalker) handleEvent(ui *UI, e tcell.Event) bool {
switch v := e.(type) {
case *tcell.EventKey:
key := v.Key()
@ -47,7 +47,7 @@ func (m *boxWalker) move(dx, dy int) {
m.position.y = clamp(m.position.y+dy, 0, m.height-1)
}
func (m *boxWalker) draw(ui *ui) {
func (m *boxWalker) draw(ui *UI) {
offset := point{1, 1}
// fill in background dots first

@ -1,4 +1,4 @@
package main
package ui
type point struct{ x, y int }

@ -1,4 +1,4 @@
package main
package ui
import (
"context"
@ -10,15 +10,14 @@ import (
"github.com/jordanorelli/blammo"
)
// ui represents our terminal-based user interface
type ui struct {
type UI struct {
*blammo.Log
screen tcell.Screen
mode uiMode
mode Mode
client *wire.Client
}
func (ui *ui) run() {
func (ui *UI) Run() {
ui.client = &wire.Client{
Log: ui.Child("client"),
Host: "127.0.0.1",
@ -75,7 +74,7 @@ func (ui *ui) run() {
// 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) {
func (ui *UI) writeString(x, y int, s string, style tcell.Style) {
width, height := ui.screen.Size()
if y > height {
return
@ -89,7 +88,7 @@ func (ui *ui) writeString(x, y int, s string, style tcell.Style) {
}
}
func (ui *ui) menu() {
func (ui *UI) menu() {
ui.screen.Clear()
_, height := ui.screen.Size()
ui.writeString(0, height-1, "fart", tcell.StyleDefault)

@ -7,6 +7,7 @@ import (
"github.com/jordanorelli/astro-domu/internal/exit"
"github.com/jordanorelli/astro-domu/internal/server"
"github.com/jordanorelli/astro-domu/internal/ui"
"github.com/jordanorelli/blammo"
)
@ -61,8 +62,8 @@ func runClient() {
log.Info("total play time: %v", finished.Sub(start))
}()
ui := ui{
ui := ui.UI{
Log: log.Child("ui"),
}
ui.run()
ui.Run()
}

Loading…
Cancel
Save