2
0
mirror of https://github.com/edouardparis/lntop synced 2024-11-06 03:20:21 +00:00
lntop/ui/controller.go

258 lines
5.2 KiB
Go
Raw Normal View History

2019-03-22 08:36:13 +00:00
package ui
import (
"context"
2019-03-28 12:36:23 +00:00
"github.com/jroimartin/gocui"
2019-03-22 08:36:13 +00:00
"github.com/edouardparis/lntop/app"
2019-03-29 16:30:48 +00:00
"github.com/edouardparis/lntop/events"
"github.com/edouardparis/lntop/logging"
2019-03-28 12:36:23 +00:00
"github.com/edouardparis/lntop/ui/models"
2019-03-22 08:36:13 +00:00
"github.com/edouardparis/lntop/ui/views"
)
type controller struct {
2019-04-01 11:24:49 +00:00
logger logging.Logger
2019-03-28 12:36:23 +00:00
models *models.Models
views *views.Views
2019-03-22 08:36:13 +00:00
}
func (c *controller) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
2019-03-28 08:47:47 +00:00
return c.views.Layout(g, maxX, maxY)
2019-03-22 08:36:13 +00:00
}
2019-04-10 11:16:51 +00:00
func (c *controller) cursorDown(g *gocui.Gui, v *gocui.View) error {
view := c.views.Get(v)
if view != nil {
return view.CursorDown()
2019-03-22 08:36:13 +00:00
}
return nil
}
2019-04-10 11:16:51 +00:00
func (c *controller) cursorUp(g *gocui.Gui, v *gocui.View) error {
view := c.views.Get(v)
if view != nil {
return view.CursorUp()
2019-04-10 07:22:44 +00:00
}
return nil
}
2019-04-10 11:16:51 +00:00
func (c *controller) cursorRight(g *gocui.Gui, v *gocui.View) error {
view := c.views.Get(v)
if view != nil {
return view.CursorRight()
2019-04-10 07:22:44 +00:00
}
return nil
}
2019-04-10 11:16:51 +00:00
func (c *controller) cursorLeft(g *gocui.Gui, v *gocui.View) error {
view := c.views.Get(v)
if view != nil {
return view.CursorLeft()
2019-03-22 08:36:13 +00:00
}
return nil
}
2019-03-29 11:38:09 +00:00
func (c *controller) SetModels(ctx context.Context) error {
2019-03-28 13:07:46 +00:00
err := c.models.RefreshInfo(ctx)
2019-03-22 17:14:43 +00:00
if err != nil {
return err
}
2019-03-28 13:07:46 +00:00
2019-03-29 11:38:09 +00:00
err = c.models.RefreshWalletBalance(ctx)
if err != nil {
return err
}
err = c.models.RefreshChannelsBalance(ctx)
if err != nil {
return err
}
2019-03-29 08:30:05 +00:00
return c.models.RefreshChannels(ctx)
2019-03-22 17:14:43 +00:00
}
2019-04-01 11:24:49 +00:00
func (c *controller) Listen(ctx context.Context, g *gocui.Gui, sub chan *events.Event) {
c.logger.Debug("Listening...")
2019-04-09 08:08:43 +00:00
refresh := func(fn ...func(context.Context) error) {
for i := range fn {
err := fn[i](ctx)
if err != nil {
c.logger.Error("failed", logging.Error(err))
}
}
2019-04-09 11:52:12 +00:00
g.Update(func(*gocui.Gui) error { return nil })
2019-04-09 08:08:43 +00:00
}
2019-03-29 16:30:48 +00:00
for event := range sub {
2019-04-09 08:08:43 +00:00
c.logger.Debug("event received", logging.String("type", event.Type))
2019-04-01 11:24:49 +00:00
switch event.Type {
2019-04-01 12:46:59 +00:00
case events.BlockReceived:
2019-04-09 08:08:43 +00:00
refresh(c.models.RefreshInfo)
2019-04-15 11:34:29 +00:00
case events.WalletBalanceUpdated:
refresh(
c.models.RefreshInfo,
c.models.RefreshWalletBalance,
)
case events.ChannelBalanceUpdated:
refresh(
c.models.RefreshInfo,
c.models.RefreshChannelsBalance,
c.models.RefreshChannels,
)
2019-04-01 12:46:59 +00:00
case events.ChannelPending:
2019-04-09 08:08:43 +00:00
refresh(
c.models.RefreshInfo,
c.models.RefreshChannelsBalance,
c.models.RefreshChannels,
)
2019-04-01 12:46:59 +00:00
case events.ChannelActive:
2019-04-09 08:08:43 +00:00
refresh(
c.models.RefreshInfo,
c.models.RefreshChannelsBalance,
c.models.RefreshChannels,
)
2019-04-01 12:46:59 +00:00
case events.ChannelInactive:
2019-04-09 08:08:43 +00:00
refresh(
c.models.RefreshInfo,
c.models.RefreshChannelsBalance,
c.models.RefreshChannels,
)
2019-04-11 11:30:28 +00:00
case events.InvoiceSettled:
refresh(
c.models.RefreshInfo,
c.models.RefreshChannelsBalance,
c.models.RefreshChannels,
)
2019-04-01 12:46:59 +00:00
case events.PeerUpdated:
2019-04-09 08:08:43 +00:00
refresh(c.models.RefreshInfo)
2019-03-29 16:30:48 +00:00
}
}
}
2019-03-22 08:36:13 +00:00
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
2019-04-02 08:14:39 +00:00
func (c *controller) Help(g *gocui.Gui, v *gocui.View) error {
2019-04-01 16:30:25 +00:00
maxX, maxY := g.Size()
2019-04-10 07:44:52 +00:00
view := c.views.Get(g.CurrentView())
2019-04-02 08:14:39 +00:00
if view == nil {
return nil
}
if view.Name() != views.HELP {
c.views.SetPrevious(view)
return c.views.Help.Set(g, 0, -1, maxX, maxY)
}
err := g.DeleteView(views.HELP)
if err != nil {
return err
}
if c.views.Previous != nil {
_, err := g.SetCurrentView(c.views.Previous.Name())
return err
}
return nil
2019-04-01 16:30:25 +00:00
}
2019-04-02 12:27:51 +00:00
func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error {
maxX, maxY := g.Size()
2019-04-10 07:44:52 +00:00
view := c.views.Get(v)
2019-04-02 12:27:51 +00:00
if view == nil {
return nil
}
switch view.Name() {
case views.CHANNELS:
c.views.SetPrevious(view)
_, cy := v.Cursor()
2019-04-03 08:12:36 +00:00
err := c.models.SetCurrentChannel(context.Background(), cy)
if err != nil {
return err
}
2019-04-05 07:35:49 +00:00
err = c.views.Channel.Set(g, 0, 6, maxX-1, maxY)
2019-04-03 08:12:36 +00:00
if err != nil {
return err
}
_, err = g.SetCurrentView(c.views.Channel.Name())
return err
case views.CHANNEL:
2019-04-03 12:11:49 +00:00
err := c.views.Channel.Delete(g)
2019-04-03 08:12:36 +00:00
if err != nil {
return err
}
if c.views.Previous != nil {
_, err := g.SetCurrentView(c.views.Previous.Name())
return err
}
2019-04-05 07:35:49 +00:00
err = c.views.Channels.Set(g, 0, 6, maxX-1, maxY)
2019-04-03 08:12:36 +00:00
if err != nil {
return err
}
2019-04-02 12:27:51 +00:00
}
return nil
}
2019-03-22 08:36:13 +00:00
func (c *controller) setKeyBinding(g *gocui.Gui) error {
err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit)
if err != nil {
return err
}
2019-04-05 07:48:29 +00:00
err = g.SetKeybinding("", gocui.KeyF10, gocui.ModNone, quit)
if err != nil {
return err
}
2019-04-10 11:16:51 +00:00
err = g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, c.cursorUp)
2019-03-22 08:36:13 +00:00
if err != nil {
return err
}
2019-04-10 11:16:51 +00:00
err = g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, c.cursorDown)
2019-03-22 08:36:13 +00:00
if err != nil {
return err
}
2019-04-10 11:16:51 +00:00
err = g.SetKeybinding("", gocui.KeyArrowLeft, gocui.ModNone, c.cursorLeft)
2019-04-10 07:22:44 +00:00
if err != nil {
return err
}
2019-04-10 11:16:51 +00:00
err = g.SetKeybinding("", gocui.KeyArrowRight, gocui.ModNone, c.cursorRight)
2019-04-10 07:22:44 +00:00
if err != nil {
return err
}
2019-04-02 12:27:51 +00:00
err = g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, c.OnEnter)
if err != nil {
return err
}
2019-04-02 08:14:39 +00:00
err = g.SetKeybinding("", gocui.KeyF1, gocui.ModNone, c.Help)
2019-04-01 16:30:25 +00:00
if err != nil {
return err
}
2019-03-22 08:36:13 +00:00
return nil
}
func newController(app *app.App) *controller {
2019-03-28 13:07:46 +00:00
m := models.New(app)
2019-03-22 08:36:13 +00:00
return &controller{
2019-04-01 11:24:49 +00:00
logger: app.Logger.With(logging.String("logger", "controller")),
2019-03-28 13:07:46 +00:00
models: m,
views: views.New(m),
2019-03-22 08:36:13 +00:00
}
}