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 ( import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/wire" "github.com/jordanorelli/astro-domu/internal/wire"
) )
type uiMode interface { type Mode interface {
handleEvent(*ui, tcell.Event) bool handleEvent(*UI, tcell.Event) bool
draw(*ui) draw(*UI)
} }
type boxWalker struct { type boxWalker struct {
@ -16,7 +16,7 @@ type boxWalker struct {
position point 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) { switch v := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:
key := v.Key() 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) 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} offset := point{1, 1}
// fill in background dots first // fill in background dots first

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

@ -1,4 +1,4 @@
package main package ui
import ( import (
"context" "context"
@ -10,15 +10,14 @@ import (
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
) )
// ui represents our terminal-based user interface type UI struct {
type ui struct {
*blammo.Log *blammo.Log
screen tcell.Screen screen tcell.Screen
mode uiMode mode Mode
client *wire.Client client *wire.Client
} }
func (ui *ui) run() { func (ui *UI) Run() {
ui.client = &wire.Client{ ui.client = &wire.Client{
Log: ui.Child("client"), Log: ui.Child("client"),
Host: "127.0.0.1", 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 // 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 // at the location (x, y). Writing of the screen just fails silently so don't
// do that. // 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() width, height := ui.screen.Size()
if y > height { if y > height {
return 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() ui.screen.Clear()
_, height := ui.screen.Size() _, height := ui.screen.Size()
ui.writeString(0, height-1, "fart", tcell.StyleDefault) 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/exit"
"github.com/jordanorelli/astro-domu/internal/server" "github.com/jordanorelli/astro-domu/internal/server"
"github.com/jordanorelli/astro-domu/internal/ui"
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
) )
@ -61,8 +62,8 @@ func runClient() {
log.Info("total play time: %v", finished.Sub(start)) log.Info("total play time: %v", finished.Sub(start))
}() }()
ui := ui{ ui := ui.UI{
Log: log.Child("ui"), Log: log.Child("ui"),
} }
ui.run() ui.Run()
} }

Loading…
Cancel
Save