-
-
Notifications
You must be signed in to change notification settings - Fork 575
/
textview.go
1446 lines (1303 loc) · 41.5 KB
/
textview.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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tview
import (
"math"
"strings"
"sync"
"github.com/gdamore/tcell/v2"
colorful "github.com/lucasb-eyer/go-colorful"
)
// TabSize is the number of spaces with which a tab character will be replaced.
var TabSize = 4
// textViewLine contains information about a line displayed in the text view.
type textViewLine struct {
offset int // The string position in the buffer where this line starts.
width int // The screen width of this line.
length int // The string length (in bytes) of this line.
state *stepState // The parser state at the beginning of the line, before parsing the first character.
regions map[string][2]int // The start and end columns of all regions in this line. Only valid for visible lines. May be nil.
}
// TextViewWriter is a writer that can be used to write to and clear a TextView
// in batches, i.e. multiple writes with the lock only being acquired once. Don't
// instantiated this class directly but use the TextView's BatchWriter method
// instead.
type TextViewWriter struct {
t *TextView
}
// Close implements io.Closer for the writer by unlocking the original TextView.
func (w TextViewWriter) Close() error {
w.t.Unlock()
return nil
}
// Clear removes all text from the buffer.
func (w TextViewWriter) Clear() {
w.t.clear()
}
// Write implements the io.Writer interface. It behaves like the TextView's
// Write() method except that it does not acquire the lock.
func (w TextViewWriter) Write(p []byte) (n int, err error) {
return w.t.write(p)
}
// HasFocus returns whether the underlying TextView has focus.
func (w TextViewWriter) HasFocus() bool {
return w.t.hasFocus
}
// TextView is a component to display read-only text. While the text to be
// displayed can be changed or appended to, there is no functionality that
// allows the user to edit it. For that, [TextArea] should be used.
//
// TextView implements the io.Writer interface so you can stream text to it,
// appending to the existing text. This does not trigger a redraw automatically
// but if a handler is installed via [TextView.SetChangedFunc], you can cause it
// to be redrawn. (See [TextView.SetChangedFunc] for more details.)
//
// Tab characters advance the text to the next tab stop at every [TabSize]
// screen columns, but only if the text is left-aligned. If the text is centered
// or right-aligned, tab characters are simply replaced with [TabSize] spaces.
//
// Word wrapping is enabled by default. Use [TextView.SetWrap] and
// [TextView.SetWordWrap] to change this.
//
// # Navigation
//
// If the text view is set to be scrollable (which is the default), text is kept
// in a buffer which may be larger than the screen and can be navigated
// with Vim-like key binds:
//
// - h, left arrow: Move left.
// - l, right arrow: Move right.
// - j, down arrow: Move down.
// - k, up arrow: Move up.
// - g, home: Move to the top.
// - G, end: Move to the bottom.
// - Ctrl-F, page down: Move down by one page.
// - Ctrl-B, page up: Move up by one page.
//
// If the text is not scrollable, any text above the top visible line is
// discarded. This can be useful when you want to continuously stream text to
// the text view and only keep the latest lines.
//
// Use [Box.SetInputCapture] to override or modify keyboard input.
//
// # Styles / Colors
//
// If dynamic colors are enabled via [TextView.SetDynamicColors], text style can
// be changed dynamically by embedding color strings in square brackets. This
// works the same way as anywhere else. See the package documentation for more
// information.
//
// # Regions and Highlights
//
// If regions are enabled via [TextView.SetRegions], you can define text regions
// within the text and assign region IDs to them. Text regions start with region
// tags. Region tags are square brackets that contain a region ID in double
// quotes, for example:
//
// We define a ["rg"]region[""] here.
//
// A text region ends with the next region tag. Tags with no region ID ([""])
// don't start new regions. They can therefore be used to mark the end of a
// region. Region IDs must satisfy the following regular expression:
//
// [a-zA-Z0-9_,;: \-\.]+
//
// Regions can be highlighted by calling the [TextView.Highlight] function with
// one or more region IDs. This can be used to display search results, for
// example.
//
// The [TextView.ScrollToHighlight] function can be used to jump to the
// currently highlighted region once when the text view is drawn the next time.
//
// # Large Texts
//
// The text view can handle reasonably large texts. It will parse the text as
// needed. For optimal performance, it is best to access or display parts of the
// text very far down only if really needed. For example, call
// [TextView.ScrollToBeginning] before adding the text to the text view, to
// avoid scrolling the text all the way to the bottom, forcing a full-text
// parse.
//
// For even larger texts or "infinite" streams of text such as log files, you
// should consider using [TextView.SetMaxLines] to limit the number of lines in
// the text view buffer. Or disable the text view's scrollability altogether
// (using [TextView.SetScrollable]). This will cause the text view to discard
// lines moving out of the visible area at the top.
//
// See https://github.com/rivo/tview/wiki/TextView for an example.
type TextView struct {
sync.Mutex
*Box
// The size of the text area. If set to 0, the text view will use the entire
// available space.
width, height int
// The text buffer.
text strings.Builder
// The line index. It is valid at any time but may not contain trailing
// lines which are not visible.
lineIndex []*textViewLine
// The screen width of the longest line in the index.
longestLine int
// Regions mapped by their ID to the line where they start. Regions which
// cannot be found in [TextView.lineIndex] are not contained.
regions map[string]int
// The label text shown, usually when part of a form.
label string
// The width of the text area's label.
labelWidth int
// The label style.
labelStyle tcell.Style
// The text alignment, one of AlignLeft, AlignCenter, or AlignRight.
align int
// Currently highlighted regions.
highlights map[string]struct{}
// The last width for which the current text view was drawn.
lastWidth int
// The height of the content the last time the text view was drawn.
pageSize int
// The index of the first line shown in the text view.
lineOffset int
// If set to true, the text view will always remain at the end of the
// content when text is added.
trackEnd bool
// The width of the characters to be skipped on each line (not used in wrap
// mode).
columnOffset int
// The maximum number of lines kept in the line index, effectively the
// latest word-wrapped lines. Ignored if 0.
maxLines int
// If set to true, the text view will keep a buffer of text which can be
// navigated when the text is longer than what fits into the box.
scrollable bool
// If set to true, lines that are longer than the available width are
// wrapped onto the next line. If set to false, any characters beyond the
// available width are discarded.
wrap bool
// If set to true and if wrap is also true, Unicode line breaking is
// applied.
wordWrap bool
// The (starting) style of the text. This also defines the background color
// of the main text element.
textStyle tcell.Style
// Whether or not style tags are used.
styleTags bool
// Whether or not region tags are used.
regionTags bool
// A temporary flag which, when true, will automatically bring the current
// highlight(s) into the visible screen the next time the text view is
// drawn.
scrollToHighlights bool
// If true, setting new highlights will be a XOR instead of an overwrite
// operation.
toggleHighlights bool
// An optional function which is called when the content of the text view
// has changed.
changed func()
// An optional function which is called when the user presses one of the
// following keys: Escape, Enter, Tab, Backtab.
done func(tcell.Key)
// An optional function which is called when one or more regions were
// highlighted.
highlighted func(added, removed, remaining []string)
// A callback function set by the Form class and called when the user leaves
// this form item.
finished func(tcell.Key)
}
// NewTextView returns a new text view.
func NewTextView() *TextView {
return &TextView{
Box: NewBox(),
labelStyle: tcell.StyleDefault.Foreground(Styles.SecondaryTextColor),
highlights: make(map[string]struct{}),
lineOffset: -1,
scrollable: true,
align: AlignLeft,
wrap: true,
wordWrap: true,
textStyle: tcell.StyleDefault.Background(Styles.PrimitiveBackgroundColor).Foreground(Styles.PrimaryTextColor),
regionTags: false,
styleTags: false,
}
}
// SetLabel sets the text to be displayed before the text view.
func (t *TextView) SetLabel(label string) *TextView {
t.label = label
return t
}
// GetLabel returns the text to be displayed before the text view.
func (t *TextView) GetLabel() string {
return t.label
}
// SetLabelWidth sets the screen width of the label. A value of 0 will cause the
// primitive to use the width of the label string.
func (t *TextView) SetLabelWidth(width int) *TextView {
t.labelWidth = width
return t
}
// SetSize sets the screen size of the main text element of the text view. This
// element is always located next to the label which is always located in the
// top left corner. If any of the values are 0 or larger than the available
// space, the available space will be used.
func (t *TextView) SetSize(rows, columns int) *TextView {
t.width = columns
t.height = rows
return t
}
// GetFieldWidth returns this primitive's field width.
func (t *TextView) GetFieldWidth() int {
return t.width
}
// GetFieldHeight returns this primitive's field height.
func (t *TextView) GetFieldHeight() int {
return t.height
}
// SetDisabled sets whether or not the item is disabled / read-only.
func (t *TextView) SetDisabled(disabled bool) FormItem {
return t // Text views are always read-only.
}
// SetScrollable sets the flag that decides whether or not the text view is
// scrollable. If false, text that moves above the text view's top row will be
// permanently deleted.
func (t *TextView) SetScrollable(scrollable bool) *TextView {
t.scrollable = scrollable
if !scrollable {
t.trackEnd = true
}
return t
}
// SetWrap sets the flag that, if true, leads to lines that are longer than the
// available width being wrapped onto the next line. If false, any characters
// beyond the available width are not displayed.
func (t *TextView) SetWrap(wrap bool) *TextView {
if t.wrap != wrap {
t.resetIndex() // This invalidates the entire index.
}
t.wrap = wrap
return t
}
// SetWordWrap sets the flag that, if true and if the "wrap" flag is also true
// (see [TextView.SetWrap]), wraps according to [Unicode Standard Annex #14].
//
// This flag is ignored if the "wrap" flag is false.
func (t *TextView) SetWordWrap(wrapOnWords bool) *TextView {
if t.wrap && t.wordWrap != wrapOnWords {
t.resetIndex() // This invalidates the entire index.
}
t.wordWrap = wrapOnWords
return t
}
// SetMaxLines sets the maximum number of lines for this text view. Lines at the
// beginning of the text will be discarded when the text view is drawn, so as to
// remain below this value. Only lines above the first visible line are removed.
//
// Broken-over lines via word/character wrapping are counted individually.
//
// Note that [TextView.GetText] will return the shortened text.
//
// A value of 0 (the default) will keep all lines in place.
func (t *TextView) SetMaxLines(maxLines int) *TextView {
t.maxLines = maxLines
return t
}
// SetTextAlign sets the text alignment within the text view. This must be
// either AlignLeft, AlignCenter, or AlignRight.
func (t *TextView) SetTextAlign(align int) *TextView {
t.align = align
return t
}
// SetTextColor sets the initial color of the text.
func (t *TextView) SetTextColor(color tcell.Color) *TextView {
t.textStyle = t.textStyle.Foreground(color)
t.resetIndex()
return t
}
// SetBackgroundColor overrides its implementation in Box to set the background
// color of this primitive. For backwards compatibility reasons, it also sets
// the background color of the main text element.
func (t *TextView) SetBackgroundColor(color tcell.Color) *Box {
t.Box.SetBackgroundColor(color)
t.textStyle = t.textStyle.Background(color)
t.resetIndex()
return t.Box
}
// SetTextStyle sets the initial style of the text. This style's background
// color also determines the background color of the main text element.
func (t *TextView) SetTextStyle(style tcell.Style) *TextView {
t.textStyle = style
t.resetIndex()
return t
}
// SetText sets the text of this text view to the provided string. Previously
// contained text will be removed. As with writing to the text view io.Writer
// interface directly, this does not trigger an automatic redraw but it will
// trigger the "changed" callback if one is set.
func (t *TextView) SetText(text string) *TextView {
t.Lock()
defer t.Unlock()
t.text.Reset()
t.text.WriteString(text)
t.resetIndex()
if t.changed != nil {
go t.changed()
}
return t
}
// GetText returns the current text of this text view. If "stripAllTags" is set
// to true, any region/style tags are stripped from the text. Note that any text
// that has been discarded due to [TextView.SetMaxLines] or
// [TextView.SetScrollable] will not be part of the returned text.
func (t *TextView) GetText(stripAllTags bool) string {
if !stripAllTags || (!t.styleTags && !t.regionTags) {
return t.text.String()
}
var (
str strings.Builder
state *stepState
text = t.text.String()
opts stepOptions
ch string
)
if t.styleTags {
opts = stepOptionsStyle
}
if t.regionTags {
opts |= stepOptionsRegion
}
for len(text) > 0 {
ch, text, state = step(text, state, opts)
str.WriteString(ch)
}
return str.String()
}
// GetOriginalLineCount returns the number of lines in the original text buffer,
// without applying any wrapping. This is an expensive call as it needs to
// iterate over the entire text. Note that any text that has been discarded due
// to [TextView.SetMaxLines] or [TextView.SetScrollable] will not be part of the
// count.
func (t *TextView) GetOriginalLineCount() int {
if t.text.Len() == 0 {
return 0
}
var (
state *stepState
str = t.text.String()
lines int = 1
)
for len(str) > 0 {
_, str, state = step(str, state, stepOptionsNone)
if lineBreak, optional := state.LineBreak(); lineBreak && !optional {
lines++
}
}
return lines
}
// GetWrappedLineCount returns the number of lines in the text view, taking
// wrapping into account (if activated). This is an even more expensive call
// than [TextView.GetOriginalLineCount] as it needs to parse the text until the
// end and calculate the line breaks. It will also allocate memory for each
// line. Note that any text that has been discarded due to
// [TextView.SetMaxLines] or [TextView.SetScrollable] will not be part of the
// count. Calling this method before the text view was drawn for the first time
// will assume no wrapping.
func (t *TextView) GetWrappedLineCount() int {
t.parseAhead(t.width, func(int, *textViewLine) bool {
return false
})
return len(t.lineIndex)
}
// SetDynamicColors sets the flag that allows the text color to be changed
// dynamically with style tags. See class description for details.
func (t *TextView) SetDynamicColors(dynamic bool) *TextView {
if t.styleTags != dynamic {
t.resetIndex() // This invalidates the entire index.
}
t.styleTags = dynamic
return t
}
// SetRegions sets the flag that allows to define regions in the text. See class
// description for details.
func (t *TextView) SetRegions(regions bool) *TextView {
if t.regionTags != regions {
t.resetIndex() // This invalidates the entire index.
}
t.regionTags = regions
return t
}
// SetChangedFunc sets a handler function which is called when the text of the
// text view has changed. This is useful when text is written to this
// [io.Writer] in a separate goroutine. Doing so does not automatically cause
// the screen to be refreshed so you may want to use the "changed" handler to
// redraw the screen.
//
// Note that to avoid race conditions or deadlocks, there are a few rules you
// should follow:
//
// - You can call [Application.Draw] from this handler.
// - You can call [TextView.HasFocus] from this handler.
// - During the execution of this handler, access to any other variables from
// this primitive or any other primitive must be queued using
// [Application.QueueUpdate].
//
// See package description for details on dealing with concurrency.
func (t *TextView) SetChangedFunc(handler func()) *TextView {
t.changed = handler
return t
}
// SetDoneFunc sets a handler which is called when the user presses on the
// following keys: Escape, Enter, Tab, Backtab. The key is passed to the
// handler.
func (t *TextView) SetDoneFunc(handler func(key tcell.Key)) *TextView {
t.done = handler
return t
}
// SetHighlightedFunc sets a handler which is called when the list of currently
// highlighted regions change. It receives a list of region IDs which were newly
// highlighted, those that are not highlighted anymore, and those that remain
// highlighted.
//
// Note that because regions are only determined when drawing the text view,
// this function can only fire for regions that have existed when the text view
// was last drawn.
func (t *TextView) SetHighlightedFunc(handler func(added, removed, remaining []string)) *TextView {
t.highlighted = handler
return t
}
// SetFinishedFunc sets a callback invoked when the user leaves this form item.
func (t *TextView) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
t.finished = handler
return t
}
// SetFormAttributes sets attributes shared by all form items.
func (t *TextView) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
t.labelWidth = labelWidth
t.backgroundColor = bgColor
t.labelStyle = t.labelStyle.Foreground(labelColor)
// We ignore the field background color because this is a read-only element.
t.textStyle = tcell.StyleDefault.Foreground(fieldTextColor).Background(bgColor)
return t
}
// ScrollTo scrolls to the specified row and column (both starting with 0).
func (t *TextView) ScrollTo(row, column int) *TextView {
if !t.scrollable {
return t
}
t.lineOffset = row
t.columnOffset = column
t.trackEnd = false
return t
}
// ScrollToBeginning scrolls to the top left corner of the text if the text view
// is scrollable.
func (t *TextView) ScrollToBeginning() *TextView {
if !t.scrollable {
return t
}
t.trackEnd = false
t.lineOffset = 0
t.columnOffset = 0
return t
}
// ScrollToEnd scrolls to the bottom left corner of the text if the text view
// is scrollable. Adding new rows to the end of the text view will cause it to
// scroll with the new data.
func (t *TextView) ScrollToEnd() *TextView {
if !t.scrollable {
return t
}
t.trackEnd = true
t.columnOffset = 0
return t
}
// GetScrollOffset returns the number of rows and columns that are skipped at
// the top left corner when the text view has been scrolled.
func (t *TextView) GetScrollOffset() (row, column int) {
return t.lineOffset, t.columnOffset
}
// Clear removes all text from the buffer. This triggers the "changed" callback.
func (t *TextView) Clear() *TextView {
t.Lock()
defer t.Unlock()
t.clear()
if t.changed != nil {
go t.changed()
}
return t
}
// clear is the internal implementation of clear. It is used by TextViewWriter
// and anywhere that we need to perform a write without locking the buffer.
func (t *TextView) clear() {
t.text.Reset()
t.resetIndex()
}
// Highlight specifies which regions should be highlighted. If highlight
// toggling is set to true (see [TextView.SetToggleHighlights]), the highlight
// of the provided regions is toggled (i.e. highlighted regions are
// un-highlighted and vice versa). If toggling is set to false, the provided
// regions are highlighted and all other regions will not be highlighted (you
// may also provide nil to turn off all highlights).
//
// For more information on regions, see class description. Empty region strings
// or regions not contained in the text are ignored.
//
// Text in highlighted regions will be drawn inverted, i.e. with their
// background and foreground colors swapped.
//
// If toggling is set to false, clicking outside of any region will remove all
// highlights.
//
// This function is expensive if a specified region is in a part of the text
// that has not yet been parsed.
func (t *TextView) Highlight(regionIDs ...string) *TextView {
// Make sure we know these regions.
t.parseAhead(t.lastWidth, func(lineNumber int, line *textViewLine) bool {
for _, regionID := range regionIDs {
if _, ok := t.regions[regionID]; !ok {
return false
}
}
return true
})
// Remove unknown regions.
newRegions := make([]string, 0, len(regionIDs))
for _, regionID := range regionIDs {
if _, ok := t.regions[regionID]; ok {
newRegions = append(newRegions, regionID)
}
}
regionIDs = newRegions
// Toggle highlights.
if t.toggleHighlights {
var newIDs []string
HighlightLoop:
for regionID := range t.highlights {
for _, id := range regionIDs {
if regionID == id {
continue HighlightLoop
}
}
newIDs = append(newIDs, regionID)
}
for _, regionID := range regionIDs {
if _, ok := t.highlights[regionID]; !ok {
newIDs = append(newIDs, regionID)
}
}
regionIDs = newIDs
} // Now we have a list of region IDs that end up being highlighted.
// Determine added and removed regions.
var added, removed, remaining []string
if t.highlighted != nil {
for _, regionID := range regionIDs {
if _, ok := t.highlights[regionID]; ok {
remaining = append(remaining, regionID)
delete(t.highlights, regionID)
} else {
added = append(added, regionID)
}
}
for regionID := range t.highlights {
removed = append(removed, regionID)
}
}
// Make new selection.
t.highlights = make(map[string]struct{})
for _, id := range regionIDs {
if id == "" {
continue
}
t.highlights[id] = struct{}{}
}
// Notify.
if t.highlighted != nil && (len(added) > 0 || len(removed) > 0) {
t.highlighted(added, removed, remaining)
}
return t
}
// GetHighlights returns the IDs of all currently highlighted regions.
func (t *TextView) GetHighlights() (regionIDs []string) {
for id := range t.highlights {
regionIDs = append(regionIDs, id)
}
return
}
// SetToggleHighlights sets a flag to determine how regions are highlighted.
// When set to true, the [TextView.Highlight] function (or a mouse click) will
// toggle the provided/selected regions. When set to false, [TextView.Highlight]
// (or a mouse click) will simply highlight the provided regions.
func (t *TextView) SetToggleHighlights(toggle bool) *TextView {
t.toggleHighlights = toggle
return t
}
// ScrollToHighlight will cause the visible area to be scrolled so that the
// highlighted regions appear in the visible area of the text view. This
// repositioning happens the next time the text view is drawn. It happens only
// once so you will need to call this function repeatedly to always keep
// highlighted regions in view.
//
// Nothing happens if there are no highlighted regions or if the text view is
// not scrollable.
func (t *TextView) ScrollToHighlight() *TextView {
if len(t.highlights) == 0 || !t.scrollable || !t.regionTags {
return t
}
t.scrollToHighlights = true
t.trackEnd = false
return t
}
// GetRegionText returns the text of the first region with the given ID. If
// dynamic colors are enabled, style tags are stripped from the text.
//
// If the region does not exist or if regions are turned off, an empty string
// is returned.
//
// This function can be expensive if the specified region is way beyond the
// visible area of the text view as the text needs to be parsed until the region
// can be found, or if the region does not contain any text.
func (t *TextView) GetRegionText(regionID string) string {
if !t.regionTags || regionID == "" {
return ""
}
// Parse until we find the region.
lineNumber, ok := t.regions[regionID]
if !ok {
lineNumber = -1
t.parseAhead(t.lastWidth, func(number int, line *textViewLine) bool {
lineNumber, ok = t.regions[regionID]
return ok
})
if lineNumber < 0 {
return "" // We couldn't find this region.
}
}
// Extract text from region.
var (
line = t.lineIndex[lineNumber]
text = t.text.String()[line.offset:]
st = *line.state
state = &st
options = stepOptionsRegion
regionText strings.Builder
)
if t.styleTags {
options |= stepOptionsStyle
}
for len(text) > 0 {
var ch string
ch, text, state = step(text, state, options)
if state.region == regionID {
regionText.WriteString(ch)
} else if regionText.Len() > 0 {
break
}
}
return regionText.String()
}
// Focus is called when this primitive receives focus.
func (t *TextView) Focus(delegate func(p Primitive)) {
// Implemented here with locking because this is used by layout primitives.
t.Lock()
defer t.Unlock()
// But if we're part of a form and not scrollable, there's nothing the user
// can do here so we're finished.
if t.finished != nil && !t.scrollable {
t.finished(-1)
return
}
t.Box.Focus(delegate)
}
// HasFocus returns whether or not this primitive has focus.
func (t *TextView) HasFocus() bool {
// Implemented here with locking because this may be used in the "changed"
// callback.
t.Lock()
defer t.Unlock()
return t.Box.HasFocus()
}
// Write lets us implement the io.Writer interface.
func (t *TextView) Write(p []byte) (n int, err error) {
t.Lock()
defer t.Unlock()
return t.write(p)
}
// write is the internal implementation of Write. It is used by [TextViewWriter]
// and anywhere that we need to perform a write without locking the buffer.
func (t *TextView) write(p []byte) (n int, err error) {
// Notify at the end.
changed := t.changed
if changed != nil {
defer func() {
// We always call the "changed" function in a separate goroutine to avoid
// deadlocks.
go changed()
}()
}
return t.text.Write(p)
}
// BatchWriter returns a new writer that can be used to write into the buffer
// but without Locking/Unlocking the buffer on every write, as [TextView.Write]
// and [TextView.Clear] do. The lock will be acquired once when BatchWriter is
// called, and will be released when the returned writer is closed. Example:
//
// tv := tview.NewTextView()
// w := tv.BatchWriter()
// defer w.Close()
// w.Clear()
// fmt.Fprintln(w, "To sit in solemn silence")
// fmt.Fprintln(w, "on a dull, dark, dock")
// fmt.Println(tv.GetText(false))
//
// Note that using the batch writer requires you to manage any issues that may
// arise from concurrency yourself. See package description for details on
// dealing with concurrency.
func (t *TextView) BatchWriter() TextViewWriter {
t.Lock()
return TextViewWriter{
t: t,
}
}
// resetIndex resets all indexed data, including the line index.
func (t *TextView) resetIndex() {
t.lineIndex = nil
t.regions = make(map[string]int)
t.longestLine = 0
}
// parseAhead parses the text buffer starting at the last line in
// [TextView.lineIndex] until either the end of the buffer or until stop returns
// true for the last complete line that was parsed. If wrapping is enabled,
// width will be used as the available screen width. If width is 0, it is
// assumed that there is no wrapping. This can happen when this function is
// called before the first time [TextView.Draw] is called.
//
// There is no guarantee that stop will ever be called.
//
// The function adds entries to the [TextView.lineIndex] slice and the
// [TextView.regions] map and adjusts [TextView.longestLine].
func (t *TextView) parseAhead(width int, stop func(lineNumber int, line *textViewLine) bool) {
if t.text.Len() == 0 {
return // No text. Nothing to parse.
}
// If width is 0, make it infinite.
if width == 0 {
width = math.MaxInt
}
// What kind of tags do we scan for?
var options stepOptions
if t.styleTags {
options |= stepOptionsStyle
}
if t.regionTags {
options |= stepOptionsRegion
}
// Start parsing at the last line in the index.
var lastLine *textViewLine
str := t.text.String()
if len(t.lineIndex) == 0 {
// Insert the first line.
lastLine = &textViewLine{
state: &stepState{
unisegState: -1,
style: t.textStyle,
},
}
t.lineIndex = append(t.lineIndex, lastLine)
} else {
// Reset the last line.
lastLine = t.lineIndex[len(t.lineIndex)-1]
lastLine.width = 0
lastLine.length = 0
str = str[lastLine.offset:]
}
// Parse.
var (
lastOption int // Text index of the last optional split point.
lastOptionWidth int // Line width at last optional split point.
lastOptionState *stepState // State at last optional split point.
leftPos int // The current position in the line (only for left-alignment).
offset = lastLine.offset // Text index of the current position.
st = *lastLine.state // Current state.
state = &st // Pointer to current state.
)
for len(str) > 0 {
var c string
region := state.region
c, str, state = step(str, state, options)
w := state.Width()
if c == "\t" {
if t.align == AlignLeft {
w = TabSize - leftPos%TabSize
} else {
w = TabSize
}
}
length := state.GrossLength()
// Would it exceed the line width?
if t.wrap && lastLine.width+w > width {
if lastOptionWidth == 0 {
// No split point so far. Just split at the current position.
if stop(len(t.lineIndex)-1, lastLine) {
return
}
st := *state
lastLine = &textViewLine{
offset: offset,
state: &st,
}
lastOption, lastOptionWidth, leftPos = 0, 0, 0
} else {
// Split at the last split point.
newLine := &textViewLine{
offset: lastLine.offset + lastOption,
width: lastLine.width - lastOptionWidth,
length: lastLine.length - lastOption,
state: lastOptionState,
}
lastLine.width = lastOptionWidth
lastLine.length = lastOption
if stop(len(t.lineIndex)-1, lastLine) {
return
}
lastLine = newLine
lastOption, lastOptionWidth = 0, 0
leftPos -= lastOptionWidth
}
t.lineIndex = append(t.lineIndex, lastLine)
}
// Move ahead.
lastLine.width += w
lastLine.length += length
offset += length
leftPos += w
// Do we have a new longest line?
if lastLine.width > t.longestLine {
t.longestLine = lastLine.width
}
// Check for split points.
if lineBreak, optional := state.LineBreak(); lineBreak {
if optional {
if t.wrap && t.wordWrap {
// Remember this split point.
lastOption = offset - lastLine.offset
lastOptionWidth = lastLine.width
st := *state
lastOptionState = &st
}
} else {
// We must split here.
if stop(len(t.lineIndex)-1, lastLine) {
return
}
st := *state
lastLine = &textViewLine{
offset: offset,
state: &st,
}
t.lineIndex = append(t.lineIndex, lastLine)
lastOption, lastOptionWidth, leftPos = 0, 0, 0