2
0
mirror of https://github.com/edouardparis/lntop synced 2024-11-16 00:12:44 +00:00
lntop/ui/views/channels.go

198 lines
4.2 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
"fmt"
"github.com/jroimartin/gocui"
2019-03-29 08:30:05 +00:00
netmodels "github.com/edouardparis/lntop/network/models"
2019-03-20 09:04:17 +00:00
"github.com/edouardparis/lntop/ui/color"
2019-03-29 08:30:05 +00:00
"github.com/edouardparis/lntop/ui/models"
2019-03-19 13:19:49 +00:00
)
2019-03-20 09:04:17 +00:00
const (
2019-04-02 12:27:51 +00:00
CHANNEL = "channel"
2019-04-03 12:11:49 +00:00
CHANNEL_HEADER = "channel_header"
2019-03-22 15:52:12 +00:00
CHANNELS = "channels"
CHANNELS_COLUMNS = "channels_columns"
2019-03-20 09:04:17 +00:00
)
2019-03-19 13:19:49 +00:00
2019-03-19 08:54:45 +00:00
type Channels struct {
2019-03-29 08:30:05 +00:00
channels *models.Channels
2019-03-19 13:19:49 +00:00
}
2019-04-02 08:14:39 +00:00
func (c Channels) Name() string {
return CHANNELS
}
2019-03-19 13:19:49 +00:00
func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
2019-03-26 08:40:43 +00:00
columns, err := g.SetView(CHANNELS_COLUMNS, x0-1, y0, x1+2, y0+2)
2019-03-20 09:04:17 +00:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
2019-03-22 15:52:12 +00:00
columns.Frame = false
columns.BgColor = gocui.ColorGreen
columns.FgColor = gocui.ColorBlack | gocui.AttrBold
displayChannelsColumns(columns)
2019-03-20 09:04:17 +00:00
2019-04-02 12:27:51 +00:00
v, err := g.SetView(CHANNELS, x0-1, y0+1, x1+2, 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
}
2019-04-02 12:27:51 +00:00
v.Frame = false
v.Autoscroll = true
v.SelBgColor = gocui.ColorCyan
v.SelFgColor = gocui.ColorBlack
v.Highlight = true
2019-03-19 13:19:49 +00:00
2019-04-02 12:27:51 +00:00
c.display(v)
2019-03-19 13:19:49 +00:00
return nil
}
2019-03-22 15:52:12 +00:00
func displayChannelsColumns(v *gocui.View) {
2019-04-03 12:11:49 +00:00
v.Clear()
2019-04-04 11:24:01 +00:00
fmt.Fprintln(v, fmt.Sprintf("%-9s %-20s %-26s %12s %12s %5s %-15s %s",
2019-03-21 09:08:33 +00:00
"Status",
2019-04-04 11:24:01 +00:00
"Alias",
2019-03-22 09:14:58 +00:00
"Gauge",
2019-03-21 09:08:33 +00:00
"Local",
"Capacity",
2019-03-22 16:20:41 +00:00
"pHTLC",
2019-04-04 08:10:33 +00:00
"Last Update",
2019-04-04 11:24:01 +00:00
"ID",
2019-03-20 12:44:30 +00:00
))
}
2019-04-02 12:27:51 +00:00
func (c *Channels) display(v *gocui.View) {
v.Clear()
2019-04-03 12:11:49 +00:00
for _, item := range c.channels.List() {
2019-04-04 11:24:01 +00:00
line := fmt.Sprintf("%s %-20s %s %s %12d %5d %15s %d %500s",
2019-03-20 09:04:17 +00:00
active(item),
2019-04-04 11:24:01 +00:00
alias(item),
2019-03-22 09:14:58 +00:00
gauge(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,
2019-03-22 16:20:41 +00:00
len(item.PendingHTLC),
2019-04-04 08:10:33 +00:00
lastUpdate(item),
2019-04-04 11:24:01 +00:00
item.ID,
2019-03-25 17:29:25 +00:00
"",
2019-03-19 16:20:36 +00:00
)
2019-04-02 12:27:51 +00:00
fmt.Fprintln(v, line)
2019-03-19 13:19:49 +00:00
}
}
2019-03-19 08:54:45 +00:00
2019-04-04 11:24:01 +00:00
func alias(c *netmodels.Channel) string {
if c.Node == nil || c.Node.Alias == "" {
return c.RemotePubKey[:19]
}
return c.Node.Alias
}
2019-04-04 08:10:33 +00:00
func lastUpdate(c *netmodels.Channel) string {
if c.LastUpdate != nil {
return c.LastUpdate.Format("15:04:05 Jan _2")
}
return ""
}
2019-03-29 08:30:05 +00:00
func active(c *netmodels.Channel) string {
2019-03-20 09:04:17 +00:00
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"))
}
2019-03-29 08:30:05 +00:00
func gauge(c *netmodels.Channel) string {
2019-03-22 09:14:58 +00:00
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(" ")
}
2019-03-22 14:00:14 +00:00
return fmt.Sprintf("[%s] %2d%%", buffer.String(), c.LocalBalance*100/c.Capacity)
2019-03-22 09:14:58 +00:00
}
2019-03-29 08:30:05 +00:00
func NewChannels(channels *models.Channels) *Channels {
return &Channels{channels: channels}
2019-03-21 09:08:33 +00:00
}
2019-04-02 12:27:51 +00:00
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 {
2019-04-03 12:11:49 +00:00
header, err := g.SetView(CHANNEL_HEADER, x0-1, y0, x1+2, y0+2)
2019-04-02 12:27:51 +00:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
2019-04-03 08:12:36 +00:00
header.Frame = false
header.BgColor = gocui.ColorGreen
header.FgColor = gocui.ColorBlack | gocui.AttrBold
header.Clear()
fmt.Fprintln(header, "Channel")
v, err := g.SetView(CHANNEL, x0-1, y0+1, x1+2, y1)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
v.Frame = false
2019-04-02 12:27:51 +00:00
c.display(v)
return nil
}
2019-04-03 12:11:49 +00:00
func (c Channel) Delete(g *gocui.Gui) error {
err := g.DeleteView(CHANNEL_HEADER)
if err != nil {
return err
}
return g.DeleteView(CHANNEL)
}
2019-04-02 12:27:51 +00:00
func (c *Channel) display(v *gocui.View) {
v.Clear()
2019-04-03 08:12:36 +00:00
channel := c.channel.Item
fmt.Fprintln(v, active(channel))
fmt.Fprintln(v, fmt.Sprintf("%s %d",
color.Cyan(" ID:"), channel.ID))
fmt.Fprintln(v, fmt.Sprintf("%s %d",
color.Cyan(" Capacity:"), channel.Capacity))
fmt.Fprintln(v, fmt.Sprintf("%s %d",
color.Cyan(" Local Balance:"), channel.LocalBalance))
2019-04-02 12:27:51 +00:00
fmt.Fprintln(v, fmt.Sprintf("%s %d",
2019-04-03 08:12:36 +00:00
color.Cyan(" Remote Balance:"), channel.RemoteBalance))
fmt.Fprintln(v, fmt.Sprintf("%s %s",
color.Cyan(" Remote PubKey:"), channel.RemotePubKey))
fmt.Fprintln(v, fmt.Sprintf("%s %s",
color.Cyan(" Channel Point:"), channel.ChannelPoint))
2019-04-02 12:27:51 +00:00
}
func NewChannel(channel *models.Channel) *Channel {
return &Channel{channel: channel}
}