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

104 lines
2.0 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"
"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
2019-03-21 09:08:33 +00:00
items []*models.Channel
2019-03-19 13:19:49 +00:00
}
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-22 09:14:58 +00:00
fmt.Fprintln(v, fmt.Sprintf("%-9s %-26s %12s %12s",
2019-03-21 09:08:33 +00:00
"Status",
2019-03-22 09:14:58 +00:00
"Gauge",
2019-03-21 09:08:33 +00:00
"Local",
"Capacity",
2019-03-20 12:44:30 +00:00
))
}
2019-03-21 09:08:33 +00:00
func (c *Channels) Update(items []*models.Channel) {
c.items = items
2019-03-19 13:19:49 +00:00
}
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-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,
)
fmt.Fprintln(c.View, line)
2019-03-19 13:19:49 +00:00
}
}
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"))
}
2019-03-22 09:14:58 +00:00
func gauge(c *models.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(" ")
}
return fmt.Sprintf("[%s %2d%%]", buffer.String(), c.LocalBalance*100/c.Capacity)
}
2019-03-21 09:08:33 +00:00
func NewChannels() *Channels {
return &Channels{}
}