refac POC models

pull/1/head
Edouard Paris 5 years ago
parent 7611eea6a0
commit 97c59ed809

@ -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),
}
}

@ -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
}

@ -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}
}

@ -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(),

Loading…
Cancel
Save