-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
146 lines (125 loc) · 3.05 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
package main
import (
"fmt"
"log"
"strings"
"github.com/jroimartin/gocui"
)
func drawchat() {
// Create a new GUI.
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
panic(err)
}
defer g.Close()
g.Cursor = true
// Update the views when terminal changes size.
g.SetManagerFunc(func(g *gocui.Gui) error {
termwidth, termheight := g.Size()
_, err := g.SetView("output", 0, 0, termwidth-1, termheight-4)
if err != nil {
return err
}
_, err = g.SetView("input", 0, termheight-3, termwidth-1, termheight-1)
if err != nil {
return err
}
return nil
})
// Terminal width and height.
termwidth, termheight := g.Size()
// Output.
ov, err := g.SetView("output", 0, 0, termwidth-1, termheight-4)
if err != nil && err != gocui.ErrUnknownView {
log.Println("Failed to create output view:", err)
return
}
ov.Title = " Messages "
ov.FgColor = gocui.ColorRed
ov.Autoscroll = true
ov.Wrap = true
// Input.
iv, err := g.SetView("input", 0, termheight-3, termwidth-1, termheight-1)
if err != nil && err != gocui.ErrUnknownView {
log.Println("Failed to create input view:", err)
return
}
iv.Title = " New Message "
iv.FgColor = gocui.ColorWhite
iv.Editable = true
err = iv.SetCursor(0, 0)
if err != nil {
log.Println("Failed to set cursor:", err)
return
}
// Bind Ctrl-C so the user can quit.
err = g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
})
if err != nil {
log.Println("Could not set key binding:", err)
return
}
// Subscribe (listen on) a channel.
chatMessage := make(chan string)
// Bind enter key to input to send new messages.
err = g.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, func(g *gocui.Gui, iv *gocui.View) error {
// Read buffer from the beginning.
iv.Rewind()
// Send message if text was entered.
if len(iv.Buffer()) >= 2 {
chatMessage <- "You: " + iv.Buffer()
writeData(iv.Buffer())
// Reset input.
iv.Clear()
// Reset cursor.
err = iv.SetCursor(0, 0)
if err != nil {
log.Println("Failed to set cursor:", err)
}
return err
}
return nil
})
if err != nil {
log.Println("Cannot bind the enter key:", err)
}
// Set the focus to input.
_, err = g.SetCurrentView("input")
if err != nil {
log.Println("Cannot set focus to input view:", err)
}
incomingMessages := make(chan string)
go func(c <-chan string) {
readData(incomingMessages)
for {
msg := <-incomingMessages
if !strings.Contains(msg, "|heartbeat|") {
chatMessage <- "Them: " + msg
}
}
}(incomingMessages)
go func() {
for {
select {
case message := <-chatMessage:
ov, err := g.View("output")
if err != nil {
log.Println("Cannot get output view:", err)
return
}
_, err = fmt.Fprintf(ov, "%s", message)
if err != nil {
log.Println("Cannot print to output view:", err)
}
// Refresh view
g.Update(func(g *gocui.Gui) error {
return nil
})
}
}
}()
// Start the main loop.
err = g.MainLoop()
log.Println("Main loop has finished:", err)
}