refac view channels

pull/1/head
Edouard Paris 5 years ago
parent 97c59ed809
commit 8aa3c9e047

@ -57,13 +57,7 @@ func (c *controller) Update(ctx context.Context) error {
// info.NumActiveChannels, // info.NumActiveChannels,
// info.NumInactiveChannels, // info.NumInactiveChannels,
//) //)
return c.models.RefreshChannels(ctx)
channels, err := c.models.App.Network.ListChannels(ctx)
if err != nil {
return err
}
c.views.Channels.Update(channels)
return nil
} }
func quit(g *gocui.Gui, v *gocui.View) error { func quit(g *gocui.Gui, v *gocui.View) error {

@ -8,14 +8,16 @@ import (
) )
type Models struct { type Models struct {
App *app.App App *app.App
Info *Info Info *Info
Channels *Channels
} }
func New(app *app.App) *Models { func New(app *app.App) *Models {
return &Models{ return &Models{
App: app, App: app,
Info: &Info{}, Info: &Info{},
Channels: &Channels{},
} }
} }
@ -28,6 +30,19 @@ func (m *Models) RefreshInfo(ctx context.Context) error {
return nil 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 { type Info struct {
*models.Info *models.Info
} }
type Channels struct {
Items []*models.Channel
}

@ -6,8 +6,9 @@ import (
"github.com/jroimartin/gocui" "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/color"
"github.com/edouardparis/lntop/ui/models"
) )
const ( const (
@ -17,7 +18,7 @@ const (
type Channels struct { type Channels struct {
*gocui.View *gocui.View
items []*models.Channel channels *models.Channels
} }
func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { 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() { func (c *Channels) display() {
c.Clear() c.Clear()
for _, item := range c.items { for _, item := range c.channels.Items {
line := fmt.Sprintf("%s %s %s %12d %5d %500s", line := fmt.Sprintf("%s %s %s %12d %5d %500s",
active(item), active(item),
gauge(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 { if c.Active {
return color.Green(fmt.Sprintf("%-9s", "active")) return color.Green(fmt.Sprintf("%-9s", "active"))
} }
return color.Red(fmt.Sprintf("%-9s", "inactive")) 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) index := int(c.LocalBalance * int64(20) / c.Capacity)
var buffer bytes.Buffer var buffer bytes.Buffer
for i := 0; i < 20; i++ { 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) return fmt.Sprintf("[%s] %2d%%", buffer.String(), c.LocalBalance*100/c.Capacity)
} }
func NewChannels() *Channels { func NewChannels(channels *models.Channels) *Channels {
return &Channels{} return &Channels{channels: channels}
} }

@ -36,6 +36,6 @@ func New(m *models.Models) *Views {
Header: NewHeader(m.Info), Header: NewHeader(m.Info),
Footer: NewFooter(), Footer: NewFooter(),
Summary: NewSummary(), Summary: NewSummary(),
Channels: NewChannels(), Channels: NewChannels(m.Channels),
} }
} }

Loading…
Cancel
Save