forked from Stewmath/oracles-randomizer-ng
/
ui.go
330 lines (290 loc) · 7.62 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package randomizer
import (
"fmt"
"strings"
"github.com/gdamore/tcell"
)
// a uiSegment is a text string with formatting information.
type uiSegment struct {
style tcell.Style
text string
el uiEllipsis
}
// a uiLine is just an array of segments.
type uiLine []uiSegment
// a uiEllipsis defines whether a segment will opt in to being truncated at the
// left or right to fit onscreen. by default, lines are truncated at the right,
// but if (for example) a segment in the line has ellipsisLeft, that segment
// will be truncated at the left in order to make the line fit.
type uiEllipsis int
// ellipsis constants
const (
ellipsisNone uiEllipsis = iota
ellipsisLeft
ellipsisRight
)
// a uiMode defines the way the UI currently handles input and displays
// information.
type uiMode int
// uiMode constants
const (
modeWorking uiMode = iota
modePrompt
modeDone
)
var uiBottom = []uiSegment{
{text: "(q)", style: tcell.StyleDefault.Bold(true)},
{text: "uit"},
}
type uiInstance struct {
// this one's actually used as a constant, but can't be declared as one
lines []uiLine
screen tcell.Screen
write, rewrite chan uiLine
input, prompt chan rune
resize chan interface{}
change chan uiMode
}
// creates and displays a blank TUI.
func newUI(title string) *uiInstance {
screen, err := tcell.NewScreen()
if err != nil {
panic(err)
}
if err := screen.Init(); err != nil {
_ = err
}
ui := &uiInstance{
lines: []uiLine{
[]uiSegment{{text: title}},
},
screen: screen,
write: make(chan uiLine, 1), // add a line
rewrite: make(chan uiLine), // rewrite the last line
input: make(chan rune), // key input to main loop
prompt: make(chan rune), // key input passed from main to prompt
resize: make(chan interface{}, 1), // send to update window size
change: make(chan uiMode, 1), // change uiMode
}
ui.draw(modeWorking)
return ui
}
// runs the TUI, waiting for input from other functions and displaying updated
// information as needed.
func (ui *uiInstance) run() {
// run event processing in a different goroutine
go func() {
for {
switch evt := ui.screen.PollEvent().(type) {
case *tcell.EventKey:
switch evt.Key() {
case tcell.KeyCtrlC, tcell.KeyDEL: // del is backspace
ui.input <- rune(evt.Key())
case tcell.KeyRune:
ui.input <- evt.Rune()
}
case *tcell.EventResize:
ui.resize <- 1
}
}
}()
// continuously select from various channels
mode := modeWorking
loop := true
for loop {
select {
case ln := <-ui.write:
ui.lines = append(ui.lines, ln)
ui.draw(mode)
case ln := <-ui.rewrite:
ui.lines[len(ui.lines)-1] = ln
ui.draw(mode)
case ch := <-ui.input:
if ch == 'q' || ch == rune(tcell.KeyCtrlC) || mode == modeDone {
ui.screen.Fini()
loop = false
} else if mode == modePrompt {
ui.prompt <- ch
}
case <-ui.resize:
ui.screen.Sync()
ui.draw(mode)
case m := <-ui.change:
mode = m
ui.draw(mode)
}
}
}
// (re)draws the entire display.
func (ui *uiInstance) draw(mode uiMode) {
ui.screen.Clear()
// draw title bar
w, h := ui.screen.Size()
ui.drawLine(w, 0, ui.lines[0])
var x int
for x := 0; x < w; x++ {
ui.screen.SetContent(x, 1, '─', nil, tcell.StyleDefault)
}
// draw content lines
scroll := 0
if len(ui.lines) > h-3 {
scroll = len(ui.lines) - (h - 3)
}
for i, ln := range ui.lines[scroll+1:] {
x = ui.drawLine(w, i+2, ln)
}
// draw bottom bar
for x := 0; x < w; x++ {
ui.screen.SetContent(x, h-2, '─', nil, tcell.StyleDefault)
}
ui.drawLine(w, h-1, uiBottom)
// draw cursor if applicable
if mode == modePrompt {
ui.screen.ShowCursor(x, len(ui.lines)-scroll)
} else {
ui.screen.HideCursor()
}
ui.screen.Show()
}
// draws a line of text on the display, truncating it as needed (not wrapping
// it).
func (ui *uiInstance) drawLine(w, y int, ln uiLine) int {
// figure out whether the line needs to be shortened
var truncLen int
truncIndex := -1
lineWidth := 0
for _, seg := range ln {
lineWidth += len(seg.text)
}
if lineWidth > w-2 {
// figure out which segment to shorten
truncIndex = len(ln) - 1
for i, seg := range ln {
if seg.el != ellipsisNone {
truncIndex = i
break
}
}
// figure out by how much to shorten it
truncLen = len(ln[truncIndex].text) - (lineWidth - (w - 2) + 3)
if truncLen < 0 {
truncLen = 0
}
}
// draw characters
x := 0
for i, seg := range ln {
text := seg.text
// ...text
if i == truncIndex {
if seg.el == ellipsisLeft {
x = ui.drawEllipsis(x, y, seg.style)
text = text[len(text)-truncLen:]
} else {
text = text[:truncLen]
}
}
for _, ch := range text {
ui.screen.SetContent(x, y, ch, nil, seg.style)
x++
}
// text...
if i == truncIndex && seg.el != ellipsisLeft {
x = ui.drawEllipsis(x, y, seg.style)
}
}
return x
}
// drawEllipsis draws "..." at the given coords and returns the new x position.
func (ui *uiInstance) drawEllipsis(x, y int, style tcell.Style) int {
for i := 0; i < 3; i++ {
ui.screen.SetContent(x, y, '.', nil, style)
x++
}
return x
}
// adds a line to the display, formatted by fmt.Sprintf.
func (ui *uiInstance) printf(format string, a ...interface{}) {
ui.write <- []uiSegment{{text: fmt.Sprintf(format, a...)}}
}
// adds a line to the display, with a middle path segment that is truncated at
// the left rather than the right.
func (ui *uiInstance) printPath(pre, path, post string) {
ui.write <- []uiSegment{
{text: pre},
{text: path, el: ellipsisLeft},
{text: post},
}
}
// prints the given string and blocks until the user inputs one of the
// alphanumeric characters shown in parentheses. for example, a prompt
// containing "(y/n)" would accept either 'y' or 'n' as input. multiple
// parentheticals can be included.
func (ui *uiInstance) doPrompt(s string) rune {
acceptedRunes := ""
// show parentheticals in bold
line := make([]uiSegment, 0)
pos := 0
for pos < len(s) {
open := strings.IndexRune(s[pos:], '(')
if open == -1 {
line = append(line, uiSegment{text: s[pos:]})
break
} else {
close := strings.IndexRune(s[pos+open:], ')')
if close == -1 {
line = append(line, uiSegment{text: s[pos:]})
break
} else {
line = append(line, uiSegment{text: s[pos : pos+open]})
line = append(line, uiSegment{
text: s[pos+open : pos+open+close+1],
style: tcell.StyleDefault.Bold(true),
})
acceptedRunes += s[pos+open+1 : pos+open+close]
pos += open + close + 1
}
}
}
// add space before cursor
line = append(line, uiSegment{text: " "})
// wait for and return a valid rune
ui.write <- line
ui.change <- modePrompt
for {
ch := <-ui.prompt
if strings.ContainsRune(acceptedRunes, ch) {
ui.rewrite <- append(line, uiSegment{text: string(ch)})
ui.change <- modeWorking
return ch
}
}
}
// waits for the user to input 8 hex digits, then returns the string.
func (ui *uiInstance) promptSeed(s string) string {
acceptedRunes := "0123456789abcdef"
line := []uiSegment{{text: s + " "}, {text: ""}}
ui.write <- line
ui.change <- modePrompt
for {
ch := <-ui.prompt
if strings.ContainsRune(acceptedRunes, ch) {
line[1].text += string(ch)
} else if ch == rune(tcell.KeyDEL) && len(line[1].text) > 0 {
line[1].text = line[1].text[:len(line[1].text)-1]
}
ui.rewrite <- line
if len(line[1].text) == 8 {
ui.change <- modeWorking
return line[1].text
}
}
}
// changes the mode to one where no action is taken, and any input closes the
// program.
func (ui *uiInstance) done() {
ui.write <- []uiSegment{{}}
ui.write <- []uiSegment{{text: "press any key to exit.",
style: tcell.StyleDefault.Bold(true)}}
ui.change <- modeDone
}