refac ui: MVC

pull/1/head
Edouard Paris 5 years ago
parent 9cd954380b
commit c2eb0ad4d7

@ -1,6 +1,8 @@
package ui
import (
"context"
"github.com/jroimartin/gocui"
"github.com/edouardparis/lntop/app"
@ -8,6 +10,7 @@ import (
)
type Ui struct {
app *app.App
channels *views.Channels
}
@ -26,7 +29,7 @@ func (u *Ui) Run() error {
return err
}
g.Update(u.channels.Refresh)
g.Update(u.Refresh)
err = g.MainLoop()
if err != nil && err != gocui.ErrQuit {
@ -81,6 +84,15 @@ func cursorUp(g *gocui.Gui, v *gocui.View) error {
return nil
}
func (u *Ui) Refresh(g *gocui.Gui) error {
channels, err := u.app.Network.ListChannels(context.Background())
if err != nil {
return err
}
u.channels.Update(channels)
return nil
}
func (u *Ui) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
return u.channels.Set(g, 0, maxY/8, maxX-1, maxY-1)
@ -92,6 +104,7 @@ func quit(g *gocui.Gui, v *gocui.View) error {
func New(app *app.App) *Ui {
return &Ui{
channels: views.NewChannels(app.Network),
app: app,
channels: views.NewChannels(),
}
}

@ -2,12 +2,10 @@ package views
import (
"bytes"
"context"
"fmt"
"github.com/jroimartin/gocui"
"github.com/edouardparis/lntop/network"
"github.com/edouardparis/lntop/network/models"
"github.com/edouardparis/lntop/ui/color"
)
@ -19,8 +17,7 @@ const (
type Channels struct {
*gocui.View
items []*models.Channel
network *network.Network
items []*models.Channel
}
func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
@ -57,37 +54,15 @@ func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
func displayChannelsHeader(v *gocui.View) {
fmt.Fprintln(v, fmt.Sprintf("%-9s %-19s %12s %12s",
"status",
"id",
"local",
"capacity",
"Status",
"CID",
"Local",
"Capacity",
))
}
func (c *Channels) Refresh(g *gocui.Gui) error {
var err error
c.View, err = g.View(CHANNELS)
if err != nil {
return err
}
err = c.update(context.Background())
if err != nil {
return err
}
c.display()
return nil
}
func (c *Channels) update(ctx context.Context) error {
channels, err := c.network.ListChannels(ctx)
if err != nil {
return err
}
c.items = channels
return nil
func (c *Channels) Update(items []*models.Channel) {
c.items = items
}
func (c *Channels) display() {
@ -103,10 +78,6 @@ func (c *Channels) display() {
}
}
func NewChannels(network *network.Network) *Channels {
return &Channels{network: network}
}
func active(c *models.Channel) string {
if c.Active {
return color.Green(fmt.Sprintf("%-9s", "active"))
@ -124,3 +95,7 @@ func chartID(c *models.Channel) string {
return buffer.String()
}
func NewChannels() *Channels {
return &Channels{}
}

Loading…
Cancel
Save