2
0
mirror of https://github.com/edouardparis/lntop synced 2024-11-13 13:10:34 +00:00
lntop/ui/views/channels.go

127 lines
2.4 KiB
Go
Raw Normal View History

2019-03-19 08:54:45 +00:00
package views
2019-03-19 13:19:49 +00:00
import (
2019-03-20 12:44:30 +00:00
"bytes"
2019-03-19 13:19:49 +00:00
"context"
"fmt"
"github.com/jroimartin/gocui"
"github.com/edouardparis/lntop/network"
"github.com/edouardparis/lntop/network/models"
2019-03-20 09:04:17 +00:00
"github.com/edouardparis/lntop/ui/color"
2019-03-19 13:19:49 +00:00
)
2019-03-20 09:04:17 +00:00
const (
CHANNELS = "channels"
CHANNELS_HEADER = "header"
)
2019-03-19 13:19:49 +00:00
2019-03-19 08:54:45 +00:00
type Channels struct {
2019-03-19 13:19:49 +00:00
*gocui.View
items []*models.Channel
network *network.Network
}
func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
2019-03-20 09:04:17 +00:00
headerView, err := g.SetView(CHANNELS_HEADER, x0, y0, x1, y0+2)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
headerView.Frame = false
headerView.BgColor = gocui.ColorGreen
2019-03-20 12:44:30 +00:00
headerView.FgColor = gocui.ColorBlack | gocui.AttrBold
displayChannelsHeader(headerView)
2019-03-20 09:04:17 +00:00
c.View, err = g.SetView(CHANNELS, x0, y0+1, x1, y1)
2019-03-19 13:19:49 +00:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
2019-03-21 08:34:45 +00:00
_, err = g.SetCurrentView(CHANNELS)
if err != nil {
return err
}
2019-03-19 13:19:49 +00:00
}
c.View.Frame = false
2019-03-20 12:53:12 +00:00
c.View.Autoscroll = true
c.View.SelBgColor = gocui.ColorCyan
c.View.SelFgColor = gocui.ColorBlack
2019-03-21 08:34:45 +00:00
c.Highlight = true
2019-03-19 13:19:49 +00:00
2019-03-20 09:04:17 +00:00
c.display()
2019-03-19 13:19:49 +00:00
return nil
}
2019-03-20 12:44:30 +00:00
func displayChannelsHeader(v *gocui.View) {
2019-03-21 08:38:31 +00:00
fmt.Fprintln(v, fmt.Sprintf("%-9s %-19s %12s %12s",
2019-03-20 12:44:30 +00:00
"status",
2019-03-21 08:38:31 +00:00
"id",
2019-03-20 12:44:30 +00:00
"local",
"capacity",
))
}
2019-03-19 16:20:36 +00:00
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 {
2019-03-19 13:19:49 +00:00
channels, err := c.network.ListChannels(ctx)
if err != nil {
return err
}
c.items = channels
return nil
}
2019-03-19 16:20:36 +00:00
func (c *Channels) display() {
2019-03-21 08:34:45 +00:00
c.Clear()
2019-03-19 16:20:36 +00:00
for _, item := range c.items {
2019-03-21 08:38:31 +00:00
line := fmt.Sprintf("%s %s %s %12d",
2019-03-20 09:04:17 +00:00
active(item),
2019-03-21 08:38:31 +00:00
chartID(item),
2019-03-20 12:44:30 +00:00
color.Cyan(fmt.Sprintf("%12d", item.LocalBalance)),
2019-03-19 16:20:36 +00:00
item.Capacity,
)
fmt.Fprintln(c.View, line)
2019-03-19 13:19:49 +00:00
}
}
2019-03-19 08:54:45 +00:00
2019-03-19 13:19:49 +00:00
func NewChannels(network *network.Network) *Channels {
return &Channels{network: network}
2019-03-19 08:54:45 +00:00
}
2019-03-20 09:04:17 +00:00
func active(c *models.Channel) string {
if c.Active {
2019-03-20 12:44:30 +00:00
return color.Green(fmt.Sprintf("%-9s", "active"))
2019-03-20 09:04:17 +00:00
}
2019-03-20 12:44:30 +00:00
return color.Red(fmt.Sprintf("%-9s", "inactive"))
}
func chartID(c *models.Channel) string {
id := fmt.Sprintf("%-19d", c.ID)
index := int(c.LocalBalance * int64(len(id)) / c.Capacity)
var buffer bytes.Buffer
buffer.WriteString(color.Cyan(id[:index]))
buffer.WriteString(id[index:])
return buffer.String()
2019-03-20 09:04:17 +00:00
}