From 78c41c0741b0fbdf986d4382b701c8e87911c77b Mon Sep 17 00:00:00 2001 From: Edouard Paris Date: Tue, 2 Apr 2019 14:27:51 +0200 Subject: [PATCH] ft channel view --- ui/controller.go | 22 +++++++++++++++++ ui/models/channels.go | 19 +++++++++++++++ ui/models/models.go | 17 +++++++++---- ui/views/channels.go | 55 ++++++++++++++++++++++++++++++++++--------- ui/views/views.go | 2 ++ 5 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 ui/models/channels.go diff --git a/ui/controller.go b/ui/controller.go index 8ee8896..81747c0 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -121,6 +121,23 @@ func (c *controller) Help(g *gocui.Gui, v *gocui.View) error { return nil } +func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error { + maxX, maxY := g.Size() + view := c.views.Get(v.Name()) + if view == nil { + return nil + } + + switch view.Name() { + case views.CHANNELS: + c.views.SetPrevious(view) + _, cy := v.Cursor() + c.models.SetCurrentChannel(context.Background(), cy) + return c.views.Channel.Set(g, 0, 6, maxX-1, maxY-1) + } + return nil +} + func (c *controller) setKeyBinding(g *gocui.Gui) error { err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit) if err != nil { @@ -137,6 +154,11 @@ func (c *controller) setKeyBinding(g *gocui.Gui) error { return err } + err = g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, c.OnEnter) + if err != nil { + return err + } + err = g.SetKeybinding("", gocui.KeyF1, gocui.ModNone, c.Help) if err != nil { return err diff --git a/ui/models/channels.go b/ui/models/channels.go new file mode 100644 index 0000000..4b609b2 --- /dev/null +++ b/ui/models/channels.go @@ -0,0 +1,19 @@ +package models + +import "github.com/edouardparis/lntop/network/models" + +type Channels struct { + Items []*models.Channel +} + +func (c *Channels) Get(index int) *models.Channel { + if index < 0 || index > len(c.Items) { + return nil + } + + return c.Items[index] +} + +type Channel struct { + Item *models.Channel +} diff --git a/ui/models/models.go b/ui/models/models.go index 3ef8864..e63ff5c 100644 --- a/ui/models/models.go +++ b/ui/models/models.go @@ -11,6 +11,7 @@ type Models struct { App *app.App Info *Info Channels *Channels + CurrentChannel *Channel WalletBalance *WalletBalance ChannelsBalance *ChannelsBalance } @@ -22,6 +23,7 @@ func New(app *app.App) *Models { Channels: &Channels{}, WalletBalance: &WalletBalance{}, ChannelsBalance: &ChannelsBalance{}, + CurrentChannel: &Channel{}, } } @@ -38,16 +40,21 @@ func (m *Models) RefreshInfo(ctx context.Context) error { return nil } -type Channels struct { - Items []*models.Channel -} - func (m *Models) RefreshChannels(ctx context.Context) error { channels, err := m.App.Network.ListChannels(ctx) if err != nil { return err } - *m.Channels = Channels{channels} + *m.Channels = Channels{Items: channels} + return nil +} + +func (m *Models) SetCurrentChannel(ctx context.Context, index int) error { + channel := m.Channels.Get(index) + if channel == nil { + return nil + } + *m.CurrentChannel = Channel{Item: channel} return nil } diff --git a/ui/views/channels.go b/ui/views/channels.go index bb0b60f..951849d 100644 --- a/ui/views/channels.go +++ b/ui/views/channels.go @@ -12,12 +12,12 @@ import ( ) const ( + CHANNEL = "channel" CHANNELS = "channels" CHANNELS_COLUMNS = "channels_columns" ) type Channels struct { - *gocui.View channels *models.Channels } @@ -37,7 +37,7 @@ func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { columns.FgColor = gocui.ColorBlack | gocui.AttrBold displayChannelsColumns(columns) - c.View, err = g.SetView(CHANNELS, x0-1, y0+1, x1+2, y1) + v, err := g.SetView(CHANNELS, x0-1, y0+1, x1+2, y1) if err != nil { if err != gocui.ErrUnknownView { return err @@ -47,13 +47,13 @@ func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { return err } } - c.View.Frame = false - c.View.Autoscroll = true - c.View.SelBgColor = gocui.ColorCyan - c.View.SelFgColor = gocui.ColorBlack - c.Highlight = true + v.Frame = false + v.Autoscroll = true + v.SelBgColor = gocui.ColorCyan + v.SelFgColor = gocui.ColorBlack + v.Highlight = true - c.display() + c.display(v) return nil } @@ -67,8 +67,8 @@ func displayChannelsColumns(v *gocui.View) { )) } -func (c *Channels) display() { - c.Clear() +func (c *Channels) display(v *gocui.View) { + v.Clear() for _, item := range c.channels.Items { line := fmt.Sprintf("%s %s %s %12d %5d %500s", active(item), @@ -78,7 +78,7 @@ func (c *Channels) display() { len(item.PendingHTLC), "", ) - fmt.Fprintln(c.View, line) + fmt.Fprintln(v, line) } } @@ -105,3 +105,36 @@ func gauge(c *netmodels.Channel) string { func NewChannels(channels *models.Channels) *Channels { return &Channels{channels: channels} } + +type Channel struct { + channel *models.Channel +} + +func (c Channel) Name() string { + return CHANNEL +} + +func (c Channel) Empty() bool { + return c.channel == nil +} + +func (c *Channel) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { + v, err := g.SetView(CHANNEL, x0-1, y0, x1+2, y1) + if err != nil { + if err != gocui.ErrUnknownView { + return err + } + } + c.display(v) + return nil +} + +func (c *Channel) display(v *gocui.View) { + v.Clear() + fmt.Fprintln(v, fmt.Sprintf("%s %d", + color.Cyan("ID:"), c.channel.Item.ID)) +} + +func NewChannel(channel *models.Channel) *Channel { + return &Channel{channel: channel} +} diff --git a/ui/views/views.go b/ui/views/views.go index 12b20b6..b6dd4f9 100644 --- a/ui/views/views.go +++ b/ui/views/views.go @@ -16,6 +16,7 @@ type Views struct { Header *Header Summary *Summary Channels *Channels + Channel *Channel Footer *Footer } @@ -60,5 +61,6 @@ func New(m *models.Models) *Views { Help: NewHelp(), Summary: NewSummary(m.Info, m.ChannelsBalance, m.WalletBalance, m.Channels), Channels: NewChannels(m.Channels), + Channel: NewChannel(m.CurrentChannel), } }