2019-06-21 18:53:50 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
|
"log"
|
2019-06-21 19:10:06 +00:00
|
|
|
|
"strings"
|
2019-06-21 18:53:50 +00:00
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-26 23:54:04 +00:00
|
|
|
|
type PeersState struct {
|
|
|
|
|
c *client
|
|
|
|
|
list []Peer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewPeersState(host string, port int) *PeersState {
|
|
|
|
|
url := fmt.Sprintf("http://%s:%d", host, port)
|
|
|
|
|
c, err := newClient(url)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicln(err)
|
|
|
|
|
}
|
|
|
|
|
return &PeersState{c: c}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *PeersState) Fetch(g *gocui.Gui) {
|
2019-06-21 18:53:50 +00:00
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-threadDone:
|
|
|
|
|
return
|
|
|
|
|
default:
|
2019-06-26 23:54:04 +00:00
|
|
|
|
peers, err := p.c.getPeers()
|
2019-06-21 18:53:50 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicln(err)
|
|
|
|
|
}
|
2019-06-26 23:54:04 +00:00
|
|
|
|
p.list = peers
|
|
|
|
|
writePeers(g, peers)
|
2019-06-21 18:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
<-time.After(interval * time.Second)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 23:54:04 +00:00
|
|
|
|
func writePeers(g *gocui.Gui, peers []Peer) {
|
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-21 20:47:37 +00:00
|
|
|
|
func boolToString(v bool, yes string, no string) string {
|
|
|
|
|
if v {
|
|
|
|
|
return yes
|
|
|
|
|
} else {
|
|
|
|
|
return no
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-21 19:10:06 +00:00
|
|
|
|
id = string(p.Id)
|
|
|
|
|
} else {
|
|
|
|
|
id = p.Id.String()
|
|
|
|
|
}
|
2019-06-21 20:47:37 +00:00
|
|
|
|
return fmt.Sprintf("%s| %-15s| %-21s| %-7s| %-8s| %s",
|
|
|
|
|
id, p.Name,
|
|
|
|
|
p.Network.RemoteAddress,
|
|
|
|
|
boolToString(p.Network.Trusted, "trusted", "normal"),
|
|
|
|
|
boolToString(p.Network.Static, "static", "dynamic"),
|
|
|
|
|
strings.Join(p.Caps, ", "))
|
2019-06-21 19:10:06 +00:00
|
|
|
|
}
|