forked from mit-dci/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.go
119 lines (112 loc) · 2.95 KB
/
autocomplete.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
package main
import (
"fmt"
"github.com/chzyer/readline"
"github.com/mit-dci/lit/litrpc"
)
func (lc *litAfClient) completePeers(line string) []string {
names := make([]string, 0)
pReply := new(litrpc.ListConnectionsReply)
err := lc.Call("LitRPC.ListConnections", nil, pReply)
if err != nil {
return names
}
if len(pReply.Connections) > 0 {
for _, peer := range pReply.Connections {
var peerStr = fmt.Sprint(peer.PeerNumber)
names = append(names, peerStr)
}
}
return names
}
func (lc *litAfClient) completeClosedPeers(line string) []string {
channelpeers := make([]string, 0)
connectedpeers := make([]string, 0)
pReply := new(litrpc.ListConnectionsReply)
cReply := new(litrpc.ChannelListReply)
err := lc.Call("LitRPC.ListConnections", nil, pReply)
if err != nil {
return channelpeers
}
err = lc.Call("LitRPC.ChannelList", nil, cReply)
if err != nil {
return channelpeers
}
if len(pReply.Connections) > 0 {
for _, peer := range pReply.Connections {
var peerStr = fmt.Sprint(peer.PeerNumber)
connectedpeers = append(connectedpeers, peerStr)
}
}
for _, c := range cReply.Channels {
var peerid = fmt.Sprint(c.PeerIdx)
var found = false
for _, v := range append(connectedpeers, channelpeers...) {
if v == peerid {
found = true
break
}
}
if !found {
channelpeers = append(channelpeers, peerid)
}
}
return channelpeers
}
func (lc *litAfClient) completeChannelIdx(line string) []string {
names := make([]string, 0)
cReply := new(litrpc.ChannelListReply)
err := lc.Call("LitRPC.ChannelList", nil, cReply)
if err != nil {
return names
}
for _, c := range cReply.Channels {
if !c.Closed {
var cidxStr = fmt.Sprint(c.CIdx)
names = append(names, cidxStr)
}
}
return names
}
func (lc *litAfClient) NewAutoCompleter() readline.AutoCompleter {
var completer = readline.NewPrefixCompleter(
readline.PcItem("help",
readline.PcItem("say"),
readline.PcItem("ls"),
readline.PcItem("con"),
readline.PcItem("lis"),
readline.PcItem("adr"),
readline.PcItem("send"),
readline.PcItem("fan"),
readline.PcItem("sweep"),
readline.PcItem("fund"),
readline.PcItem("dualfund"),
readline.PcItem("push"),
readline.PcItem("close"),
readline.PcItem("break"),
readline.PcItem("stop"),
readline.PcItem("exit"),
),
readline.PcItem("say",
readline.PcItemDynamic(lc.completePeers)),
readline.PcItem("ls"),
readline.PcItem("con",
readline.PcItemDynamic(lc.completeClosedPeers)),
readline.PcItem("lis"),
readline.PcItem("adr"),
readline.PcItem("send"),
readline.PcItem("fan"),
readline.PcItem("sweep"),
readline.PcItem("fund",
readline.PcItemDynamic(lc.completePeers)),
readline.PcItem("push",
readline.PcItemDynamic(lc.completeChannelIdx)),
readline.PcItem("close",
readline.PcItemDynamic(lc.completeChannelIdx)),
readline.PcItem("break",
readline.PcItemDynamic(lc.completeChannelIdx)),
readline.PcItem("stop"),
readline.PcItem("exit"),
)
return completer
}