-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
237 lines (205 loc) · 5.73 KB
/
ui.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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/jroimartin/gocui"
peer "github.com/libp2p/go-libp2p-peer"
)
/*
TODO: Some sort of map of Error code to message
*/
type UIColor struct {
nameColor string
}
type ChatUI struct {
node *Node
colorMap map[string]*UIColor
colorRandomizer *rand.Rand
channelID string
clientPeer *Peer
hostPeerID peer.ID
isTurn bool
host *Peer
hostMA string
}
func NewChatUI(node *Node, channelID string, clientPeer *Peer, hostPeerID peer.ID, hostMA string) *ChatUI {
var seed = rand.NewSource(time.Now().UnixNano())
var randomizer = rand.New(seed)
return &ChatUI{
node: node,
colorMap: make(map[string]*UIColor),
colorRandomizer: randomizer,
channelID: channelID,
clientPeer: clientPeer,
host: &Peer{username: "*Channel*"},
hostPeerID: hostPeerID,
hostMA: hostMA}
}
func (ui *ChatUI) randomColor() string {
randColor := strconv.Itoa(ui.colorRandomizer.Intn(8))
bold := ui.colorRandomizer.Intn(2) == 0
colorString := "\u001b[3" + randColor
if bold {
colorString = colorString + ";1"
}
return colorString + "m"
}
// Layout creates chat UI
func (ui *ChatUI) Layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
g.Cursor = true
if messages, err := g.SetView("messages", 0, 0, maxX-1, maxY-5); err != nil {
if err != gocui.ErrUnknownView {
return err
}
messages.Title = ui.channelID + ": " + ui.hostMA
messages.Autoscroll = true
messages.Wrap = true
messagesView, _ := g.View("messages")
go ui.BroadcastPanel(g, messagesView, ui.channelID)
}
if input, err := g.SetView("input", 0, maxY-5, maxX-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
input.Title = ui.clientPeer.username + ": "
input.Autoscroll = false
input.Wrap = true
input.Editable = true
if _, err := g.SetCurrentView("input"); err != nil {
return err
}
}
return nil
}
func (ui *ChatUI) exit(g *gocui.Gui, v *gocui.View) error {
ui.node.LeaveChannel(ui.clientPeer, ui.hostPeerID, ui.channelID)
g.Close()
os.Exit(0)
return gocui.ErrQuit
}
func (ui *ChatUI) buildMessage(message *Message) string {
color := ui.randomColor()
return color + message.peer.username + ": \u001b[0m" + message.text
}
func (ui *ChatUI) sendMessage(g *gocui.Gui, v *gocui.View) error {
if ui.isTurn {
msg := strings.TrimSpace(v.Buffer())
req, _ := ui.node.SendMessage(ui.clientPeer, ui.hostPeerID, ui.channelID, msg)
var resp *Response
select {
case <-req.success:
// Clear input panel
g.Update(func(g *gocui.Gui) error {
v.Clear()
v.SetCursor(0, 0)
v.SetOrigin(0, 0)
return nil
})
break
case resp = <-req.fail:
ui.isTurn = false
errorName := ProtocolErrorName(resp.errorCode)
errorMsg := ui.clientPeer.username + " turn finished because " + errorName
ui.displayMessage(g, v, ui.buildChannelMessage(errorMsg))
break
case <-time.After(3 * time.Second):
// Timeout
ui.isTurn = false
ui.displayMessage(g, v, ui.buildChannelMessage("Message request timed out. "))
break
}
} else {
ui.displayMessage(g, v, ui.buildChannelMessage("Not your turn. "))
}
return nil
}
func (ui *ChatUI) requestTransmission(g *gocui.Gui, v *gocui.View) error {
if !ui.isTurn {
req, reqSent := ui.node.SendTransmissionRequest(ui.clientPeer, ui.hostPeerID, ui.channelID)
if reqSent {
select {
case <-req.success:
ui.isTurn = true
msg := ui.clientPeer.username + " turn strated "
ui.displayMessage(g, v, ui.buildChannelMessage(msg))
case <-req.fail:
ui.isTurn = false
msg := "Can't start turn, " + ui.clientPeer.username
ui.displayMessage(g, v, ui.buildChannelMessage(msg))
}
} else {
ui.isTurn = false
ui.node.debugPrintln("Transmission request not completed.")
}
}
return nil
}
func (ui *ChatUI) endTransmission(g *gocui.Gui, v *gocui.View) error {
if ui.isTurn {
ui.node.EndTransmission(ui.clientPeer, ui.hostPeerID, ui.channelID)
msg := ui.clientPeer.username + " turn ended."
ui.displayMessage(g, v, ui.buildChannelMessage(msg))
ui.isTurn = false
}
return nil
}
func (ui *ChatUI) StartUI() {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Fatal(err)
}
defer g.Close()
g.SetManagerFunc(ui.Layout)
g.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, ui.sendMessage)
g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, ui.exit)
g.SetKeybinding("", gocui.KeyHome, gocui.ModNone, ui.requestTransmission)
g.SetKeybinding("", gocui.KeyEnd, gocui.ModNone, ui.endTransmission)
g.MainLoop()
}
// Displays messages broadcasted to channel
func (ui *ChatUI) BroadcastPanel(g *gocui.Gui, v *gocui.View, channelID string) {
val, exists := ui.node.channels.Get(channelID)
if exists == false {
log.Println("Channel not found.")
return
}
channel := val.(*Channel)
var message *Message
for {
select {
case message = <-channel.output.messageQueue:
msgString := "From UI, message reqcieved " + message.text
ui.node.debugPrintln(msgString)
ui.displayMessage(g, v, message)
break
}
}
}
func (ui *ChatUI) buildChannelMessage(message string) *Message {
return &Message{
peer: ui.host,
timestamp: time.Now(),
text: message}
}
func (ui *ChatUI) displayMessage(g *gocui.Gui, v *gocui.View, message *Message) {
color, exists := ui.colorMap[message.peer.username]
if !exists {
color = &UIColor{
nameColor: ui.randomColor()}
ui.colorMap[message.peer.username] = color
}
msg := color.nameColor + message.peer.username +
"\u001b[0m" + ":" + message.text + "\u001b[0m"
// Add to message panel
messagesView, _ := g.View("messages")
g.Update(func(g *gocui.Gui) error {
fmt.Fprintln(messagesView, msg)
return nil
})
}