-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatview.go
67 lines (51 loc) · 1.19 KB
/
chatview.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
package main
import (
tui "github.com/marcusolsson/tui-go"
)
type SubmitMessageHandler func(string)
type ChatView struct {
tui.Box
frame *tui.Box
history *tui.Box
onSubmit SubmitMessageHandler
}
func NewChatView() *ChatView {
view := &ChatView{}
// ref: https://github.com/marcusolsson/tui-go/blob/master/example/chat/main.go
view.history = tui.NewVBox()
historyScroll := tui.NewScrollArea(view.history)
historyScroll.SetAutoscrollToBottom(true)
historyBox := tui.NewVBox(historyScroll)
historyBox.SetBorder(true)
input := tui.NewEntry()
input.SetFocused(true)
input.SetSizePolicy(tui.Expanding, tui.Maximum)
input.OnSubmit(func(e *tui.Entry) {
if e.Text() != "" {
if view.onSubmit != nil {
view.onSubmit(e.Text())
}
e.SetText("")
}
})
inputBox := tui.NewHBox(input)
inputBox.SetBorder(true)
inputBox.SetSizePolicy(tui.Expanding, tui.Maximum)
view.frame = tui.NewVBox(
historyBox,
inputBox,
)
view.frame.SetBorder(false)
view.Append(view.frame)
return view
}
func (c *ChatView) OnSubmit(handler SubmitMessageHandler) {
c.onSubmit = handler
}
func (c *ChatView) AddMessage(msg string) {
c.history.Append(
tui.NewHBox(
tui.NewLabel(msg),
),
)
}