From 97c59ed8090b6533b21da90d484846fc184045e4 Mon Sep 17 00:00:00 2001 From: Edouard Paris Date: Thu, 28 Mar 2019 14:07:46 +0100 Subject: [PATCH] refac POC models --- ui/controller.go | 23 ++++++++++------------- ui/models/models.go | 28 +++++++++++++++++++++++++--- ui/views/header.go | 27 +++++++++++---------------- ui/views/views.go | 9 ++++++--- 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/ui/controller.go b/ui/controller.go index 4d45d06..5286ca3 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -47,20 +47,16 @@ func cursorUp(g *gocui.Gui, v *gocui.View) error { } func (c *controller) Update(ctx context.Context) error { - info, err := c.models.App.Network.Info(ctx) + err := c.models.RefreshInfo(ctx) if err != nil { return err } - alias := info.Alias - if c.models.App.Config.Network.Name != "" { - alias = c.models.App.Config.Network.Name - } - c.views.Header.Update(alias, "lnd", info.Version) - c.views.Summary.UpdateChannelsStats( - info.NumPendingChannels, - info.NumActiveChannels, - info.NumInactiveChannels, - ) + + // c.views.Summary.UpdateChannelsStats( + // info.NumPendingChannels, + // info.NumActiveChannels, + // info.NumInactiveChannels, + //) channels, err := c.models.App.Network.ListChannels(ctx) if err != nil { @@ -94,8 +90,9 @@ func (c *controller) setKeyBinding(g *gocui.Gui) error { } func newController(app *app.App) *controller { + m := models.New(app) return &controller{ - models: models.New(app), - views: views.New(), + models: m, + views: views.New(m), } } diff --git a/ui/models/models.go b/ui/models/models.go index 9ded538..0ff9eb5 100644 --- a/ui/models/models.go +++ b/ui/models/models.go @@ -1,11 +1,33 @@ package models -import "github.com/edouardparis/lntop/app" +import ( + "context" + + "github.com/edouardparis/lntop/app" + "github.com/edouardparis/lntop/network/models" +) type Models struct { - App *app.App + App *app.App + Info *Info } func New(app *app.App) *Models { - return &Models{App: app} + return &Models{ + App: app, + Info: &Info{}, + } +} + +func (m *Models) RefreshInfo(ctx context.Context) error { + info, err := m.App.Network.Info(ctx) + if err != nil { + return err + } + *m.Info = Info{info} + return nil +} + +type Info struct { + *models.Info } diff --git a/ui/views/header.go b/ui/views/header.go index 8641538..5efbb6f 100644 --- a/ui/views/header.go +++ b/ui/views/header.go @@ -5,6 +5,7 @@ import ( "regexp" "github.com/edouardparis/lntop/ui/color" + "github.com/edouardparis/lntop/ui/models" "github.com/jroimartin/gocui" ) @@ -15,9 +16,7 @@ const ( var versionReg = regexp.MustCompile(`(\d+\.)?(\d+\.)?(\*|\d+)`) type Header struct { - alias string - kind string - version string + Info *models.Info } func (h *Header) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { @@ -29,25 +28,21 @@ func (h *Header) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { } v.Frame = false - version := h.version - matches := versionReg.FindStringSubmatch(h.version) + version := h.Info.Version + matches := versionReg.FindStringSubmatch(h.Info.Version) if len(matches) > 0 { version = matches[0] } - fmt.Fprintln(v, fmt.Sprintf("%s %s", - color.CyanBg(h.alias), - color.Cyan(fmt.Sprintf("%s-v%s", h.kind, version)), + fmt.Fprintln(v, fmt.Sprintf("%s %s %s %s", + color.CyanBg(h.Info.Alias), + color.Cyan(fmt.Sprintf("%s-v%s", "lnd", version)), + fmt.Sprintf("%s %d", color.Cyan("height:"), h.Info.BlockHeight), + fmt.Sprintf("%s %d", color.Cyan("peers:"), h.Info.NumPeers), )) return nil } -func (h *Header) Update(alias, kind, version string) { - h.alias = alias - h.kind = kind - h.version = version -} - -func NewHeader() *Header { - return &Header{} +func NewHeader(info *models.Info) *Header { + return &Header{Info: info} } diff --git a/ui/views/views.go b/ui/views/views.go index 58bcee0..bb05a19 100644 --- a/ui/views/views.go +++ b/ui/views/views.go @@ -1,6 +1,9 @@ package views -import "github.com/jroimartin/gocui" +import ( + "github.com/edouardparis/lntop/ui/models" + "github.com/jroimartin/gocui" +) type Views struct { Header *Header @@ -28,9 +31,9 @@ func (v *Views) Layout(g *gocui.Gui, maxX, maxY int) error { return v.Footer.Set(g, 0, maxY-2, maxX, maxY) } -func New() *Views { +func New(m *models.Models) *Views { return &Views{ - Header: NewHeader(), + Header: NewHeader(m.Info), Footer: NewFooter(), Summary: NewSummary(), Channels: NewChannels(),