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/views/channels.go

104 lines
2.1 KiB
Go

5 years ago
package views
import (
5 years ago
"bytes"
"fmt"
"github.com/jroimartin/gocui"
netmodels "github.com/edouardparis/lntop/network/models"
"github.com/edouardparis/lntop/ui/color"
"github.com/edouardparis/lntop/ui/models"
)
const (
CHANNELS = "channels"
CHANNELS_COLUMNS = "channels_columns"
)
5 years ago
type Channels struct {
*gocui.View
channels *models.Channels
}
func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
columns, err := g.SetView(CHANNELS_COLUMNS, x0-1, y0, x1+2, y0+2)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
columns.Frame = false
columns.BgColor = gocui.ColorGreen
columns.FgColor = gocui.ColorBlack | gocui.AttrBold
displayChannelsColumns(columns)
c.View, err = g.SetView(CHANNELS, x0-1, y0+1, x1+2, y1)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
5 years ago
_, err = g.SetCurrentView(CHANNELS)
if err != nil {
return err
}
}
c.View.Frame = false
c.View.Autoscroll = true
c.View.SelBgColor = gocui.ColorCyan
c.View.SelFgColor = gocui.ColorBlack
5 years ago
c.Highlight = true
c.display()
return nil
}
func displayChannelsColumns(v *gocui.View) {
5 years ago
fmt.Fprintln(v, fmt.Sprintf("%-9s %-26s %12s %12s %5s",
5 years ago
"Status",
"Gauge",
5 years ago
"Local",
"Capacity",
5 years ago
"pHTLC",
5 years ago
))
}
5 years ago
func (c *Channels) display() {
5 years ago
c.Clear()
for _, item := range c.channels.Items {
line := fmt.Sprintf("%s %s %s %12d %5d %500s",
active(item),
gauge(item),
5 years ago
color.Cyan(fmt.Sprintf("%12d", item.LocalBalance)),
5 years ago
item.Capacity,
5 years ago
len(item.PendingHTLC),
"",
5 years ago
)
fmt.Fprintln(c.View, line)
}
}
5 years ago
func active(c *netmodels.Channel) string {
if c.Active {
5 years ago
return color.Green(fmt.Sprintf("%-9s", "active"))
}
5 years ago
return color.Red(fmt.Sprintf("%-9s", "inactive"))
}
func gauge(c *netmodels.Channel) string {
index := int(c.LocalBalance * int64(20) / c.Capacity)
var buffer bytes.Buffer
for i := 0; i < 20; i++ {
if i < index {
buffer.WriteString(color.Cyan("|"))
continue
}
buffer.WriteString(" ")
}
5 years ago
return fmt.Sprintf("[%s] %2d%%", buffer.String(), c.LocalBalance*100/c.Capacity)
}
func NewChannels(channels *models.Channels) *Channels {
return &Channels{channels: channels}
5 years ago
}