You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lntop/ui/controller.go

102 lines
2.0 KiB
Go

package ui
import (
"context"
5 years ago
"github.com/jroimartin/gocui"
"github.com/edouardparis/lntop/app"
5 years ago
"github.com/edouardparis/lntop/ui/models"
"github.com/edouardparis/lntop/ui/views"
)
type controller struct {
5 years ago
models *models.Models
views *views.Views
}
func (c *controller) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
5 years ago
return c.views.Layout(g, maxX, maxY)
}
func cursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
}
}
}
return nil
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
}
return nil
}
5 years ago
func (c *controller) Update(ctx context.Context) error {
5 years ago
info, err := c.models.App.Network.Info(ctx)
5 years ago
if err != nil {
return err
}
5 years ago
alias := info.Alias
5 years ago
if c.models.App.Config.Network.Name != "" {
alias = c.models.App.Config.Network.Name
5 years ago
}
c.views.Header.Update(alias, "lnd", info.Version)
5 years ago
c.views.Summary.UpdateChannelsStats(
info.NumPendingChannels,
info.NumActiveChannels,
info.NumInactiveChannels,
)
5 years ago
5 years ago
channels, err := c.models.App.Network.ListChannels(ctx)
5 years ago
if err != nil {
return err
}
5 years ago
c.views.Channels.Update(channels)
5 years ago
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func (c *controller) setKeyBinding(g *gocui.Gui) error {
err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit)
if err != nil {
return err
}
err = g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp)
if err != nil {
return err
}
err = g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown)
if err != nil {
return err
}
return nil
}
func newController(app *app.App) *controller {
return &controller{
5 years ago
models: models.New(app),
views: views.New(),
}
}