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

112 lines
2.8 KiB
Go
Raw Normal View History

2019-03-23 13:37:17 +00:00
package views
import (
2019-03-29 11:52:49 +00:00
"bytes"
2019-03-23 13:37:17 +00:00
"fmt"
"github.com/jroimartin/gocui"
"golang.org/x/text/language"
"golang.org/x/text/message"
2019-03-29 11:52:49 +00:00
netmodels "github.com/edouardparis/lntop/network/models"
2019-03-23 13:37:17 +00:00
"github.com/edouardparis/lntop/ui/color"
2019-03-29 08:37:24 +00:00
"github.com/edouardparis/lntop/ui/models"
2019-03-23 13:37:17 +00:00
)
const (
SUMMARY_LEFT = "summary_left"
SUMMARY_RIGHT = "summary_right"
)
type Summary struct {
2019-03-29 11:38:09 +00:00
left *gocui.View
right *gocui.View
info *models.Info
channelsBalance *models.ChannelsBalance
walletBalance *models.WalletBalance
2019-03-29 11:52:49 +00:00
channels *models.Channels
2019-03-23 13:37:17 +00:00
}
func (s *Summary) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
var err error
2019-03-29 11:38:09 +00:00
s.left, err = g.SetView(SUMMARY_LEFT, x0, y0, x1/2, y1)
2019-03-23 13:37:17 +00:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
s.left.Frame = false
2019-03-29 11:52:49 +00:00
s.left.Wrap = true
2019-03-23 13:37:17 +00:00
2019-03-29 11:38:09 +00:00
s.right, err = g.SetView(SUMMARY_RIGHT, x1/2, y0, x1, y1)
2019-03-23 13:37:17 +00:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
s.right.Frame = false
2019-03-29 11:52:49 +00:00
s.right.Wrap = true
2019-03-23 13:37:17 +00:00
s.display()
return nil
}
func (s *Summary) display() {
s.left.Clear()
p := message.NewPrinter(language.English)
2019-03-23 13:37:17 +00:00
fmt.Fprintln(s.left, color.Green("[ Channels ]"))
fmt.Fprintln(s.left, p.Sprintf("%s %d (%s|%s)",
2019-03-29 11:38:09 +00:00
color.Cyan("balance:"),
s.channelsBalance.Balance+s.channelsBalance.PendingOpenBalance,
color.Green(p.Sprintf("%d", s.channelsBalance.Balance)),
color.Yellow(p.Sprintf("%d", s.channelsBalance.PendingOpenBalance)),
2019-03-29 11:38:09 +00:00
))
fmt.Fprintln(s.left, fmt.Sprintf("%s %d %s %d %s %d %s",
color.Cyan("state :"),
2019-03-29 08:37:24 +00:00
s.info.NumActiveChannels, color.Green("active"),
2019-03-29 11:38:09 +00:00
s.info.NumPendingChannels, color.Yellow("pending"),
2019-03-29 08:37:24 +00:00
s.info.NumInactiveChannels, color.Red("inactive"),
2019-03-25 17:29:25 +00:00
))
2019-03-29 11:52:49 +00:00
fmt.Fprintln(s.left, fmt.Sprintf("%s %s",
color.Cyan("gauge :"),
2019-04-03 12:11:49 +00:00
gaugeTotal(s.channelsBalance.Balance, s.channels.List()),
2019-03-29 11:52:49 +00:00
))
2019-03-23 13:37:17 +00:00
s.right.Clear()
2019-03-29 11:38:09 +00:00
fmt.Fprintln(s.right, color.Green("[ Wallet ]"))
fmt.Fprintln(s.right, p.Sprintf("%s %d (%s|%s)",
2019-03-29 11:38:09 +00:00
color.Cyan("balance:"),
s.walletBalance.TotalBalance,
color.Green(p.Sprintf("%d", s.walletBalance.ConfirmedBalance)),
color.Yellow(p.Sprintf("%d", s.walletBalance.UnconfirmedBalance)),
2019-03-29 11:38:09 +00:00
))
2019-03-23 13:37:17 +00:00
}
2019-03-29 11:52:49 +00:00
func gaugeTotal(balance int64, channels []*netmodels.Channel) string {
capacity := int64(0)
for i := range channels {
capacity += channels[i].Capacity
}
index := int(balance * int64(20) / 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(), balance*100/capacity)
}
func NewSummary(info *models.Info,
channelsBalance *models.ChannelsBalance,
walletBalance *models.WalletBalance,
channels *models.Channels) *Summary {
2019-03-29 11:38:09 +00:00
return &Summary{
info: info,
channelsBalance: channelsBalance,
walletBalance: walletBalance,
2019-03-29 11:52:49 +00:00
channels: channels,
2019-03-29 11:38:09 +00:00
}
2019-03-23 13:37:17 +00:00
}