-
Notifications
You must be signed in to change notification settings - Fork 2
/
peerlistview.go
141 lines (107 loc) · 3.51 KB
/
peerlistview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"log"
"github.com/jroimartin/gocui"
)
type peerListView struct {
viewBase
form *formEdit
}
type connectPeer struct {
PubKey string `displayname:"Pub key" length:"40" lines:"2"`
Host string `displayname:"Host" length:"16"`
Port int `displayname:"Port" length:"5"`
}
type disconnectPeer struct {
NodeAlias string `displayname:"Node alias" length:"32" readonly:"1"`
PubKey string `displayname:"Pub key" length:"32" readonly:"1" lines:"3"`
}
func newpeerListView(physicalView string, fmtnormal string, fmtheader string, fmtselected string) *peerListView {
cv := new(peerListView)
cv.grid = makeNewDataGrid()
cv.mappedToPhysicalView = physicalView
cv.init(fmtnormal, fmtheader, fmtselected)
return cv
}
func (cv *peerListView) init(fmtnormal string, fmtheader string, fmtselected string) {
cv.grid.fmtForeground = fmtnormal
cv.grid.fmtHeader = fmtheader
cv.grid.fmtSelected = fmtselected
cv.shortcuts = nil
cv.shortcuts = append(cv.shortcuts, &keyHandle{"Scroll Up", "Up", gocui.KeyArrowUp, gocui.ModNone, func() { cv.grid.moveSelectionUp() }, false, ""})
cv.shortcuts = append(cv.shortcuts, &keyHandle{"Scroll Down", "Down", gocui.KeyArrowDown, gocui.ModNone, func() { cv.grid.moveSelectionDown() }, false, ""})
cv.shortcuts = append(cv.shortcuts, &keyHandle{"Connect", "C", 'c', gocui.ModAlt, cv.connect, true, ""})
cv.shortcuts = append(cv.shortcuts, &keyHandle{"Disconnect", "D", 'd', gocui.ModAlt, cv.disconnect, true, ""})
cv.grid.key = "peers"
cv.grid.addColumn("Alias", "Alias", stringRow) // "Alias", 0
cv.grid.addColumn("Address", "Address", stringRow) //"Address", 22
cv.grid.addColumn("BytesSent", "BytesSent", intRow) //"Bytes sent",13
cv.grid.addColumn("BytesRec", "BytesRecv", intRow) //"Bytes rec.",13
cv.grid.addColumn("SatSent", "SatSent", intRow) //"Sat sent", 12
cv.grid.addColumn("SatRec", "SatRecv", intRow) //"Sat rec.", 12
cv.grid.addColumn("Inbound", "Inbound", boolRow) //"Inbound",2
cv.grid.addColumn("Ping", "PingTime", intRow) //"Ping",6
cv.grid.initConfig()
}
func (cv *peerListView) connect() {
cc := new(connectPeer)
cc.Port = 9735
cv.form = newFormEdit("connectPeerVal", "Connect to peer", cc)
cv.form.callback = func(valid bool) {
cv.form.getValue()
if valid {
status.connectToPeer(&context, cc.PubKey, cc.Host, cc.Port)
updateData()
}
cv.form.close(context.gocui)
cv.form = nil
cc = nil
}
cv.form.initialize(context.gocui)
}
func (cv *peerListView) getSelectedPeer() *lncliPeer {
return cv.grid.getSelectedItem().Interface().(*lncliPeer)
}
func (cv *peerListView) disconnect() {
c := cv.getSelectedPeer()
cc := new(disconnectPeer)
cc.NodeAlias = c.Alias
cc.PubKey = c.PubKey
cv.form = newFormEdit("disconnectPeerForm", "Disconnect peer", cc)
cv.form.callback = func(valid bool) {
if valid {
status.disconnectPeer(&context, c)
updateData()
}
cv.form.close(context.gocui)
cv.form = nil
cc = nil
}
cv.form.initialize(context.gocui)
}
func (cv *peerListView) refreshView(g *gocui.Gui) {
v, err := g.View(cv.mappedToPhysicalView)
if err != nil {
log.Panicln(err.Error())
return
}
v.Clear()
x, y := v.Size()
cv.grid.setRenderSize(x, y)
for _, row := range cv.grid.getGridRows() {
fmt.Fprintln(v, row)
}
if cv.form != nil {
cv.form.layout(g)
}
}
func (cv *peerListView) getShortCuts() []*keyHandle {
return cv.shortcuts
}
func (cv *peerListView) getGrid() *dataGrid {
return cv.grid
}
func (cv *peerListView) getPhysicalView() string {
return cv.mappedToPhysicalView
}