2019-06-21 18:53:50 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2019-06-27 19:34:34 +00:00
|
|
|
|
"log"
|
2019-06-21 19:10:06 +00:00
|
|
|
|
"strings"
|
2019-06-26 23:54:04 +00:00
|
|
|
|
|
2019-06-27 18:21:51 +00:00
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
|
)
|
2019-06-26 23:54:04 +00:00
|
|
|
|
|
2019-07-02 20:07:12 +00:00
|
|
|
|
func GenRenderFunc(g *gocui.Gui, state *AppState) func() {
|
2019-06-27 18:21:51 +00:00
|
|
|
|
return func() {
|
|
|
|
|
ps := state.GetState()
|
2019-06-27 19:34:34 +00:00
|
|
|
|
renderPeerList(g, ps.Peers)
|
2019-06-27 19:09:02 +00:00
|
|
|
|
renderPeerInfo(g, state.GetCurrent())
|
2019-06-27 19:34:34 +00:00
|
|
|
|
updatePeerCursor(g, ps.Current)
|
2019-06-21 18:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 19:34:34 +00:00
|
|
|
|
func renderPeerList(g *gocui.Gui, peers []Peer) {
|
2019-06-27 18:21:51 +00:00
|
|
|
|
if len(peers) == 0 {
|
|
|
|
|
return
|
2019-06-27 00:23:36 +00:00
|
|
|
|
}
|
2019-06-21 18:53:50 +00:00
|
|
|
|
g.Update(func(g *gocui.Gui) error {
|
|
|
|
|
v, err := g.View("main")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
v.Clear()
|
|
|
|
|
maxWidth, _ := g.Size()
|
|
|
|
|
for _, peer := range peers {
|
|
|
|
|
fmt.Fprintf(v, "%s\n", peer.AsTable(maxWidth))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-06-21 19:10:06 +00:00
|
|
|
|
|
2019-06-27 18:21:51 +00:00
|
|
|
|
func renderPeerInfo(g *gocui.Gui, peer *Peer) {
|
|
|
|
|
if peer == nil {
|
|
|
|
|
return
|
2019-06-27 13:14:28 +00:00
|
|
|
|
}
|
2019-06-27 02:31:40 +00:00
|
|
|
|
g.Update(func(g *gocui.Gui) error {
|
|
|
|
|
v, err := g.View("info")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
v.Clear()
|
2019-06-27 03:11:52 +00:00
|
|
|
|
fmt.Fprintf(v, strings.Repeat("%-8s: %v\n", 8),
|
2019-06-27 02:46:19 +00:00
|
|
|
|
"Name", peer.Name,
|
2019-06-28 20:17:52 +00:00
|
|
|
|
"ID", string(peer.ID),
|
2019-06-27 02:46:19 +00:00
|
|
|
|
"Enode", peer.Enode,
|
2019-06-27 03:09:09 +00:00
|
|
|
|
"Static", peer.Network.Static,
|
2019-06-27 03:11:52 +00:00
|
|
|
|
"Trusted", peer.Network.Trusted,
|
2019-06-27 02:48:21 +00:00
|
|
|
|
"Local", peer.Network.LocalAddress,
|
|
|
|
|
"Remote", peer.Network.RemoteAddress,
|
2019-06-27 02:46:19 +00:00
|
|
|
|
"Caps", strings.Join(peer.Caps, ", "))
|
2019-06-27 02:31:40 +00:00
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 19:34:34 +00:00
|
|
|
|
func updatePeerCursor(g *gocui.Gui, current int) {
|
|
|
|
|
v, err := g.View("main")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicln("unable to find main view")
|
|
|
|
|
}
|
|
|
|
|
cx, _ := v.Cursor()
|
|
|
|
|
|
|
|
|
|
if err := v.SetCursor(cx, current); err != nil {
|
|
|
|
|
ox, _ := v.Origin()
|
|
|
|
|
if err := v.SetOrigin(ox, current); err != nil {
|
|
|
|
|
log.Panicln("unable to scroll")
|
|
|
|
|
}
|
2019-06-21 20:47:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 19:10:06 +00:00
|
|
|
|
func (p Peer) AsTable(maxWidth int) string {
|
|
|
|
|
var id string
|
2019-06-21 20:47:37 +00:00
|
|
|
|
if maxWidth > 160 {
|
2019-06-28 20:17:52 +00:00
|
|
|
|
id = string(p.ID)
|
2019-06-21 19:10:06 +00:00
|
|
|
|
} else {
|
2019-06-28 20:17:52 +00:00
|
|
|
|
id = p.ID.String()
|
2019-06-21 19:10:06 +00:00
|
|
|
|
}
|
2019-06-27 03:07:08 +00:00
|
|
|
|
return fmt.Sprintf("%s | %-15s | %-21s | %-7s | %-8s",
|
2019-06-21 20:47:37 +00:00
|
|
|
|
id, p.Name,
|
|
|
|
|
p.Network.RemoteAddress,
|
|
|
|
|
boolToString(p.Network.Trusted, "trusted", "normal"),
|
2019-06-27 02:31:40 +00:00
|
|
|
|
boolToString(p.Network.Static, "static", "dynamic"))
|
2019-06-21 19:10:06 +00:00
|
|
|
|
}
|
2019-06-27 19:34:34 +00:00
|
|
|
|
|
|
|
|
|
func boolToString(v bool, yes string, no string) string {
|
|
|
|
|
if v {
|
|
|
|
|
return yes
|
|
|
|
|
} else {
|
|
|
|
|
return no
|
|
|
|
|
}
|
|
|
|
|
}
|