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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
|
package cview
import (
"bytes"
"regexp"
"sync"
"unicode"
"unicode/utf8"
"github.com/gdamore/tcell/v2"
"github.com/lucasb-eyer/go-colorful"
"github.com/mattn/go-runewidth"
"github.com/rivo/uniseg"
)
var (
openColorRegex = regexp.MustCompile(`\[([a-zA-Z]*|#[0-9a-zA-Z]*)$`)
openRegionRegex = regexp.MustCompile(`\["[a-zA-Z0-9_,;: \-\.]*"?$`)
newLineRegex = regexp.MustCompile(`\r?\n`)
// TabSize is the number of spaces with which a tab character will be replaced.
TabSize = 4
)
// textViewIndex contains information about each line displayed in the text
// view.
type textViewIndex struct {
Line int // The index into the "buffer" variable.
Pos int // The index into the "buffer" line ([]byte position).
NextPos int // The (byte) index of the next character in this buffer line.
Width int // The screen width of this line.
ForegroundColor string // The starting foreground color ("" = don't change, "-" = reset).
BackgroundColor string // The starting background color ("" = don't change, "-" = reset).
Attributes string // The starting attributes ("" = don't change, "-" = reset).
Region []byte // The starting region ID.
}
// textViewRegion contains information about a region.
type textViewRegion struct {
// The region ID.
ID []byte
// The starting and end screen position of the region as determined the last
// time Draw() was called. A negative value indicates out-of-rect positions.
FromX, FromY, ToX, ToY int
}
// TextView is a box which displays text. It implements the io.Writer interface
// so you can stream text to it. This does not trigger a redraw automatically
// but if a handler is installed via SetChangedFunc(), you can cause it to be
// redrawn. (See SetChangedFunc() for more details.)
//
// Navigation
//
// If the text view is scrollable (the default), text is kept in a buffer which
// may be larger than the screen and can be navigated similarly to Vim:
//
// - 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.
//
// Use SetInputCapture() to override or modify keyboard input.
//
// Colors
//
// If dynamic colors are enabled via SetDynamicColors(), text color can be
// changed dynamically by embedding color strings in square brackets. This works
// the same way as anywhere else. Please see the package documentation for more
// information.
//
// Regions and Highlights
//
// If regions are enabled via 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 Highlight() function with one or
// more region IDs. This can be used to display search results, for example.
//
// The ScrollToHighlight() function can be used to jump to the currently
// highlighted region once when the text view is drawn the next time.
type TextView struct {
*Box
// The text buffer.
buffer [][]byte
// The last bytes that have been received but are not part of the buffer yet.
recentBytes []byte
// The last width and height of the text view.
lastWidth, lastHeight int
// The processed line index. This is nil if the buffer has changed and needs
// to be re-indexed.
index []*textViewIndex
// The width of the text view buffer index.
indexWidth int
// If set to true, the buffer will be reindexed each time it is modified.
reindex bool
// The text alignment, one of AlignLeft, AlignCenter, or AlignRight.
align int
// Information about visible regions as of the last call to Draw().
regionInfos []*textViewRegion
// Indices into the "index" slice which correspond to the first line of the
// first highlight and the last line of the last highlight. This is calculated
// during re-indexing. Set to -1 if there is no current highlight.
fromHighlight, toHighlight int
// The screen space column of the highlight in its first line. Set to -1 if
// there is no current highlight.
posHighlight int
// A set of region IDs that are currently highlighted.
highlights map[string]struct{}
// The screen width of the longest line in the index (not the buffer).
longestLine int
// The index of the first line shown in the text view.
lineOffset int
// The maximum number of newlines the text view will hold (0 = unlimited).
maxLines int
// If set to true, the text view will always remain at the end of the content.
trackEnd bool
// The number of characters to be skipped on each line (not in wrap mode).
columnOffset int
// The height of the content the last time the text view was drawn.
pageSize 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
// Visibility of the scroll bar.
scrollBarVisibility ScrollBarVisibility
// The scroll bar color.
scrollBarColor tcell.Color
// 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
// The maximum line width when wrapping (0 = use TextView width).
wrapWidth int
// If set to true and if wrap is also true, lines are split at spaces or
// after punctuation characters.
wordWrap bool
// The (starting) color of the text.
textColor tcell.Color
// If set to true, the text color can be changed dynamically by piping color
// strings in square brackets to the text view.
dynamicColors bool
// If set to true, region tags can be used to define regions.
regions bool
// A temporary flag which, when true, will automatically bring the current
// highlight(s) into the visible screen.
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)
sync.RWMutex
}
// NewTextView returns a new text view.
func NewTextView() *TextView {
return &TextView{
Box: NewBox(),
highlights: make(map[string]struct{}),
lineOffset: -1,
reindex: true,
scrollable: true,
scrollBarVisibility: ScrollBarAuto,
scrollBarColor: Styles.ScrollBarColor,
align: AlignLeft,
wrap: true,
textColor: Styles.PrimaryTextColor,
}
}
// SetScrollable sets the flag that decides whether or not the text view is
// scrollable. If true, text is kept in a buffer and can be navigated. If false,
// the last line will always be visible.
func (t *TextView) SetScrollable(scrollable bool) {
t.Lock()
defer t.Unlock()
t.scrollable = scrollable
if !scrollable {
t.trackEnd = true
}
}
// SetScrollBarVisibility specifies the display of the scroll bar.
func (t *TextView) SetScrollBarVisibility(visibility ScrollBarVisibility) {
t.Lock()
defer t.Unlock()
t.scrollBarVisibility = visibility
}
// SetScrollBarColor sets the color of the scroll bar.
func (t *TextView) SetScrollBarColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.scrollBarColor = color
}
// 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) {
t.Lock()
defer t.Unlock()
if t.wrap != wrap {
t.index = nil
}
t.wrap = wrap
}
// SetWordWrap sets the flag that, if true and if the "wrap" flag is also true
// (see SetWrap()), wraps the line at spaces or after punctuation marks. Note
// that trailing spaces will not be printed.
//
// This flag is ignored if the "wrap" flag is false.
func (t *TextView) SetWordWrap(wrapOnWords bool) {
t.Lock()
defer t.Unlock()
if t.wordWrap != wrapOnWords {
t.index = nil
}
t.wordWrap = wrapOnWords
}
// SetTextAlign sets the text alignment within the text view. This must be
// either AlignLeft, AlignCenter, or AlignRight.
func (t *TextView) SetTextAlign(align int) {
t.Lock()
defer t.Unlock()
if t.align != align {
t.index = nil
}
t.align = align
}
// SetTextColor sets the initial color of the text (which can be changed
// dynamically by sending color strings in square brackets to the text view if
// dynamic colors are enabled).
func (t *TextView) SetTextColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.textColor = color
}
// SetBytes sets the text of this text view to the provided byte slice.
// Previously contained text will be removed.
func (t *TextView) SetBytes(text []byte) {
t.Lock()
defer t.Unlock()
t.clear()
t.write(text)
}
// SetText sets the text of this text view to the provided string. Previously
// contained text will be removed.
func (t *TextView) SetText(text string) {
t.SetBytes([]byte(text))
}
// GetBytes returns the current text of this text view. If "stripTags" is set
// to true, any region/color tags are stripped from the text.
func (t *TextView) GetBytes(stripTags bool) []byte {
t.RLock()
defer t.RUnlock()
if !stripTags {
if len(t.recentBytes) > 0 {
return bytes.Join(append(t.buffer, t.recentBytes), []byte("\n"))
}
return bytes.Join(t.buffer, []byte("\n"))
}
buffer := bytes.Join(t.buffer, []byte("\n"))
return StripTags(buffer, t.dynamicColors, t.regions)
}
// GetText returns the current text of this text view. If "stripTags" is set
// to true, any region/color tags are stripped from the text.
func (t *TextView) GetText(stripTags bool) string {
return string(t.GetBytes(stripTags))
}
// GetBufferSize returns the number of lines and the length of the longest line
// in the text buffer. The screen size of the widget is available via GetRect.
func (t *TextView) GetBufferSize() (rows int, maxLen int) {
t.RLock()
defer t.RUnlock()
return len(t.buffer), t.longestLine
}
// SetDynamicColors sets the flag that allows the text color to be changed
// dynamically. See class description for details.
func (t *TextView) SetDynamicColors(dynamic bool) {
t.Lock()
defer t.Unlock()
if t.dynamicColors != dynamic {
t.index = nil
}
t.dynamicColors = dynamic
}
// SetRegions sets the flag that allows to define regions in the text. See class
// description for details.
func (t *TextView) SetRegions(regions bool) {
t.Lock()
defer t.Unlock()
if t.regions != regions {
t.index = nil
}
t.regions = regions
}
// 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 should be queued using
// Application.QueueUpdate().
//
// See package description for details on dealing with concurrency.
func (t *TextView) SetChangedFunc(handler func()) {
t.Lock()
defer t.Unlock()
t.changed = handler
}
// 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)) {
t.Lock()
defer t.Unlock()
t.done = handler
}
// 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 during drawing, this function
// can only fire for regions that have existed during the last call to Draw().
func (t *TextView) SetHighlightedFunc(handler func(added, removed, remaining []string)) {
t.highlighted = handler
}
func (t *TextView) clipBuffer() {
if t.maxLines <= 0 {
return
}
lenbuf := len(t.buffer)
if lenbuf > t.maxLines {
t.buffer = t.buffer[lenbuf-t.maxLines:]
}
}
// SetMaxLines sets the maximum number of newlines the text view will hold
// before discarding older data from the buffer.
func (t *TextView) SetMaxLines(maxLines int) {
t.maxLines = maxLines
t.clipBuffer()
}
// ScrollTo scrolls to the specified row and column (both starting with 0).
func (t *TextView) ScrollTo(row, column int) {
t.Lock()
defer t.Unlock()
if !t.scrollable {
return
}
t.lineOffset = row
t.columnOffset = column
t.trackEnd = false
}
// ScrollToBeginning scrolls to the top left corner of the text if the text view
// is scrollable.
func (t *TextView) ScrollToBeginning() {
t.Lock()
defer t.Unlock()
if !t.scrollable {
return
}
t.trackEnd = false
t.lineOffset = 0
t.columnOffset = 0
}
// 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() {
t.Lock()
defer t.Unlock()
if !t.scrollable {
return
}
t.trackEnd = true
t.columnOffset = 0
}
// 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) {
t.RLock()
defer t.RUnlock()
return t.lineOffset, t.columnOffset
}
// Clear removes all text from the buffer.
func (t *TextView) Clear() {
t.Lock()
defer t.Unlock()
t.clear()
}
func (t *TextView) clear() {
t.buffer = nil
t.recentBytes = nil
if t.reindex {
t.index = nil
}
}
// Highlight specifies which regions should be highlighted. If highlight
// toggling is set to true (see SetToggleHighlights()), the highlight of the
// provided regions is toggled (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
// are ignored.
//
// Text in highlighted regions will be drawn inverted, i.e. with their
// background and foreground colors swapped.
func (t *TextView) Highlight(regionIDs ...string) {
t.Lock()
// 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{}{}
}
t.index = nil
// Notify.
if t.highlighted != nil && (len(added) > 0 || len(removed) > 0) {
t.Unlock()
t.highlighted(added, removed, remaining)
} else {
t.Unlock()
}
}
// GetHighlights returns the IDs of all currently highlighted regions.
func (t *TextView) GetHighlights() (regionIDs []string) {
t.RLock()
defer t.RUnlock()
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 Highlight() function (or a mouse click) will toggle the
// provided/selected regions. When set to false, Highlight() (or a mouse click)
// will simply highlight the provided regions.
func (t *TextView) SetToggleHighlights(toggle bool) {
t.toggleHighlights = toggle
}
// 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() {
t.Lock()
defer t.Unlock()
if len(t.highlights) == 0 || !t.scrollable || !t.regions {
return
}
t.index = nil
t.scrollToHighlights = true
t.trackEnd = false
}
// GetRegionText returns the text of the region with the given ID. If dynamic
// colors are enabled, color tags are stripped from the text. Newlines are
// always returned as '\n' runes.
//
// If the region does not exist or if regions are turned off, an empty string
// is returned.
func (t *TextView) GetRegionText(regionID string) string {
t.RLock()
defer t.RUnlock()
if !t.regions || len(regionID) == 0 {
return ""
}
var (
buffer bytes.Buffer
currentRegionID string
)
for _, str := range t.buffer {
// Find all color tags in this line.
var colorTagIndices [][]int
if t.dynamicColors {
colorTagIndices = colorPattern.FindAllIndex(str, -1)
}
// Find all regions in this line.
var (
regionIndices [][]int
regions [][][]byte
)
if t.regions {
regionIndices = regionPattern.FindAllIndex(str, -1)
regions = regionPattern.FindAllSubmatch(str, -1)
}
// Analyze this line.
var currentTag, currentRegion int
for pos, ch := range str {
// Skip any color tags.
if currentTag < len(colorTagIndices) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
if pos == colorTagIndices[currentTag][1]-1 {
currentTag++
}
if colorTagIndices[currentTag][1]-colorTagIndices[currentTag][0] > 2 {
continue
}
}
// Skip any regions.
if currentRegion < len(regionIndices) && pos >= regionIndices[currentRegion][0] && pos < regionIndices[currentRegion][1] {
if pos == regionIndices[currentRegion][1]-1 {
if currentRegionID == regionID {
// This is the end of the requested region. We're done.
return buffer.String()
}
currentRegionID = string(regions[currentRegion][1])
currentRegion++
}
continue
}
// Add this rune.
if currentRegionID == regionID {
buffer.WriteByte(ch)
}
}
// Add newline.
if currentRegionID == regionID {
buffer.WriteRune('\n')
}
}
return escapePattern.ReplaceAllString(buffer.String(), `[$1$2]`)
}
// Focus is called when this primitive receives focus.
func (t *TextView) Focus(delegate func(p Primitive)) {
t.Lock()
defer t.Unlock()
// Implemented here with locking because this is used by layout primitives.
t.hasFocus = true
}
// HasFocus returns whether or not this primitive has focus.
func (t *TextView) HasFocus() bool {
t.RLock()
defer t.RUnlock()
// Implemented here with locking because this may be used in the "changed"
// callback.
return t.hasFocus
}
// Write lets us implement the io.Writer interface. Tab characters will be
// replaced with TabSize space characters. A "\n" or "\r\n" will be interpreted
// as a new line.
func (t *TextView) Write(p []byte) (n int, err error) {
t.Lock()
changed := t.changed
if changed != nil {
// Notify at the end.
defer changed()
}
defer t.Unlock()
return t.write(p)
}
func (t *TextView) write(p []byte) (n int, err error) {
// Copy data over.
newBytes := append(t.recentBytes, p...)
t.recentBytes = nil
// If we have a trailing invalid UTF-8 byte, we'll wait.
if r, _ := utf8.DecodeLastRune(p); r == utf8.RuneError {
t.recentBytes = newBytes
return len(p), nil
}
// If we have a trailing open dynamic color, exclude it.
if t.dynamicColors {
location := openColorRegex.FindIndex(newBytes)
if location != nil {
t.recentBytes = newBytes[location[0]:]
newBytes = newBytes[:location[0]]
}
}
// If we have a trailing open region, exclude it.
if t.regions {
location := openRegionRegex.FindIndex(newBytes)
if location != nil {
t.recentBytes = newBytes[location[0]:]
newBytes = newBytes[:location[0]]
}
}
// Transform the new bytes into strings.
newBytes = bytes.Replace(newBytes, []byte{'\t'}, bytes.Repeat([]byte{' '}, TabSize), -1)
for index, line := range bytes.Split(newBytes, []byte("\n")) {
if index == 0 {
if len(t.buffer) == 0 {
t.buffer = [][]byte{line}
} else {
t.buffer[len(t.buffer)-1] = append(t.buffer[len(t.buffer)-1], line...)
}
} else {
t.buffer = append(t.buffer, line)
}
}
t.clipBuffer()
// Reset the index.
if t.reindex {
t.index = nil
}
return len(p), nil
}
// SetWrapWidth set the maximum width of lines when wrapping is enabled.
// When set to 0 the width of the TextView is used.
func (t *TextView) SetWrapWidth(width int) {
t.Lock()
defer t.Unlock()
t.wrapWidth = width
}
// SetReindexBuffer set a flag controlling whether the buffer is reindexed when
// it is modified. This improves the performance of TextViews whose contents
// always have line-breaks in the same location. This must be called after the
// buffer has been indexed.
func (t *TextView) SetReindexBuffer(reindex bool) {
t.Lock()
defer t.Unlock()
t.reindex = reindex
if reindex {
t.index = nil
}
}
// reindexBuffer re-indexes the buffer such that we can use it to easily draw
// the buffer onto the screen. Each line in the index will contain a pointer
// into the buffer from which on we will print text. It will also contain the
// color with which the line starts.
func (t *TextView) reindexBuffer(width int) {
if t.index != nil && (!t.wrap || width == t.indexWidth) {
return // Nothing has changed. We can still use the current index.
}
t.index = nil
t.indexWidth = width
t.fromHighlight, t.toHighlight, t.posHighlight = -1, -1, -1
// If there's no space, there's no index.
if width < 1 {
return
}
if t.wrapWidth > 0 && t.wrapWidth < width {
width = t.wrapWidth
}
// Initial states.
var regionID []byte
var (
highlighted bool
foregroundColor, backgroundColor, attributes string
)
// Go through each line in the buffer.
for bufferIndex, buf := range t.buffer {
colorTagIndices, colorTags, regionIndices, regions, escapeIndices, strippedStr, _ := decomposeText(buf, t.dynamicColors, t.regions)
// Split the line if required.
var splitLines []string
str := string(strippedStr)
if t.wrap && len(str) > 0 {
for len(str) > 0 {
extract := runewidth.Truncate(str, width, "")
if len(extract) == 0 {
// We'll extract at least one grapheme cluster.
gr := uniseg.NewGraphemes(str)
gr.Next()
_, to := gr.Positions()
extract = str[:to]
}
if t.wordWrap && len(extract) < len(str) {
// Add any spaces from the next line.
if spaces := spacePattern.FindStringIndex(str[len(extract):]); spaces != nil && spaces[0] == 0 {
extract = str[:len(extract)+spaces[1]]
}
// Can we split before the mandatory end?
matches := boundaryPattern.FindAllStringIndex(extract, -1)
if len(matches) > 0 {
// Yes. Let's split there.
extract = extract[:matches[len(matches)-1][1]]
}
}
splitLines = append(splitLines, extract)
str = str[len(extract):]
}
} else {
// No need to split the line.
splitLines = []string{str}
}
// Create index from split lines.
var originalPos, colorPos, regionPos, escapePos int
for _, splitLine := range splitLines {
line := &textViewIndex{
Line: bufferIndex,
Pos: originalPos,
ForegroundColor: foregroundColor,
BackgroundColor: backgroundColor,
Attributes: attributes,
Region: regionID,
}
// Shift original position with tags.
lineLength := len(splitLine)
remainingLength := lineLength
tagEnd := originalPos
totalTagLength := 0
for {
// Which tag comes next?
nextTag := make([][3]int, 0, 3)
if colorPos < len(colorTagIndices) {
nextTag = append(nextTag, [3]int{colorTagIndices[colorPos][0], colorTagIndices[colorPos][1], 0}) // 0 = color tag.
}
if regionPos < len(regionIndices) {
nextTag = append(nextTag, [3]int{regionIndices[regionPos][0], regionIndices[regionPos][1], 1}) // 1 = region tag.
}
if escapePos < len(escapeIndices) {
nextTag = append(nextTag, [3]int{escapeIndices[escapePos][0], escapeIndices[escapePos][1], 2}) // 2 = escape tag.
}
minPos := -1
tagIndex := -1
for index, pair := range nextTag {
if minPos < 0 || pair[0] < minPos {
minPos = pair[0]
tagIndex = index
}
}
// Is the next tag in range?
if tagIndex < 0 || minPos > tagEnd+remainingLength {
break // No. We're done with this line.
}
// Advance.
strippedTagStart := nextTag[tagIndex][0] - originalPos - totalTagLength
tagEnd = nextTag[tagIndex][1]
tagLength := tagEnd - nextTag[tagIndex][0]
if nextTag[tagIndex][2] == 2 {
tagLength = 1
}
totalTagLength += tagLength
remainingLength = lineLength - (tagEnd - originalPos - totalTagLength)
// Process the tag.
switch nextTag[tagIndex][2] {
case 0:
// Process color tags.
foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[colorPos])
colorPos++
case 1:
// Process region tags.
regionID = regions[regionPos][1]
_, highlighted = t.highlights[string(regionID)]
// Update highlight range.
if highlighted {
line := len(t.index)
if t.fromHighlight < 0 {
t.fromHighlight, t.toHighlight = line, line
t.posHighlight = runewidth.StringWidth(splitLine[:strippedTagStart])
} else if line > t.toHighlight {
t.toHighlight = line
}
}
regionPos++
case 2:
// Process escape tags.
escapePos++
}
}
// Advance to next line.
originalPos += lineLength + totalTagLength
// Append this line.
line.NextPos = originalPos
line.Width = runewidth.StringWidth(splitLine)
t.index = append(t.index, line)
}
// Word-wrapped lines may have trailing whitespace. Remove it.
if t.wrap && t.wordWrap {
for _, line := range t.index {
str := t.buffer[line.Line][line.Pos:line.NextPos]
trimmed := bytes.TrimRightFunc(str, unicode.IsSpace)
if len(trimmed) != len(str) {
oldNextPos := line.NextPos
line.NextPos -= len(str) - len(trimmed)
line.Width -= runewidth.StringWidth(string(t.buffer[line.Line][line.NextPos:oldNextPos]))
}
}
}
}
// Calculate longest line.
t.longestLine = 0
for _, line := range t.index {
if line.Width > t.longestLine {
t.longestLine = line.Width
}
}
}
// Draw draws this primitive onto the screen.
func (t *TextView) Draw(screen tcell.Screen) {
if !t.GetVisible() {
return
}
t.Box.Draw(screen)
t.Lock()
defer t.Unlock()
// Get the available size.
x, y, width, height := t.GetInnerRect()
if height == 0 {
return
}
t.pageSize = height
if t.index == nil || width != t.lastWidth || height != t.lastHeight {
t.reindexBuffer(width)
}
t.lastWidth, t.lastHeight = width, height
showVerticalScrollBar := t.scrollBarVisibility == ScrollBarAlways || (t.scrollBarVisibility == ScrollBarAuto && len(t.index) > height)
if showVerticalScrollBar {
width-- // Subtract space for scroll bar.
}
t.reindexBuffer(width)
if t.regions {
t.regionInfos = nil
}
// If we don't have an index, there's nothing to draw.
if t.index == nil {
return
}
// Move to highlighted regions.
if t.regions && t.scrollToHighlights && t.fromHighlight >= 0 {
// Do we fit the entire height?
if t.toHighlight-t.fromHighlight+1 < height {
// Yes, let's center the highlights.
t.lineOffset = (t.fromHighlight + t.toHighlight - height) / 2
} else {
// No, let's move to the start of the highlights.
t.lineOffset = t.fromHighlight
}
// If the highlight is too far to the right, move it to the middle.
if t.posHighlight-t.columnOffset > 3*width/4 {
t.columnOffset = t.posHighlight - width/2
}
// If the highlight is offscreen on the left, move it onscreen.
if t.posHighlight-t.columnOffset < 0 {
t.columnOffset = t.posHighlight - width/4
}
}
t.scrollToHighlights = false
// Adjust line offset.
if t.lineOffset+height > len(t.index) {
t.trackEnd = true
}
if t.trackEnd {
t.lineOffset = len(t.index) - height
}
if t.lineOffset < 0 {
t.lineOffset = 0
}
// Adjust column offset.
if t.align == AlignLeft {
if t.columnOffset+width > t.longestLine {
t.columnOffset = t.longestLine - width
}
if t.columnOffset < 0 {
t.columnOffset = 0
}
} else if t.align == AlignRight {
if t.columnOffset-width < -t.longestLine {
t.columnOffset = width - t.longestLine
}
if t.columnOffset > 0 {
t.columnOffset = 0
}
} else { // AlignCenter.
half := (t.longestLine - width) / 2
if half > 0 {
if t.columnOffset > half {
t.columnOffset = half
}
if t.columnOffset < -half {
t.columnOffset = -half
}
} else {
t.columnOffset = 0
}
}
// Draw the buffer.
defaultStyle := tcell.StyleDefault.Foreground(t.textColor)
for line := t.lineOffset; line < len(t.index); line++ {
// Are we done?
if line-t.lineOffset >= height {
break
}
// Get the text for this line.
index := t.index[line]
text := t.buffer[index.Line][index.Pos:index.NextPos]
foregroundColor := index.ForegroundColor
backgroundColor := index.BackgroundColor
attributes := index.Attributes
regionID := index.Region
if t.regions && len(regionID) > 0 && (len(t.regionInfos) == 0 || !bytes.Equal(t.regionInfos[len(t.regionInfos)-1].ID, regionID)) {
t.regionInfos = append(t.regionInfos, &textViewRegion{
ID: regionID,
FromX: x,
FromY: y + line - t.lineOffset,
ToX: -1,
ToY: -1,
})
}
// Process tags.
colorTagIndices, colorTags, regionIndices, regions, escapeIndices, strippedText, _ := decomposeText(text, t.dynamicColors, t.regions)
// Calculate the position of the line.
var skip, posX int
if t.align == AlignLeft {
posX = -t.columnOffset
} else if t.align == AlignRight {
posX = width - index.Width - t.columnOffset
} else { // AlignCenter.
posX = (width-index.Width)/2 - t.columnOffset
}
if posX < 0 {
skip = -posX
posX = 0
}
// Print the line.
if y+line-t.lineOffset >= 0 {
var colorPos, regionPos, escapePos, tagOffset, skipped int
iterateString(string(strippedText), func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool {
// Process tags.
for {
if colorPos < len(colorTags) && textPos+tagOffset >= colorTagIndices[colorPos][0] && textPos+tagOffset < colorTagIndices[colorPos][1] {
// Get the color.
foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[colorPos])
tagOffset += colorTagIndices[colorPos][1] - colorTagIndices[colorPos][0]
colorPos++
} else if regionPos < len(regionIndices) && textPos+tagOffset >= regionIndices[regionPos][0] && textPos+tagOffset < regionIndices[regionPos][1] {
// Get the region.
if len(regionID) > 0 && len(t.regionInfos) > 0 && bytes.Equal(t.regionInfos[len(t.regionInfos)-1].ID, regionID) {
// End last region.
t.regionInfos[len(t.regionInfos)-1].ToX = x + posX
t.regionInfos[len(t.regionInfos)-1].ToY = y + line - t.lineOffset
}
regionID = regions[regionPos][1]
if len(regionID) > 0 {
// Start new region.
t.regionInfos = append(t.regionInfos, &textViewRegion{
ID: regionID,
FromX: x + posX,
FromY: y + line - t.lineOffset,
ToX: -1,
ToY: -1,
})
}
tagOffset += regionIndices[regionPos][1] - regionIndices[regionPos][0]
regionPos++
} else {
break
}
}
// Skip the second-to-last character of an escape tag.
if escapePos < len(escapeIndices) && textPos+tagOffset == escapeIndices[escapePos][1]-2 {
tagOffset++
escapePos++
}
// Mix the existing style with the new style.
_, _, existingStyle, _ := screen.GetContent(x+posX, y+line-t.lineOffset)
_, background, _ := existingStyle.Decompose()
style := overlayStyle(background, defaultStyle, foregroundColor, backgroundColor, attributes)
// Do we highlight this character?
var highlighted bool
if len(regionID) > 0 {
if _, ok := t.highlights[string(regionID)]; ok {
highlighted = true
}
}
if highlighted {
fg, bg, _ := style.Decompose()
if bg == tcell.ColorDefault {
r, g, b := fg.RGB()
c := colorful.Color{R: float64(r) / 255, G: float64(g) / 255, B: float64(b) / 255}
_, _, li := c.Hcl()
if li < .5 {
bg = tcell.ColorWhite.TrueColor()
} else {
bg = tcell.ColorBlack.TrueColor()
}
}
style = style.Background(fg).Foreground(bg)
}
// Skip to the right.
if !t.wrap && skipped < skip {
skipped += screenWidth
return false
}
// Stop at the right border.
if posX+screenWidth > width {
return true
}
// Draw the character.
for offset := screenWidth - 1; offset >= 0; offset-- {
if offset == 0 {
screen.SetContent(x+posX+offset, y+line-t.lineOffset, main, comb, style)
} else {
screen.SetContent(x+posX+offset, y+line-t.lineOffset, ' ', nil, style)
}
}
// Advance.
posX += screenWidth
return false
})
}
}
// Draw scroll bar.
if showVerticalScrollBar {
cursor := int(float64(len(t.index)) * (float64(t.lineOffset) / float64(len(t.index)-height)))
for printed := 0; printed < height; printed++ {
RenderScrollBar(screen, t.scrollBarVisibility, x+width, y+printed, height, len(t.index), cursor, printed, t.hasFocus, t.scrollBarColor)
}
}
// If this view is not scrollable, we'll purge the buffer of lines that have
// scrolled out of view.
if !t.scrollable && t.lineOffset > 0 {
if t.lineOffset >= len(t.index) {
t.buffer = nil
} else {
t.buffer = t.buffer[t.index[t.lineOffset].Line:]
}
t.index = nil
t.lineOffset = 0
}
}
// InputHandler returns the handler for this primitive.
func (t *TextView) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return t.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
key := event.Key()
if HitShortcut(event, Keys.Cancel, Keys.Select, Keys.Select2, Keys.MovePreviousField, Keys.MoveNextField) {
if t.done != nil {
t.done(key)
}
return
}
t.Lock()
defer t.Unlock()
if !t.scrollable {
return
}
if HitShortcut(event, Keys.MoveFirst, Keys.MoveFirst2) {
t.trackEnd = false
t.lineOffset = 0
t.columnOffset = 0
} else if HitShortcut(event, Keys.MoveLast, Keys.MoveLast2) {
t.trackEnd = true
t.columnOffset = 0
} else if HitShortcut(event, Keys.MoveUp, Keys.MoveUp2) {
t.trackEnd = false
t.lineOffset--
} else if HitShortcut(event, Keys.MoveDown, Keys.MoveDown2) {
t.lineOffset++
} else if HitShortcut(event, Keys.MoveLeft, Keys.MoveLeft2) {
t.columnOffset--
} else if HitShortcut(event, Keys.MoveRight, Keys.MoveRight2) {
t.columnOffset++
} else if HitShortcut(event, Keys.MovePreviousPage) {
t.trackEnd = false
t.lineOffset -= t.pageSize
} else if HitShortcut(event, Keys.MoveNextPage) {
t.lineOffset += t.pageSize
}
})
}
// MouseHandler returns the mouse handler for this primitive.
func (t *TextView) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return t.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
x, y := event.Position()
if !t.InRect(x, y) {
return false, nil
}
switch action {
case MouseLeftClick:
if t.regions {
// Find a region to highlight.
for _, region := range t.regionInfos {
if y == region.FromY && x < region.FromX ||
y == region.ToY && x >= region.ToX ||
region.FromY >= 0 && y < region.FromY ||
region.ToY >= 0 && y > region.ToY {
continue
}
t.Highlight(string(region.ID))
break
}
}
consumed = true
setFocus(t)
case MouseScrollUp:
if t.scrollable {
t.trackEnd = false
t.lineOffset--
consumed = true
}
case MouseScrollDown:
if t.scrollable {
t.lineOffset++
consumed = true
}
}
return
})
}
|