what am i even doing with my life

master
Jordan Orelli 4 years ago
parent 944b75bbd4
commit 13ee0e7fa8

@ -85,7 +85,7 @@ func (sn *session) read() {
sn.outbox <- wire.ErrorResponse(0, "unable to parse request: %v", err) sn.outbox <- wire.ErrorResponse(0, "unable to parse request: %v", err)
break break
} }
sn.outbox <- wire.NewResponse(req.Seq, wire.OK{}) sn.outbox <- wire.Response{req.Seq, wire.OK{}}
case websocket.BinaryMessage: case websocket.BinaryMessage:
sn.outbox <- wire.ErrorResponse(0, "unable to parse binary frames") sn.outbox <- wire.ErrorResponse(0, "unable to parse binary frames")
} }

@ -1,6 +1,8 @@
package ui package ui
import ( import (
"fmt"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/exit" "github.com/jordanorelli/astro-domu/internal/exit"
"github.com/jordanorelli/astro-domu/internal/wire" "github.com/jordanorelli/astro-domu/internal/wire"
@ -15,18 +17,41 @@ type UI struct {
} }
func (ui *UI) Run() { func (ui *UI) Run() {
ui.setupTerminal()
defer ui.clearTerminal()
if err := ui.connect(); err != nil {
return
}
ui.mode = &boxWalker{width: 10, height: 6}
ui.Info("running ui")
if ui.handleUserInput() {
ui.Info("user requested close")
ui.Info("closing client")
ui.client.Close()
ui.Info("client closed")
ui.Info("finalizing screen")
}
ui.Info("run loop done, shutting down")
}
func (ui *UI) connect() error {
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",
Port: 12805, Port: 12805,
} }
notifications, err := ui.client.Dial() c, err := ui.client.Dial()
if err != nil { if err != nil {
exit.WithMessage(1, "unable to dial server: %v", err) return fmt.Errorf("unable to dial server: %v", err)
}
go ui.handleNotifications(c)
return nil
} }
go ui.handleNotifications(notifications)
func (ui *UI) setupTerminal() {
screen, err := tcell.NewScreen() screen, err := tcell.NewScreen()
if err != nil { if err != nil {
exit.WithMessage(1, "unable to create a screen: %v", err) exit.WithMessage(1, "unable to create a screen: %v", err)
@ -43,21 +68,11 @@ func (ui *UI) Run() {
ui.Debug("screen height: %d", height) ui.Debug("screen height: %d", height)
ui.Debug("screen colors: %v", screen.Colors()) ui.Debug("screen colors: %v", screen.Colors())
ui.Debug("screen has mouse: %v", screen.HasMouse()) ui.Debug("screen has mouse: %v", screen.HasMouse())
ui.mode = &boxWalker{
width: 10,
height: 6,
}
ui.Info("running ui")
if ui.run() {
ui.Info("user requested close")
ui.Info("closing client")
ui.client.Close()
ui.Info("client closed")
ui.Info("finalizing screen")
} }
ui.Info("run loop done, shutting down")
screen.Fini() func (ui *UI) clearTerminal() {
ui.screen.Clear()
ui.screen.Fini()
} }
func (ui *UI) handleNotifications(c <-chan wire.Response) { func (ui *UI) handleNotifications(c <-chan wire.Response) {
@ -68,6 +83,7 @@ func (ui *UI) handleNotifications(c <-chan wire.Response) {
ui.Info("clearing and finalizing screen from notifications goroutine") ui.Info("clearing and finalizing screen from notifications goroutine")
ui.screen.Clear() ui.screen.Clear()
ui.screen.Fini() ui.screen.Fini()
ui.Info("screen finalized")
} }
// 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
@ -87,7 +103,7 @@ func (ui *UI) writeString(x, y int, s string, style tcell.Style) {
} }
} }
func (ui *UI) run() bool { func (ui *UI) handleUserInput() bool {
ui.screen.Clear() ui.screen.Clear()
ui.mode.draw(ui) ui.mode.draw(ui)
@ -98,6 +114,7 @@ func (ui *UI) run() bool {
// someone else shut us down, so return false // someone else shut us down, so return false
return false return false
} }
ui.Info("screen sees event: %v", e)
switch v := e.(type) { switch v := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:

@ -1,6 +1,7 @@
package wire package wire
import ( import (
"encoding/json"
"fmt" "fmt"
) )
@ -12,6 +13,10 @@ func (e Error) Error() string { return e.val.Error() }
func (e Error) NetTag() string { return "error" } func (e Error) NetTag() string { return "error" }
func (e Error) Unwrap() error { return e.val } func (e Error) Unwrap() error { return e.val }
func (e Error) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Error())
}
func Errorf(t string, args ...interface{}) Error { func Errorf(t string, args ...interface{}) Error {
return Error{val: fmt.Errorf(t, args...)} return Error{val: fmt.Errorf(t, args...)}
} }

@ -4,6 +4,8 @@ type OK struct{}
func (OK) NetTag() string { return "ok" } func (OK) NetTag() string { return "ok" }
func (OK) MarshalJSON() ([]byte, error) { return []byte(`"ok"`), nil }
func init() { func init() {
Register(func() Value { return new(OK) }) Register(func() Value { return new(OK) })
} }

@ -1,19 +1,45 @@
package wire package wire
import (
"encoding/json"
"fmt"
)
type Response struct { type Response struct {
Re int `json:"re,omitempty"` Re int
Type string `json:"type"` Body Value
Body interface{} `json:"body"` }
func (r Response) MarshalJSON() ([]byte, error) {
return json.Marshal([3]interface{}{r.Re, r.Body.NetTag(), r.Body})
} }
func NewResponse(re int, v Value) Response { func (r *Response) UnmarshalJSON(b []byte) error {
return Response{ var parts [3]json.RawMessage
Re: re, if err := json.Unmarshal(b, &parts); err != nil {
Type: v.NetTag(), return err
Body: v, }
if err := json.Unmarshal(parts[0], &r.Re); err != nil {
return err
}
var tag string
if err := json.Unmarshal(parts[1], &tag); err != nil {
return err
}
f, ok := registry[tag]
if !ok {
return fmt.Errorf("unknown tag: %q", tag)
}
v := f()
if err := json.Unmarshal(parts[2], v); err != nil {
return err
} }
r.Body = v
return nil
} }
func ErrorResponse(re int, t string, args ...interface{}) Response { func ErrorResponse(re int, t string, args ...interface{}) Response {
return NewResponse(re, Errorf(t, args...)) return Response{re, Errorf(t, args...)}
} }

@ -7,3 +7,7 @@ type Self_Move struct {
} }
func (Self_Move) NetTag() string { return "self/move" } func (Self_Move) NetTag() string { return "self/move" }
func init() {
Register(func() Value { return new(Self_Move) })
}

Loading…
Cancel
Save