/
inputField.go
451 lines (387 loc) · 11.7 KB
/
inputField.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package gui
import (
"strconv"
"strings"
ui "github.com/gizak/termui"
"github.com/nsf/termbox-go"
)
// default mappings between /sys/kbd events and multi-line inputs
var multiLineCharMap = map[string]string{
"<space>": " ",
"<tab>": "\t",
"<enter>": "\n",
"<escape>": "",
}
// default mappings between /sys/kbd events and single line inputs
var singleLineCharMap = map[string]string{
"<space>": " ",
"<tab>": "\t",
"<enter>": "",
"<escape>": "",
}
const NEW_LINE = "\n"
const LINE_NO_MIN_SPACE = 1000
// EvtInput defines the structure for the /input/* events. The event contains the last keystroke, the full text
// for the current line, and the position of the cursor in the current line as well as the index of the current
// line in the full text of the input
type EvtInput struct {
KeyStr string
LineText string
CursorPosition int
LineIndex int
}
// Input is the main object for a text input. The object exposes the following public properties:
// TextFgColor: color for the text.
// TextBgColor: background color for the text box.
// IsCapturing: true if the input is currently capturing keyboard events, this is controlled by the StartCapture and
// StopCapture methods.
// IsMultiline: Whether we should accept multiple lines of input or this is a singe line form field.
// TextBuilder: An implementation of the TextBuilder interface to customize the look of the text on the screen.
// SpecialChars: a map[string]string of characters from the /sys/kbd events to actual strings in the content.
// Name: When specified, the Input uses its name to propagate events, for example /input/<name>/kbd.
type Input struct {
ui.Block
TextFgColor ui.Attribute
TextBgColor ui.Attribute
IsCapturing bool
IsMultiLine bool
TextBuilder ui.TextBuilder
SpecialChars map[string]string
ShowLineNo bool
Name string
CursorX int
CursorY int
//DebugMode bool
//debugMessage string
// internal vars
lines []string
cursorLineIndex int
cursorLinePos int
}
// NewInput returns a new, initialized Input object. The method receives the initial content for the input (if any)
// and whether it should be initialized as a multi-line innput field or not
func NewInput(s string, isMultiLine bool) *Input {
textArea := &Input{
Block: *ui.NewBlock(),
TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"),
TextBuilder: ui.NewMarkdownTxBuilder(),
IsMultiLine: isMultiLine,
ShowLineNo: false,
cursorLineIndex: 0,
cursorLinePos: 0,
}
if s != "" {
textArea.SetText(s)
}
if isMultiLine {
textArea.SpecialChars = multiLineCharMap
} else {
textArea.SpecialChars = singleLineCharMap
}
return textArea
}
// StartCapture begins catching events from the /sys/kbd stream and updates the content of the Input field. While
// capturing events, the Input field also publishes its own event stream under the /input/kbd path.
func (i *Input) StartCapture() {
i.IsCapturing = true
ui.Handle("/sys/kbd", func(e ui.Event) {
if i.IsCapturing {
key := e.Data.(ui.EvtKbd).KeyStr
switch key {
// case "<up>":
// i.moveUp()
// case "<down>":
// i.moveDown()
case "<left>":
i.moveLeft()
case "<right>":
i.moveRight()
case "C-8":
i.backspace()
default:
// If it's a CTRL something we don't handle then just ignore it
if strings.HasPrefix(key, "C-") {
break
}
newString := i.getCharString(key)
i.addString(newString)
}
if i.Name == "" {
ui.SendCustomEvt("/input/kbd", i.getInputEvt(key))
} else {
ui.SendCustomEvt("/input/"+i.Name+"/kbd", i.getInputEvt(key))
}
ui.Render(i)
}
})
}
// StopCapture tells the Input field to stop accepting events from the /sys/kbd stream
func (i *Input) StopCapture() {
i.IsCapturing = false
}
// Text returns the text of the input field as a string
func (i *Input) Text() string {
if len(i.lines) == 0 {
return ""
}
if len(i.lines) == 1 {
return i.lines[0]
}
if i.IsMultiLine {
return strings.Join(i.lines, NEW_LINE)
} else {
// we should never get here!
return i.lines[0]
}
}
func (i *Input) SetText(text string) {
i.lines = strings.Split(text, NEW_LINE)
}
// Lines returns the slice of strings with the content of the input field. By default lines are separated by \n
func (i *Input) Lines() []string {
return i.lines
}
func (i *Input) Clear() {
i.SetText("")
i.CursorX = 0
i.CursorY = 0
i.cursorLinePos = 0
}
// Private methods for the input field
// TODO: handle delete key
func (i *Input) backspace() {
curLine := i.lines[i.cursorLineIndex]
// at the beginning of the buffer, nothing to do
if len(curLine) == 0 && i.cursorLineIndex == 0 {
return
}
// at the beginning of a line somewhere in the buffer
if i.cursorLinePos == 0 {
prevLine := i.lines[i.cursorLineIndex-1]
// remove the newline character from the prevline
prevLine = prevLine[:len(curLine)-1] + curLine
i.lines = append(i.lines[:i.cursorLineIndex], i.lines[i.cursorLineIndex+1:]...)
i.cursorLineIndex--
i.cursorLinePos = len(prevLine) - 1
return
}
// I'm at the end of a line
if i.cursorLinePos == len(curLine)-1 {
i.lines[i.cursorLineIndex] = curLine[:len(curLine)-1]
i.cursorLinePos--
return
}
// I'm in the middle of a line
i.lines[i.cursorLineIndex] = curLine[:i.cursorLinePos-1] + curLine[i.cursorLinePos:]
i.cursorLinePos--
}
func (i *Input) addString(key string) {
if len(i.lines) > 0 {
if key == NEW_LINE {
// special case when we go back to the beginning of a buffer with multiple lines, prepend a new line
if i.cursorLineIndex == 0 && len(i.lines) > 1 {
i.lines = append([]string{""}, i.lines...)
// this case handles newlines at the end of the file or in the middle of the file
} else {
newString := ""
// if we are inserting a newline in a populated line then set what goes into the new line
// and what stays in the current line
if i.cursorLinePos < len(i.lines[i.cursorLineIndex]) {
newString = i.lines[i.cursorLineIndex][i.cursorLinePos:]
i.lines[i.cursorLineIndex] = i.lines[i.cursorLineIndex][:i.cursorLinePos]
}
// append a newline in the current position with the content we computed in the previous if statement
i.lines = append(
i.lines[:i.cursorLineIndex+1],
append(
[]string{newString},
i.lines[i.cursorLineIndex+1:]...,
)...,
)
}
// increment the line index, reset the cursor to the beginning and return to skip the next step
i.cursorLineIndex++
i.cursorLinePos = 0
return
}
// cursor is at the end of the line
if i.cursorLinePos == len(i.lines[i.cursorLineIndex]) {
//i.debugMessage ="end"
i.lines[i.cursorLineIndex] += key
// cursor at the beginning of the line
} else if i.cursorLinePos == 0 {
//i.debugMessage = "beginning"
i.lines[i.cursorLineIndex] = key + i.lines[i.cursorLineIndex]
// cursor in the middle of the line
} else {
//i.debugMessage = "middle"
before := i.lines[i.cursorLineIndex][:i.cursorLinePos]
after := i.lines[i.cursorLineIndex][i.cursorLinePos:]
i.lines[i.cursorLineIndex] = before + key + after
}
i.cursorLinePos += len(key)
} else {
//i.debugMessage = "newline"
i.lines = append(i.lines, key)
i.cursorLinePos += len(key)
}
}
func (i *Input) moveUp() {
// if we are already on the first line then just move the cursor to the beginning
if i.cursorLineIndex == 0 {
i.cursorLinePos = 0
return
}
// The previous line is just as long, we can move to the same position in the line
prevLine := i.lines[i.cursorLineIndex-1]
if len(prevLine) >= i.cursorLinePos {
i.cursorLineIndex--
} else {
// otherwise we move the cursor to the end of the previous line
i.cursorLineIndex--
i.cursorLinePos = len(prevLine) - 1
}
}
func (i *Input) moveDown() {
// we are already on the last line, we just need to move the position to the end of the line
if i.cursorLineIndex == len(i.lines)-1 {
i.cursorLinePos = len(i.lines[i.cursorLineIndex])
return
}
// check if the cursor can move to the same position in the next line, otherwise move it to the end
nextLine := i.lines[i.cursorLineIndex+1]
if len(nextLine) >= i.cursorLinePos {
i.cursorLineIndex++
} else {
i.cursorLineIndex++
i.cursorLinePos = len(nextLine) - 1
}
}
func (i *Input) moveLeft() {
// if we are at the beginning of the line move the cursor to the previous line
if i.cursorLinePos == 0 {
origLine := i.cursorLineIndex
i.moveUp()
if origLine > 0 {
i.cursorLinePos = len(i.lines[i.cursorLineIndex])
}
return
}
i.cursorLinePos--
}
func (i *Input) moveRight() {
// if we are at the end of the line move to the next
if i.cursorLinePos >= len(i.lines[i.cursorLineIndex]) {
origLine := i.cursorLineIndex
i.moveDown()
if origLine < len(i.lines)-1 {
i.cursorLinePos = 0
}
return
}
i.cursorLinePos++
}
// Buffer implements Bufferer interface.
func (i *Input) Buffer() ui.Buffer {
buf := i.Block.Buffer()
// offset used to display the line numbers
textXOffset := 0
bufferLines := i.lines[:]
firstLine := 0
lastLine := i.InnerBounds().Dy()
if i.IsMultiLine {
if i.cursorLineIndex >= lastLine {
firstLine += i.cursorLineIndex - lastLine + 1
lastLine += i.cursorLineIndex - lastLine + 1
}
if len(i.lines) < lastLine {
bufferLines = i.lines[firstLine:]
} else {
bufferLines = i.lines[firstLine:lastLine]
}
}
if i.ShowLineNo {
// forcing space for up to 1K
if lastLine < LINE_NO_MIN_SPACE {
textXOffset = len(strconv.Itoa(LINE_NO_MIN_SPACE)) + 2
} else {
textXOffset = len(strconv.Itoa(lastLine)) + 2 // one space at the beginning and one at the end
}
}
text := strings.Join(bufferLines, NEW_LINE)
// if the last line is empty then we add a fake space to make sure line numbers are displayed
if len(bufferLines) > 0 && bufferLines[len(bufferLines)-1] == "" && i.ShowLineNo {
text += " "
}
fg, bg := i.TextFgColor, i.TextBgColor
cs := i.TextBuilder.Build(text, fg, bg)
y, x, n := 0, 0, 0
lineNoCnt := 1
for n < len(cs) {
w := cs[n].Width()
if x == 0 && i.ShowLineNo {
curLineNoString := " " + strconv.Itoa(lineNoCnt) +
strings.Join(make([]string, textXOffset-len(strconv.Itoa(lineNoCnt))-1), " ")
//i.debugMessage = "Line no: " + curLineNoString
curLineNoRunes := i.TextBuilder.Build(curLineNoString, fg, bg)
for lineNo := 0; lineNo < len(curLineNoRunes); lineNo++ {
buf.Set(i.InnerX()+lineNo, i.InnerY()+y, curLineNoRunes[lineNo])
}
lineNoCnt++
}
if cs[n].Ch == '\n' {
y++
n++
x = 0 // set x = 0
continue
}
buf.Set(i.InnerX()+x+textXOffset, i.InnerY()+y, cs[n])
n++
x += w
}
cursorXOffset := i.X + textXOffset
if i.BorderLeft {
cursorXOffset++
}
cursorYOffset := i.Y // termui.TermHeight() - i.innerArea.Dy()
if i.BorderTop {
cursorYOffset++
}
if lastLine > i.InnerBounds().Dy() {
cursorYOffset += i.InnerBounds().Dy() - 1
} else {
cursorYOffset += i.cursorLineIndex
}
if i.IsCapturing {
i.CursorX = i.cursorLinePos + cursorXOffset
i.CursorY = cursorYOffset
termbox.SetCursor(i.cursorLinePos+cursorXOffset, cursorYOffset)
}
/*
if i.DebugMode {
position := fmt.Sprintf("%s li: %d lp: %d n: %d", i.debugMessage, i.cursorLineIndex, i.cursorLinePos, len(i.lines))
for idx, char := range position {
buf.Set(i.innerArea.Min.X+i.innerArea.Dx()-len(position) + idx,
i.innerArea.Min.Y+i.innerArea.Dy()-1,
Cell{Ch: char, Fg: i.TextFgColor, Bg: i.TextBgColor})
}
}
*/
return buf
}
func (i *Input) getCharString(s string) string {
if val, ok := i.SpecialChars[s]; ok {
return val
} else {
return s
}
}
func (i *Input) getInputEvt(key string) EvtInput {
return EvtInput{
KeyStr: key,
LineText: i.lines[i.cursorLineIndex],
CursorPosition: i.cursorLinePos,
LineIndex: i.cursorLineIndex,
}
}