ft channel view

pull/1/head
Edouard Paris 5 years ago
parent 868b2519ca
commit 78c41c0741

@ -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

@ -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
}

@ -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
}

@ -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}
}

@ -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),
}
}

Loading…
Cancel
Save