forked from jroimartin/gocui
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtext_area.go
More file actions
656 lines (546 loc) · 16 KB
/
text_area.go
File metadata and controls
656 lines (546 loc) · 16 KB
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package gocui
import (
"regexp"
"slices"
"strings"
"github.com/rivo/uniseg"
)
const (
WHITESPACES = " \t"
WORD_SEPARATORS = "*?_+-.[]~=/&;!#$%^(){}<>"
)
type TextAreaCell struct {
char string // string because it could be a multi-rune grapheme cluster
width int
x, y int // cell coordinates
contentIndex int // byte index into the original content
}
// returns the cursor x,y position after this cell
func (c *TextAreaCell) nextCursorXY() (int, int) {
if c.char == "\n" {
return 0, c.y + 1
}
return c.x + c.width, c.y
}
type TextArea struct {
content string
cells []TextAreaCell
cursor int // position in content, as an index into the byte array
overwrite bool
clipboard string
AutoWrap bool
AutoWrapWidth int
}
func stringToTextAreaCells(str string) []TextAreaCell {
result := make([]TextAreaCell, 0, len(str))
contentIndex := 0
state := -1
for len(str) > 0 {
var c string
var w int
c, str, w, state = uniseg.FirstGraphemeClusterInString(str, state)
// only set char, width, and contentIndex; x and y will be set later
result = append(result, TextAreaCell{char: c, width: w, contentIndex: contentIndex})
contentIndex += len(c)
}
return result
}
// Returns the indices in content where soft line breaks occur due to auto-wrapping to the given width.
func AutoWrapContent(content string, autoWrapWidth int) []int {
_, softLineBreakIndices := contentToCells(content, autoWrapWidth)
return softLineBreakIndices
}
func contentToCells(content string, autoWrapWidth int) ([]TextAreaCell, []int) {
estimatedNumberOfSoftLineBreaks := 0
if autoWrapWidth > 0 {
estimatedNumberOfSoftLineBreaks = len(content) / autoWrapWidth
}
softLineBreakIndices := make([]int, 0, estimatedNumberOfSoftLineBreaks)
result := make([]TextAreaCell, 0, len(content)+estimatedNumberOfSoftLineBreaks)
startOfLine := 0
currentLineWidth := 0
indexOfLastWhitespace := -1
var footNoteMatcher footNoteMatcher
var trailerMatcher trailerMatcher
cells := stringToTextAreaCells(content)
y := 0
appendCellsSinceLineStart := func(to int) {
x := 0
for i := startOfLine; i < to; i++ {
cells[i].x = x
cells[i].y = y
x += cells[i].width
}
result = append(result, cells[startOfLine:to]...)
}
for currentPos, c := range cells {
if c.char == "\n" {
appendCellsSinceLineStart(currentPos + 1)
y++
startOfLine = currentPos + 1
indexOfLastWhitespace = -1
currentLineWidth = 0
footNoteMatcher.reset()
trailerMatcher.reset()
} else {
currentLineWidth += c.width
if c.char == " " && !footNoteMatcher.isFootNote() && !trailerMatcher.isTrailer() {
indexOfLastWhitespace = currentPos + 1
} else if autoWrapWidth > 0 && currentLineWidth > autoWrapWidth && indexOfLastWhitespace >= 0 {
wrapAt := indexOfLastWhitespace
appendCellsSinceLineStart(wrapAt)
contentIndex := cells[wrapAt].contentIndex
y++
result = append(result, TextAreaCell{char: "\n", width: 1, contentIndex: contentIndex, x: 0, y: y})
softLineBreakIndices = append(softLineBreakIndices, contentIndex)
startOfLine = wrapAt
indexOfLastWhitespace = -1
currentLineWidth = 0
for _, c1 := range cells[startOfLine : currentPos+1] {
currentLineWidth += c1.width
}
footNoteMatcher.reset()
trailerMatcher.reset()
}
footNoteMatcher.addCharacter(c.char)
trailerMatcher.addCharacter(c.char)
}
}
appendCellsSinceLineStart(len(cells))
return result, softLineBreakIndices
}
var footNoteRe = regexp.MustCompile(`^\[\d+\]:\s*$`)
type footNoteMatcher struct {
lineStr strings.Builder
didFailToMatch bool
}
func (self *footNoteMatcher) addCharacter(chr string) {
if self.didFailToMatch {
// don't bother tracking the rune if we know it can't possibly match any more
return
}
if self.lineStr.Len() == 0 && chr != "[" {
// fail early if the first rune of a line isn't a '['; this is mainly to avoid a (possibly
// expensive) regex match
self.didFailToMatch = true
return
}
self.lineStr.WriteString(chr)
}
func (self *footNoteMatcher) isFootNote() bool {
if self.didFailToMatch {
return false
}
if footNoteRe.MatchString(self.lineStr.String()) {
// it's a footnote, so treat spaces as non-breaking. It's important not to reset the matcher
// here, because there could be multiple spaces after a footnote.
return true
}
// no need to check again for this line
self.didFailToMatch = true
return false
}
func (self *footNoteMatcher) reset() {
self.lineStr.Reset()
self.didFailToMatch = false
}
var supportedTrailers = []string{
"Signed-off-by:",
"Co-authored-by:",
}
type trailerMatcher struct {
lineStr strings.Builder
didFailToMatch bool
didMatch bool
}
func (self *trailerMatcher) addCharacter(chr string) {
if self.didFailToMatch || self.didMatch {
return
}
if len(chr) != 1 {
// Trailers are all ASCII, so if we get a non-ASCII UTF-8 character (or even a multi-rune
// grapheme cluster), we can fail early.
self.didFailToMatch = true
return
}
if self.lineStr.Len() == 0 {
// If this is the first character, see if it could possibly match any supported trailer; if
// not, we can fail early and stop tracking further characters for this line.
if !anyOf(supportedTrailers, func(trailer string) bool { return trailer[0] == chr[0] }) {
self.didFailToMatch = true
return
}
}
self.lineStr.WriteString(chr)
}
func (self *trailerMatcher) isTrailer() bool {
if self.didFailToMatch {
return false
}
if self.didMatch {
return true
}
line := self.lineStr.String()
if anyOf(supportedTrailers, func(trailer string) bool { return line == trailer }) {
self.didMatch = true
return true
}
self.didFailToMatch = true
return false
}
func (self *trailerMatcher) reset() {
self.lineStr.Reset()
self.didFailToMatch = false
self.didMatch = false
}
func anyOf(strings []string, predicate func(s string) bool) bool {
for _, s := range strings {
if predicate(s) {
return true
}
}
return false
}
func (self *TextArea) updateCells() {
width := self.AutoWrapWidth
if !self.AutoWrap {
width = -1
}
self.cells, _ = contentToCells(self.content, width)
}
func (self *TextArea) typeCharacter(ch string) {
widthToDelete := 0
if self.overwrite && !self.atEnd() {
s, _, _, _ := uniseg.FirstGraphemeClusterInString(self.content[self.cursor:], -1)
widthToDelete = len(s)
}
self.content = self.content[:self.cursor] + ch + self.content[self.cursor+widthToDelete:]
self.cursor += len(ch)
}
func (self *TextArea) TypeCharacter(ch string) {
self.typeCharacter(ch)
self.updateCells()
}
func (self *TextArea) BackSpaceChar() {
if self.cursor == 0 {
return
}
cellCursor := self.contentCursorToCellCursor(self.cursor)
widthToDelete := len(self.cells[cellCursor-1].char)
oldCursor := self.cursor
self.cursor -= widthToDelete
self.content = self.content[:self.cursor] + self.content[oldCursor:]
self.updateCells()
}
func (self *TextArea) DeleteChar() {
if self.atEnd() {
return
}
s, _, _, _ := uniseg.FirstGraphemeClusterInString(self.content[self.cursor:], -1)
widthToDelete := len(s)
self.content = self.content[:self.cursor] + self.content[self.cursor+widthToDelete:]
self.updateCells()
}
func (self *TextArea) MoveCursorLeft() {
if self.cursor == 0 {
return
}
cellCursor := self.contentCursorToCellCursor(self.cursor)
self.cursor -= len(self.cells[cellCursor-1].char)
}
func (self *TextArea) MoveCursorRight() {
if self.cursor == len(self.content) {
return
}
s, _, _, _ := uniseg.FirstGraphemeClusterInString(self.content[self.cursor:], -1)
self.cursor += len(s)
}
func (self *TextArea) newCursorForMoveLeftWord() int {
if self.cursor == 0 {
return 0
}
if self.atLineStart() {
return self.cursor - 1
}
cellCursor := self.contentCursorToCellCursor(self.cursor)
for cellCursor > 0 && (self.isSoftLineBreak(cellCursor-1) || strings.Contains(WHITESPACES, self.cells[cellCursor-1].char)) {
cellCursor--
}
separators := false
for cellCursor > 0 && strings.Contains(WORD_SEPARATORS, self.cells[cellCursor-1].char) {
cellCursor--
separators = true
}
if !separators {
for cellCursor > 0 && self.cells[cellCursor-1].char != "\n" && !strings.Contains(WHITESPACES+WORD_SEPARATORS, self.cells[cellCursor-1].char) {
cellCursor--
}
}
return self.cellCursorToContentCursor(cellCursor)
}
func (self *TextArea) MoveLeftWord() {
self.cursor = self.newCursorForMoveLeftWord()
}
func (self *TextArea) MoveRightWord() {
if self.atEnd() {
return
}
if self.atLineEnd() {
self.cursor++
return
}
cellCursor := self.contentCursorToCellCursor(self.cursor)
for cellCursor < len(self.cells) && (self.isSoftLineBreak(cellCursor) || strings.Contains(WHITESPACES, self.cells[cellCursor].char)) {
cellCursor++
}
separators := false
for cellCursor < len(self.cells) && strings.Contains(WORD_SEPARATORS, self.cells[cellCursor].char) {
cellCursor++
separators = true
}
if !separators {
for cellCursor < len(self.cells) && self.cells[cellCursor].char != "\n" && !strings.Contains(WHITESPACES+WORD_SEPARATORS, self.cells[cellCursor].char) {
cellCursor++
}
}
self.cursor = self.cellCursorToContentCursor(cellCursor)
}
func (self *TextArea) MoveCursorUp() {
x, y := self.GetCursorXY()
self.SetCursor2D(x, y-1)
}
func (self *TextArea) MoveCursorDown() {
x, y := self.GetCursorXY()
self.SetCursor2D(x, y+1)
}
func (self *TextArea) GetContent() string {
var b strings.Builder
for _, c := range self.cells {
b.WriteString(c.char)
}
return b.String()
}
func (self *TextArea) GetUnwrappedContent() string {
return self.content
}
func (self *TextArea) ToggleOverwrite() {
self.overwrite = !self.overwrite
}
func (self *TextArea) atEnd() bool {
return self.cursor == len(self.content)
}
func (self *TextArea) DeleteToStartOfLine() {
// copying vim's logic: if you're at the start of the line, you delete the newline
// character and go to the end of the previous line
if self.atLineStart() {
if self.cursor == 0 {
return
}
self.content = self.content[:self.cursor-1] + self.content[self.cursor:]
self.cursor--
self.updateCells()
return
}
// otherwise, if we're at a soft line start, skip left past the soft line
// break, so we'll end up deleting the previous line. This seems like the
// only reasonable behavior in this case, as you can't delete just the soft
// line break.
if self.atSoftLineStart() {
self.cursor--
}
// otherwise, you delete everything up to the start of the current line, without
// deleting the newline character
newlineIndex := self.closestNewlineOnLeft()
self.clipboard = self.content[newlineIndex+1 : self.cursor]
self.content = self.content[:newlineIndex+1] + self.content[self.cursor:]
self.updateCells()
self.cursor = newlineIndex + 1
}
func (self *TextArea) DeleteToEndOfLine() {
if self.atEnd() {
return
}
// if we're at the end of the line, delete just the newline character
if self.atLineEnd() {
self.content = self.content[:self.cursor] + self.content[self.cursor+1:]
self.updateCells()
return
}
// otherwise, if we're at a soft line end, skip right past the soft line
// break, so we'll end up deleting the next line. This seems like the
// only reasonable behavior in this case, as you can't delete just the soft
// line break.
if self.atSoftLineEnd() {
self.cursor++
}
lineEndIndex := self.closestNewlineOnRight()
self.clipboard = self.content[self.cursor:lineEndIndex]
self.content = self.content[:self.cursor] + self.content[lineEndIndex:]
self.updateCells()
}
func (self *TextArea) GoToStartOfLine() {
if self.atSoftLineStart() {
return
}
newlineIndex := self.closestNewlineOnLeft()
self.cursor = newlineIndex + 1
}
func (self *TextArea) closestNewlineOnLeft() int {
cellCursor := self.contentCursorToCellCursor(self.cursor)
newlineCellIndex := -1
for i, c := range self.cells[0:cellCursor] {
if c.char == "\n" {
newlineCellIndex = i
}
}
if newlineCellIndex == -1 {
return -1
}
newlineContentIndex := self.cells[newlineCellIndex].contentIndex
if self.content[newlineContentIndex] != '\n' {
newlineContentIndex--
}
return newlineContentIndex
}
func (self *TextArea) GoToEndOfLine() {
if self.atEnd() {
return
}
self.cursor = self.closestNewlineOnRight()
self.moveLeftFromSoftLineBreak()
}
func (self *TextArea) closestNewlineOnRight() int {
cellCursor := self.contentCursorToCellCursor(self.cursor)
for i, c := range self.cells[cellCursor:] {
if c.char == "\n" {
return self.cellCursorToContentCursor(cellCursor + i)
}
}
return len(self.content)
}
func (self *TextArea) moveLeftFromSoftLineBreak() {
// If the end of line is a soft line break, we need to move left by one so
// that we end up at the last whitespace before the line break. Otherwise
// we'd be at the start of the next line, since the newline character
// doesn't really exist in the real content.
if self.cursor < len(self.content) && self.content[self.cursor] != '\n' {
self.cursor--
}
}
func (self *TextArea) atLineStart() bool {
return self.cursor == 0 ||
(len(self.content) > self.cursor-1 && self.content[self.cursor-1] == '\n')
}
func (self *TextArea) isSoftLineBreak(cellCursor int) bool {
cell := self.cells[cellCursor]
return cell.char == "\n" && self.content[cell.contentIndex] != '\n'
}
func (self *TextArea) atSoftLineStart() bool {
cellCursor := self.contentCursorToCellCursor(self.cursor)
return cellCursor == 0 ||
(len(self.cells) > cellCursor-1 && self.cells[cellCursor-1].char == "\n")
}
func (self *TextArea) atLineEnd() bool {
return self.atEnd() ||
(len(self.content) > self.cursor && self.content[self.cursor] == '\n')
}
func (self *TextArea) atSoftLineEnd() bool {
cellCursor := self.contentCursorToCellCursor(self.cursor)
return cellCursor == len(self.cells) ||
(len(self.cells) > cellCursor+1 && self.cells[cellCursor+1].char == "\n")
}
func (self *TextArea) BackSpaceWord() {
newCursor := self.newCursorForMoveLeftWord()
if newCursor == self.cursor {
return
}
clipboard := self.content[newCursor:self.cursor]
if clipboard != "\n" {
self.clipboard = clipboard
}
self.content = self.content[:newCursor] + self.content[self.cursor:]
self.cursor = newCursor
self.updateCells()
}
func (self *TextArea) Yank() {
self.TypeString(self.clipboard)
}
func (self *TextArea) contentCursorToCellCursor(origCursor int) int {
idx, _ := slices.BinarySearchFunc(self.cells, origCursor, func(cell TextAreaCell, cursor int) int {
return cell.contentIndex - cursor
})
for idx < len(self.cells)-1 && self.cells[idx+1].contentIndex == origCursor {
idx++
}
return idx
}
func (self *TextArea) cellCursorToContentCursor(cellCursor int) int {
if cellCursor >= len(self.cells) {
return len(self.content)
}
return self.cells[cellCursor].contentIndex
}
func (self *TextArea) GetCursorXY() (int, int) {
if len(self.cells) == 0 {
return 0, 0
}
cellCursor := self.contentCursorToCellCursor(self.cursor)
if cellCursor >= len(self.cells) {
return self.cells[len(self.cells)-1].nextCursorXY()
}
if cellCursor > 0 && self.cells[cellCursor].char == "\n" {
return self.cells[cellCursor-1].nextCursorXY()
}
cell := self.cells[cellCursor]
return cell.x, cell.y
}
// takes an x,y position and maps it to a 1D cursor position
func (self *TextArea) SetCursor2D(x int, y int) {
if y < 0 {
y = 0
}
if x < 0 {
x = 0
}
newCursor := 0
for _, c := range self.cells {
if x <= 0 && y == 0 {
self.cursor = self.cellCursorToContentCursor(newCursor)
if self.cells[newCursor].char == "\n" {
self.moveLeftFromSoftLineBreak()
}
return
}
if c.char == "\n" {
if y == 0 {
self.cursor = self.cellCursorToContentCursor(newCursor)
self.moveLeftFromSoftLineBreak()
return
}
y--
} else if y == 0 {
x -= c.width
}
newCursor++
}
// if we weren't able to run-down our arg, the user is trying to move out of
// bounds so we'll just return
if y > 0 {
return
}
self.cursor = self.cellCursorToContentCursor(newCursor)
}
func (self *TextArea) Clear() {
self.content = ""
self.cells = nil
self.cursor = 0
}
func (self *TextArea) TypeString(str string) {
state := -1
for str != "" {
var chr string
chr, str, _, state = uniseg.FirstGraphemeClusterInString(str, state)
self.typeCharacter(chr)
}
self.updateCells()
}