-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
129 lines (118 loc) · 2.84 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
package main
import (
"context"
"github.com/mum4k/termdash"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/linestyle"
"github.com/mum4k/termdash/terminal/tcell"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/mum4k/termdash/widgets/text"
"github.com/mum4k/termdash/widgets/textinput"
)
// the "wrapped" widget
func createWrapped() *text.Text {
wrapped, err := text.New(text.WrapAtRunes())
if err != nil {
panic(err)
}
if err := wrapped.Write("Supports", text.WriteCellOpts(cell.FgColor(cell.ColorRed))); err != nil {
panic(err)
}
if err := wrapped.Write(" colors", text.WriteCellOpts(cell.FgColor(cell.ColorNumber(33)))); err != nil {
panic(err)
}
return wrapped
}
// ChatLogUI widget
func createChatLogUI(mc <-chan Message) *text.Text {
rolled, err := text.New(text.RollContent(), text.WrapAtWords())
if err != nil {
panic(err)
}
go func() {
for {
for m := range mc {
var err error
for _, txt := range m.texts {
if txt.text != "" {
err = rolled.Write(txt.text)
}
}
err = rolled.Write("\n")
if err != nil {
panic(err)
}
}
}
}()
return rolled
}
// ChatInputUI widget
func createChatInputUI(cc chan<- Command) *textinput.TextInput {
input, err := textinput.New(
textinput.FillColor(cell.ColorDefault),
textinput.TextColor(cell.ColorDefault),
textinput.ClearOnSubmit(),
textinput.OnSubmit(func(text string) error {
if text != "" {
cc <- Command{text: text, origin: USER}
}
return nil
}),
)
if err != nil {
panic(err)
}
return input
}
func createUI(cc chan<- Command, mc <-chan Message) {
ctx, cancel := context.WithCancel(context.Background())
wrapped := createWrapped()
chatInputUI := createChatInputUI(cc)
chatLogUI := createChatLogUI(mc)
rootUI, err := tcell.New()
if err != nil {
panic(err)
}
defer rootUI.Close()
innerUI, err := container.New(
rootUI,
container.Border(linestyle.Light),
container.BorderTitle("RPG"),
container.SplitVertical(
container.Left(
container.SplitHorizontal(
container.Top(
container.Border(linestyle.Light),
container.BorderTitle("Game Log"),
container.PlaceWidget(chatLogUI),
),
container.Bottom(
container.Border(linestyle.Light),
container.BorderTitle("Chat Input"),
container.PlaceWidget(chatInputUI),
),
container.SplitPercent(85),
),
),
container.Right(
container.Border(linestyle.Light),
container.BorderTitle("Wraps lines at rune boundaries"),
container.PlaceWidget(wrapped),
),
container.SplitPercent(70),
),
)
if err != nil {
panic(err)
}
quitter := func(k *terminalapi.Keyboard) {
if k.Key == 'q' || k.Key == 'Q' {
cancel()
}
}
if err := termdash.Run(ctx, rootUI, innerUI, termdash.KeyboardSubscriber(quitter)); err != nil {
panic(err)
}
}