/
gui.go
280 lines (246 loc) · 5.55 KB
/
gui.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"fmt"
"io/ioutil"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/jroimartin/gocui"
)
type GuiClient struct {
sync.Mutex
pki *PKI
myName string
myPublicKey *BoxKey
myPrivateKey *BoxKey
gui *gocui.Gui
client *Client
selectedConvo *Conversation
conversations map[string]*Conversation
dialer *Dialer
}
func (gc *GuiClient) switchConversation(peer string) {
var convo *Conversation
convo, ok := gc.conversations[peer]
if !ok {
peerPublicKey, ok := gc.pki.People[peer]
if !ok {
gc.Warnf("unknown user: %s", peer)
return
}
convo = &Conversation{
pki: gc.pki,
peerName: peer,
peerPublicKey: peerPublicKey,
myPublicKey: gc.myPublicKey,
myPrivateKey: gc.myPrivateKey,
gui: gc,
}
convo.Init()
gc.conversations[peer] = convo
}
gc.selectedConvo = convo
gc.activateConvo(convo)
gc.Warnf("Now talking to %s\n", peer)
}
func (gc *GuiClient) activateConvo(convo *Conversation) {
if gc.client != nil {
convo.Lock()
convo.lastPeerResponding = false
convo.lastLatency = 0
convo.Unlock()
gc.client.SetConvoHandler(convo)
}
}
func (gc *GuiClient) handleLine(line string) error {
switch {
case line == "/quit":
return gocui.Quit
case strings.HasPrefix(line, "/talk "):
peer := line[6:]
gc.switchConversation(peer)
case strings.HasPrefix(line, "/dial "):
peer := line[6:]
pk, ok := gc.pki.People[peer]
if !ok {
gc.Warnf("Unknown user: %q (see %s)\n", peer, *pkiPath)
return nil
}
gc.Warnf("Dialing user: %s\n", peer)
gc.dialer.QueueRequest(pk)
default:
msg := strings.TrimSpace(line)
gc.selectedConvo.QueueTextMessage([]byte(msg))
gc.Printf("<%s> %s\n", gc.myName, msg)
}
return nil
}
func (gc *GuiClient) readLine(_ *gocui.Gui, v *gocui.View) error {
// HACK: pressing enter on startup causes panic
if len(v.Buffer()) == 0 {
return nil
}
_, cy := v.Cursor()
line, err := v.Line(cy - 1)
if err != nil {
return err
}
if line == "" {
return nil
}
v.Clear()
return gc.handleLine(line)
}
func (gc *GuiClient) Flush() {
gc.gui.Flush()
}
func (gc *GuiClient) Warnf(format string, v ...interface{}) {
mv, err := gc.gui.View("main")
if err != nil {
return
}
fmt.Fprintf(mv, "-!- "+format, v...)
gc.gui.Flush()
}
func (gc *GuiClient) Printf(format string, v ...interface{}) {
mv, err := gc.gui.View("main")
if err != nil {
return
}
fmt.Fprintf(mv, format, v...)
gc.gui.Flush()
}
func (gc *GuiClient) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("main", 0, -1, maxX-1, maxY-1); err != nil {
if err != gocui.ErrorUnkView {
return err
}
v.Autoscroll = true
v.Wrap = true
v.Frame = false
log.AddHook(gc)
log.SetOutput(ioutil.Discard)
log.SetFormatter(&GuiFormatter{})
}
sv, err := g.SetView("status", -1, maxY-3, maxX, maxY-1)
if err != nil {
if err != gocui.ErrorUnkView {
return err
}
sv.Wrap = false
sv.Frame = false
sv.BgColor = gocui.ColorBlue
sv.FgColor = gocui.ColorWhite
}
sv.Clear()
st := gc.selectedConvo.Status()
latency := fmt.Sprintf("%.1fs", st.Latency)
if st.Latency == 0.0 {
latency = "-"
}
round := fmt.Sprintf("%d", st.Round)
if st.Round == 0 {
round = "-"
}
fmt.Fprintf(sv, " [%s] [round: %s] [latency: %s]", gc.myName, round, latency)
partner := "(no partner)"
if !gc.selectedConvo.Solo() {
partner = gc.selectedConvo.peerName
}
pv, err := g.SetView("partner", -1, maxY-2, len(partner)+1, maxY)
if err != nil {
if err != gocui.ErrorUnkView {
return err
}
pv.Wrap = false
pv.Frame = false
}
pv.Clear()
if st.PeerResponding {
pv.FgColor = gocui.ColorGreen
} else {
pv.FgColor = gocui.ColorRed
}
fmt.Fprintf(pv, "%s>", partner)
if v, err := g.SetView("input", len(partner)+1, maxY-2, maxX, maxY); err != nil {
if err != gocui.ErrorUnkView {
return err
}
v.Editable = true
v.Wrap = false
v.Frame = false
if err := g.SetCurrentView("input"); err != nil {
return err
}
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.Quit
}
func (gc *GuiClient) Connect() error {
if gc.client == nil {
gc.client = NewClient(gc.pki.EntryServer, gc.myPublicKey)
gc.client.SetDialHandler(gc.dialer)
}
gc.activateConvo(gc.selectedConvo)
return gc.client.Connect()
}
func (gc *GuiClient) Run() {
gui := gocui.NewGui()
if err := gui.Init(); err != nil {
log.Panicln(err)
}
defer gui.Close()
gc.gui = gui
if err := gui.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := gui.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, gc.readLine); err != nil {
log.Panicln(err)
}
gui.ShowCursor = true
gui.BgColor = gocui.ColorDefault
gui.FgColor = gocui.ColorDefault
gui.SetLayout(gc.layout)
gc.conversations = make(map[string]*Conversation)
gc.switchConversation(gc.myName)
gc.dialer = &Dialer{
gui: gc,
pki: gc.pki,
myPublicKey: gc.myPublicKey,
myPrivateKey: gc.myPrivateKey,
}
gc.dialer.Init()
go func() {
time.Sleep(500 * time.Millisecond)
if err := gc.Connect(); err != nil {
gc.Warnf("Failed to connect: %s\n", err)
}
gc.Warnf("Connected: %s\n", gc.pki.EntryServer)
}()
err := gui.MainLoop()
if err != nil && err != gocui.Quit {
log.Panicln(err)
}
}
func (gc *GuiClient) Fire(entry *log.Entry) error {
line, err := entry.String()
if err != nil {
return err
}
gc.Warnf(line)
return nil
}
func (gc *GuiClient) Levels() []log.Level {
return []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
log.WarnLevel,
log.InfoLevel,
log.DebugLevel,
}
}