diff --git a/ui/controller.go b/ui/controller.go index 5286ca3..a88d0d1 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -57,13 +57,7 @@ func (c *controller) Update(ctx context.Context) error { // info.NumActiveChannels, // info.NumInactiveChannels, //) - - channels, err := c.models.App.Network.ListChannels(ctx) - if err != nil { - return err - } - c.views.Channels.Update(channels) - return nil + return c.models.RefreshChannels(ctx) } func quit(g *gocui.Gui, v *gocui.View) error { diff --git a/ui/models/models.go b/ui/models/models.go index 0ff9eb5..9207313 100644 --- a/ui/models/models.go +++ b/ui/models/models.go @@ -8,14 +8,16 @@ import ( ) type Models struct { - App *app.App - Info *Info + App *app.App + Info *Info + Channels *Channels } func New(app *app.App) *Models { return &Models{ - App: app, - Info: &Info{}, + App: app, + Info: &Info{}, + Channels: &Channels{}, } } @@ -28,6 +30,19 @@ func (m *Models) RefreshInfo(ctx context.Context) error { return nil } +func (m *Models) RefreshChannels(ctx context.Context) error { + channels, err := m.App.Network.ListChannels(ctx) + if err != nil { + return err + } + *m.Channels = Channels{channels} + return nil +} + type Info struct { *models.Info } + +type Channels struct { + Items []*models.Channel +} diff --git a/ui/views/channels.go b/ui/views/channels.go index a9badb1..9cb0261 100644 --- a/ui/views/channels.go +++ b/ui/views/channels.go @@ -6,8 +6,9 @@ import ( "github.com/jroimartin/gocui" - "github.com/edouardparis/lntop/network/models" + netmodels "github.com/edouardparis/lntop/network/models" "github.com/edouardparis/lntop/ui/color" + "github.com/edouardparis/lntop/ui/models" ) const ( @@ -17,7 +18,7 @@ const ( type Channels struct { *gocui.View - items []*models.Channel + channels *models.Channels } func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { @@ -62,13 +63,9 @@ func displayChannelsColumns(v *gocui.View) { )) } -func (c *Channels) Update(items []*models.Channel) { - c.items = items -} - func (c *Channels) display() { c.Clear() - for _, item := range c.items { + for _, item := range c.channels.Items { line := fmt.Sprintf("%s %s %s %12d %5d %500s", active(item), gauge(item), @@ -81,14 +78,14 @@ func (c *Channels) display() { } } -func active(c *models.Channel) string { +func active(c *netmodels.Channel) string { if c.Active { return color.Green(fmt.Sprintf("%-9s", "active")) } return color.Red(fmt.Sprintf("%-9s", "inactive")) } -func gauge(c *models.Channel) string { +func gauge(c *netmodels.Channel) string { index := int(c.LocalBalance * int64(20) / c.Capacity) var buffer bytes.Buffer for i := 0; i < 20; i++ { @@ -101,6 +98,6 @@ func gauge(c *models.Channel) string { return fmt.Sprintf("[%s] %2d%%", buffer.String(), c.LocalBalance*100/c.Capacity) } -func NewChannels() *Channels { - return &Channels{} +func NewChannels(channels *models.Channels) *Channels { + return &Channels{channels: channels} } diff --git a/ui/views/views.go b/ui/views/views.go index bb05a19..bd778f1 100644 --- a/ui/views/views.go +++ b/ui/views/views.go @@ -36,6 +36,6 @@ func New(m *models.Models) *Views { Header: NewHeader(m.Info), Footer: NewFooter(), Summary: NewSummary(), - Channels: NewChannels(), + Channels: NewChannels(m.Channels), } }