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
|
package ui
import (
"github.com/gdamore/tcell/v2"
)
type Completion struct {
Text []rune
CursorIdx int
}
// Editor is the text field where the user writes messages and commands.
type Editor struct {
// text contains the written runes. An empty slice means no text is written.
text [][]rune
lineIdx int
// textWidth[i] contains the width of string(text[:i]). Therefore
// len(textWidth) is always strictly greater than 0 and textWidth[0] is
// always 0.
textWidth []int
// cursorIdx is the index in text of the placement of the cursor, or is
// equal to len(text) if the cursor is at the end.
cursorIdx int
// offsetIdx is the number of elements of text that are skipped when
// rendering.
offsetIdx int
// width is the width of the screen.
width int
autoComplete func(cursorIdx int, text []rune) []Completion
autoCache []Completion
autoCacheIdx int
}
func NewEditor(width int, autoComplete func(cursorIdx int, text []rune) []Completion) Editor {
return Editor{
text: [][]rune{{}},
textWidth: []int{0},
width: width,
autoComplete: autoComplete,
}
}
func (e *Editor) Resize(width int) {
if width < e.width {
e.cursorIdx = 0
e.offsetIdx = 0
e.autoCache = nil
}
e.width = width
}
func (e *Editor) IsCommand() bool {
line := e.text[e.lineIdx]
// Command can't start with two slashes because that's an escape for
// a literal slash in the message
return len(line) >= 1 && line[0] == '/' && !(len(line) >= 2 && line[1] == '/')
}
func (e *Editor) TextLen() int {
return len(e.text[e.lineIdx])
}
func (e *Editor) PutRune(r rune) {
e.putRune(r)
e.Right()
e.autoCache = nil
}
func (e *Editor) putRune(r rune) {
e.text[e.lineIdx] = append(e.text[e.lineIdx], ' ')
copy(e.text[e.lineIdx][e.cursorIdx+1:], e.text[e.lineIdx][e.cursorIdx:])
e.text[e.lineIdx][e.cursorIdx] = r
rw := runeWidth(r)
tw := e.textWidth[len(e.textWidth)-1]
e.textWidth = append(e.textWidth, tw+rw)
for i := e.cursorIdx + 1; i < len(e.textWidth); i++ {
e.textWidth[i] = rw + e.textWidth[i-1]
}
}
func (e *Editor) RemRune() (ok bool) {
ok = 0 < e.cursorIdx
if !ok {
return
}
e.remRuneAt(e.cursorIdx - 1)
e.autoCache = nil
e.Left()
return
}
func (e *Editor) RemRuneForward() (ok bool) {
ok = e.cursorIdx < len(e.text[e.lineIdx])
if !ok {
return
}
e.remRuneAt(e.cursorIdx)
e.autoCache = nil
return
}
func (e *Editor) remRuneAt(idx int) {
// TODO avoid looping twice
rw := e.textWidth[idx+1] - e.textWidth[idx]
for i := idx + 1; i < len(e.textWidth); i++ {
e.textWidth[i] -= rw
}
copy(e.textWidth[idx+1:], e.textWidth[idx+2:])
e.textWidth = e.textWidth[:len(e.textWidth)-1]
copy(e.text[e.lineIdx][idx:], e.text[e.lineIdx][idx+1:])
e.text[e.lineIdx] = e.text[e.lineIdx][:len(e.text[e.lineIdx])-1]
}
func (e *Editor) RemWord() (ok bool) {
ok = 0 < e.cursorIdx
if !ok {
return
}
line := e.text[e.lineIdx]
// To allow doing something like this (| is the cursor):
// Hello world|
// Hello |
// |
for e.cursorIdx > 0 && line[e.cursorIdx-1] == ' ' {
e.remRuneAt(e.cursorIdx - 1)
e.Left()
}
for i := e.cursorIdx - 1; i >= 0; i -= 1 {
if line[i] == ' ' {
break
}
e.remRuneAt(i)
e.Left()
}
e.autoCache = nil
return
}
func (e *Editor) Flush() (content string) {
content = string(e.text[e.lineIdx])
if len(e.text[len(e.text)-1]) == 0 {
e.lineIdx = len(e.text) - 1
} else {
e.lineIdx = len(e.text)
e.text = append(e.text, []rune{})
}
e.textWidth = e.textWidth[:1]
e.cursorIdx = 0
e.offsetIdx = 0
e.autoCache = nil
return
}
func (e *Editor) Clear() bool {
if e.TextLen() == 0 {
return false
}
e.text[e.lineIdx] = []rune{}
e.textWidth = e.textWidth[:1]
e.cursorIdx = 0
e.offsetIdx = 0
e.autoCache = nil
return true
}
func (e *Editor) Right() {
e.right()
e.autoCache = nil
}
func (e *Editor) right() {
if e.cursorIdx == len(e.text[e.lineIdx]) {
return
}
e.cursorIdx++
if e.width <= e.textWidth[e.cursorIdx]-e.textWidth[e.offsetIdx] {
e.offsetIdx += 16
max := len(e.text[e.lineIdx]) - 1
if max < e.offsetIdx {
e.offsetIdx = max
}
}
}
func (e *Editor) RightWord() {
line := e.text[e.lineIdx]
if e.cursorIdx == len(line) {
return
}
for e.cursorIdx < len(line) && line[e.cursorIdx] == ' ' {
e.Right()
}
for i := e.cursorIdx; i < len(line) && line[i] != ' '; i += 1 {
e.Right()
}
}
func (e *Editor) Left() {
if e.cursorIdx == 0 {
return
}
e.cursorIdx--
if e.cursorIdx <= e.offsetIdx {
e.offsetIdx -= 16
if e.offsetIdx < 0 {
e.offsetIdx = 0
}
}
}
func (e *Editor) LeftWord() {
if e.cursorIdx == 0 {
return
}
line := e.text[e.lineIdx]
for e.cursorIdx > 0 && line[e.cursorIdx-1] == ' ' {
e.Left()
}
for i := e.cursorIdx - 1; i >= 0 && line[i] != ' '; i -= 1 {
e.Left()
}
e.autoCache = nil
}
func (e *Editor) Home() {
if e.cursorIdx == 0 {
return
}
e.cursorIdx = 0
e.offsetIdx = 0
e.autoCache = nil
}
func (e *Editor) End() {
if e.cursorIdx == len(e.text[e.lineIdx]) {
return
}
e.cursorIdx = len(e.text[e.lineIdx])
for e.width < e.textWidth[e.cursorIdx]-e.textWidth[e.offsetIdx]+16 {
e.offsetIdx++
}
e.autoCache = nil
}
func (e *Editor) Up() {
if e.lineIdx == 0 {
return
}
e.lineIdx--
e.computeTextWidth()
e.cursorIdx = 0
e.offsetIdx = 0
e.autoCache = nil
e.End()
}
func (e *Editor) Down() {
if e.lineIdx == len(e.text)-1 {
if len(e.text[e.lineIdx]) == 0 {
return
}
e.Flush()
return
}
e.lineIdx++
e.computeTextWidth()
e.cursorIdx = 0
e.offsetIdx = 0
e.autoCache = nil
e.End()
}
func (e *Editor) AutoComplete(offset int) (ok bool) {
if e.autoCache == nil {
e.autoCache = e.autoComplete(e.cursorIdx, e.text[e.lineIdx])
if len(e.autoCache) == 0 {
e.autoCache = nil
return false
}
e.autoCacheIdx = 0
} else {
e.autoCacheIdx = (e.autoCacheIdx + len(e.autoCache) + offset) % len(e.autoCache)
}
e.text[e.lineIdx] = e.autoCache[e.autoCacheIdx].Text
e.cursorIdx = e.autoCache[e.autoCacheIdx].CursorIdx
e.computeTextWidth()
if len(e.textWidth) <= e.offsetIdx {
e.offsetIdx = 0
}
for e.width < e.textWidth[e.cursorIdx]-e.textWidth[e.offsetIdx]+16 {
e.offsetIdx++
}
return true
}
func (e *Editor) computeTextWidth() {
e.textWidth = e.textWidth[:1]
rw := 0
for _, r := range e.text[e.lineIdx] {
rw += runeWidth(r)
e.textWidth = append(e.textWidth, rw)
}
}
func (e *Editor) Draw(screen tcell.Screen, x0, y int) {
st := tcell.StyleDefault
x := x0
i := e.offsetIdx
for i < len(e.text[e.lineIdx]) && x < x0+e.width {
r := e.text[e.lineIdx][i]
screen.SetContent(x, y, r, nil, st)
x += runeWidth(r)
i++
}
for x < x0+e.width {
screen.SetContent(x, y, ' ', nil, st)
x++
}
cursorX := x0 + e.textWidth[e.cursorIdx] - e.textWidth[e.offsetIdx]
screen.ShowCursor(cursorX, y)
}
|