| 1 |
|
| 2 |
|
| 3 |
package mauview |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"regexp" |
| 9 |
"sync" |
| 10 |
"unicode/utf8" |
| 11 |
|
| 12 |
"github.com/mattn/go-runewidth" |
| 13 |
|
| •14 |
"github.com/gdamore/tcell/v2" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
var TabSize = 4 |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
type textViewIndex struct { |
| 23 |
Line int |
| 24 |
Pos int |
| 25 |
NextPos int |
| 26 |
Width int |
| 27 |
ForegroundColor string |
| 28 |
BackgroundColor string |
| 29 |
Attributes string |
| 30 |
Region string |
| 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 |
type TextView struct { |
| 87 |
sync.Mutex |
| 88 |
|
| 89 |
|
| 90 |
buffer []string |
| 91 |
|
| 92 |
|
| 93 |
recentBytes []byte |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
index []*textViewIndex |
| 98 |
|
| 99 |
|
| 100 |
align int |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
fromHighlight, toHighlight int |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
posHighlight int |
| 110 |
|
| 111 |
|
| 112 |
highlights map[string]struct{} |
| 113 |
|
| 114 |
|
| 115 |
lastWidth int |
| 116 |
|
| 117 |
|
| 118 |
longestLine int |
| 119 |
|
| 120 |
|
| 121 |
lineOffset int |
| 122 |
|
| 123 |
|
| 124 |
trackEnd bool |
| 125 |
|
| 126 |
|
| 127 |
columnOffset int |
| 128 |
|
| 129 |
|
| 130 |
pageSize int |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
scrollable bool |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
wrap bool |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
wordWrap bool |
| 144 |
|
| 145 |
baseStyle tcell.Style |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
dynamicColors bool |
| 150 |
|
| 151 |
|
| 152 |
regions bool |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
scrollToHighlights bool |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
changed func() |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
done func(tcell.Key) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
func NewTextView() *TextView { |
| 169 |
return &TextView{ |
| 170 |
highlights: make(map[string]struct{}), |
| 171 |
lineOffset: -1, |
| 172 |
scrollable: true, |
| 173 |
align: AlignLeft, |
| 174 |
wrap: true, |
| 175 |
baseStyle: tcell.StyleDefault.Foreground(Styles.PrimaryTextColor), |
| 176 |
regions: false, |
| 177 |
dynamicColors: false, |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
func (t *TextView) SetScrollable(scrollable bool) *TextView { |
| 184 |
t.scrollable = scrollable |
| 185 |
if !scrollable { |
| 186 |
t.trackEnd = true |
| 187 |
} |
| 188 |
return t |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
func (t *TextView) SetWrap(wrap bool) *TextView { |
| 195 |
if t.wrap != wrap { |
| 196 |
t.index = nil |
| 197 |
} |
| 198 |
t.wrap = wrap |
| 199 |
return t |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
func (t *TextView) SetWordWrap(wrapOnWords bool) *TextView { |
| 208 |
if t.wordWrap != wrapOnWords { |
| 209 |
t.index = nil |
| 210 |
} |
| 211 |
t.wordWrap = wrapOnWords |
| 212 |
return t |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
func (t *TextView) SetTextAlign(align int) *TextView { |
| 218 |
if t.align != align { |
| 219 |
t.index = nil |
| 220 |
} |
| 221 |
t.align = align |
| 222 |
return t |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
func (t *TextView) SetTextColor(color tcell.Color) *TextView { |
| 229 |
t.baseStyle = t.baseStyle.Foreground(color) |
| 230 |
return t |
| 231 |
} |
| 232 |
|
| 233 |
func (t *TextView) SetBackgroundColor(color tcell.Color) *TextView { |
| 234 |
t.baseStyle = t.baseStyle.Background(color) |
| 235 |
return t |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
func (t *TextView) SetText(text string) *TextView { |
| 241 |
t.Clear() |
| 242 |
fmt.Fprint(t, text) |
| 243 |
return t |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
func (t *TextView) SetDynamicColors(dynamic bool) *TextView { |
| 249 |
if t.dynamicColors != dynamic { |
| 250 |
t.index = nil |
| 251 |
} |
| 252 |
t.dynamicColors = dynamic |
| 253 |
return t |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
func (t *TextView) SetRegions(regions bool) *TextView { |
| 259 |
if t.regions != regions { |
| 260 |
t.index = nil |
| 261 |
} |
| 262 |
t.regions = regions |
| 263 |
return t |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
func (t *TextView) SetChangedFunc(handler func()) *TextView { |
| 282 |
t.changed = handler |
| 283 |
return t |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
func (t *TextView) SetDoneFunc(handler func(key tcell.Key)) *TextView { |
| 290 |
t.done = handler |
| 291 |
return t |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
func (t *TextView) ScrollTo(row, column int) *TextView { |
| 296 |
if !t.scrollable { |
| 297 |
return t |
| 298 |
} |
| 299 |
t.lineOffset = row |
| 300 |
t.columnOffset = column |
| 301 |
return t |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
func (t *TextView) ScrollToBeginning() *TextView { |
| 307 |
if !t.scrollable { |
| 308 |
return t |
| 309 |
} |
| 310 |
t.trackEnd = false |
| 311 |
t.lineOffset = 0 |
| 312 |
t.columnOffset = 0 |
| 313 |
return t |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
func (t *TextView) ScrollToEnd() *TextView { |
| 320 |
if !t.scrollable { |
| 321 |
return t |
| 322 |
} |
| 323 |
t.trackEnd = true |
| 324 |
t.columnOffset = 0 |
| 325 |
return t |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
func (t *TextView) GetScrollOffset() (row, column int) { |
| 331 |
return t.lineOffset, t.columnOffset |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
func (t *TextView) Clear() *TextView { |
| 336 |
t.buffer = nil |
| 337 |
t.recentBytes = nil |
| 338 |
t.index = nil |
| 339 |
return t |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
func (t *TextView) Highlight(regionIDs ...string) *TextView { |
| 351 |
t.highlights = make(map[string]struct{}) |
| 352 |
for _, id := range regionIDs { |
| 353 |
if id == "" { |
| 354 |
continue |
| 355 |
} |
| 356 |
t.highlights[id] = struct{}{} |
| 357 |
} |
| 358 |
t.index = nil |
| 359 |
return t |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
func (t *TextView) GetHighlights() (regionIDs []string) { |
| 364 |
for id := range t.highlights { |
| 365 |
regionIDs = append(regionIDs, id) |
| 366 |
} |
| 367 |
return |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
func (t *TextView) ScrollToHighlight() *TextView { |
| 379 |
if len(t.highlights) == 0 || !t.scrollable || !t.regions { |
| 380 |
return t |
| 381 |
} |
| 382 |
t.index = nil |
| 383 |
t.scrollToHighlights = true |
| 384 |
t.trackEnd = false |
| 385 |
return t |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
func (t *TextView) GetRegionText(regionID string) string { |
| 395 |
if !t.regions || regionID == "" { |
| 396 |
return "" |
| 397 |
} |
| 398 |
|
| 399 |
var ( |
| 400 |
buffer bytes.Buffer |
| 401 |
currentRegionID string |
| 402 |
) |
| 403 |
|
| 404 |
for _, str := range t.buffer { |
| 405 |
|
| 406 |
var colorTagIndices [][]int |
| 407 |
if t.dynamicColors { |
| 408 |
colorTagIndices = colorPattern.FindAllStringIndex(str, -1) |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
var ( |
| 413 |
regionIndices [][]int |
| 414 |
regions [][]string |
| 415 |
) |
| 416 |
if t.regions { |
| 417 |
regionIndices = regionPattern.FindAllStringIndex(str, -1) |
| 418 |
regions = regionPattern.FindAllStringSubmatch(str, -1) |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
var currentTag, currentRegion int |
| 423 |
for pos, ch := range str { |
| 424 |
|
| 425 |
if currentTag < len(colorTagIndices) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] { |
| 426 |
if pos == colorTagIndices[currentTag][1]-1 { |
| 427 |
currentTag++ |
| 428 |
} |
| 429 |
continue |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
if currentRegion < len(regionIndices) && pos >= regionIndices[currentRegion][0] && pos < regionIndices[currentRegion][1] { |
| 434 |
if pos == regionIndices[currentRegion][1]-1 { |
| 435 |
if currentRegionID == regionID { |
| 436 |
|
| 437 |
return buffer.String() |
| 438 |
} |
| 439 |
currentRegionID = regions[currentRegion][1] |
| 440 |
currentRegion++ |
| 441 |
} |
| 442 |
continue |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
if currentRegionID == regionID { |
| 447 |
buffer.WriteRune(ch) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
if currentRegionID == regionID { |
| 453 |
buffer.WriteRune('\n') |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
return escapePattern.ReplaceAllString(buffer.String(), `[$1$2]`) |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
func (t *TextView) Write(p []byte) (n int, err error) { |
| 464 |
|
| 465 |
t.Lock() |
| 466 |
changed := t.changed |
| 467 |
t.Unlock() |
| 468 |
if changed != nil { |
| 469 |
defer changed() |
| 470 |
} |
| 471 |
|
| 472 |
t.Lock() |
| 473 |
defer t.Unlock() |
| 474 |
|
| 475 |
|
| 476 |
newBytes := append(t.recentBytes, p...) |
| 477 |
t.recentBytes = nil |
| 478 |
|
| 479 |
|
| 480 |
if r, _ := utf8.DecodeLastRune(p); r == utf8.RuneError { |
| 481 |
t.recentBytes = newBytes |
| 482 |
return len(p), nil |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
if t.dynamicColors { |
| 487 |
openColor := regexp.MustCompile(`\[([a-zA-Z]*|#[0-9a-zA-Z]*)$`) |
| 488 |
location := openColor.FindIndex(newBytes) |
| 489 |
if location != nil { |
| 490 |
t.recentBytes = newBytes[location[0]:] |
| 491 |
newBytes = newBytes[:location[0]] |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
if t.regions { |
| 497 |
openRegion := regexp.MustCompile(`\["[a-zA-Z0-9_,;: \-\.]*"?$`) |
| 498 |
location := openRegion.FindIndex(newBytes) |
| 499 |
if location != nil { |
| 500 |
t.recentBytes = newBytes[location[0]:] |
| 501 |
newBytes = newBytes[:location[0]] |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
newLine := regexp.MustCompile(`\r?\n`) |
| 507 |
newBytes = bytes.Replace(newBytes, []byte{'\t'}, bytes.Repeat([]byte{' '}, TabSize), -1) |
| 508 |
for index, line := range newLine.Split(string(newBytes), -1) { |
| 509 |
if index == 0 { |
| 510 |
if len(t.buffer) == 0 { |
| 511 |
t.buffer = []string{line} |
| 512 |
} else { |
| 513 |
t.buffer[len(t.buffer)-1] += line |
| 514 |
} |
| 515 |
} else { |
| 516 |
t.buffer = append(t.buffer, line) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
t.index = nil |
| 522 |
|
| 523 |
return len(p), nil |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
func (t *TextView) reindexBuffer(width int) { |
| 531 |
if t.index != nil { |
| 532 |
return |
| 533 |
} |
| 534 |
t.index = nil |
| 535 |
t.fromHighlight, t.toHighlight, t.posHighlight = -1, -1, -1 |
| 536 |
|
| 537 |
|
| 538 |
if width < 1 { |
| 539 |
return |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
regionID := "" |
| 544 |
var highlighted bool |
| 545 |
|
| 546 |
|
| 547 |
for bufferIndex, str := range t.buffer { |
| 548 |
|
| 549 |
var ( |
| 550 |
colorTagIndices [][]int |
| 551 |
colorTags [][]string |
| 552 |
escapeIndices [][]int |
| 553 |
) |
| 554 |
strippedStr := str |
| 555 |
if t.dynamicColors { |
| 556 |
colorTagIndices, colorTags, _, _, escapeIndices, strippedStr, _ = decomposeString(str, true, false) |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
var ( |
| 561 |
regionIndices [][]int |
| 562 |
regions [][]string |
| 563 |
) |
| 564 |
if t.regions { |
| 565 |
regionIndices = regionPattern.FindAllStringIndex(str, -1) |
| 566 |
regions = regionPattern.FindAllStringSubmatch(str, -1) |
| 567 |
strippedStr = regionPattern.ReplaceAllString(strippedStr, "") |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
str = strippedStr |
| 572 |
|
| 573 |
|
| 574 |
var splitLines []string |
| 575 |
if t.wrap && len(str) > 0 { |
| 576 |
for len(str) > 0 { |
| 577 |
extract := runewidth.Truncate(str, width, "") |
| 578 |
if t.wordWrap && len(extract) < len(str) { |
| 579 |
|
| 580 |
if spaces := spacePattern.FindStringIndex(str[len(extract):]); spaces != nil && spaces[0] == 0 { |
| 581 |
extract = str[:len(extract)+spaces[1]] |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
matches := boundaryPattern.FindAllStringIndex(extract, -1) |
| 586 |
if len(matches) > 0 { |
| 587 |
|
| 588 |
extract = extract[:matches[len(matches)-1][1]] |
| 589 |
} |
| 590 |
} |
| 591 |
splitLines = append(splitLines, extract) |
| 592 |
str = str[len(extract):] |
| 593 |
} |
| 594 |
} else { |
| 595 |
|
| 596 |
splitLines = []string{str} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
var ( |
| 601 |
originalPos, colorPos, regionPos, escapePos int |
| 602 |
foregroundColor, backgroundColor, attributes string |
| 603 |
) |
| 604 |
for _, splitLine := range splitLines { |
| 605 |
line := &textViewIndex{ |
| 606 |
Line: bufferIndex, |
| 607 |
Pos: originalPos, |
| 608 |
ForegroundColor: foregroundColor, |
| 609 |
BackgroundColor: backgroundColor, |
| 610 |
Attributes: attributes, |
| 611 |
Region: regionID, |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
lineLength := len(splitLine) |
| 616 |
remainingLength := lineLength |
| 617 |
tagEnd := originalPos |
| 618 |
totalTagLength := 0 |
| 619 |
for { |
| 620 |
|
| 621 |
nextTag := make([][3]int, 0, 3) |
| 622 |
if colorPos < len(colorTagIndices) { |
| 623 |
nextTag = append(nextTag, [3]int{colorTagIndices[colorPos][0], colorTagIndices[colorPos][1], 0}) |
| 624 |
} |
| 625 |
if regionPos < len(regionIndices) { |
| 626 |
nextTag = append(nextTag, [3]int{regionIndices[regionPos][0], regionIndices[regionPos][1], 1}) |
| 627 |
} |
| 628 |
if escapePos < len(escapeIndices) { |
| 629 |
nextTag = append(nextTag, [3]int{escapeIndices[escapePos][0], escapeIndices[escapePos][1], 2}) |
| 630 |
} |
| 631 |
minPos := -1 |
| 632 |
tagIndex := -1 |
| 633 |
for index, pair := range nextTag { |
| 634 |
if minPos < 0 || pair[0] < minPos { |
| 635 |
minPos = pair[0] |
| 636 |
tagIndex = index |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
if tagIndex < 0 || minPos >= tagEnd+remainingLength { |
| 642 |
break |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
strippedTagStart := nextTag[tagIndex][0] - originalPos - totalTagLength |
| 647 |
tagEnd = nextTag[tagIndex][1] |
| 648 |
tagLength := tagEnd - nextTag[tagIndex][0] |
| 649 |
if nextTag[tagIndex][2] == 2 { |
| 650 |
tagLength = 1 |
| 651 |
} |
| 652 |
totalTagLength += tagLength |
| 653 |
remainingLength = lineLength - (tagEnd - originalPos - totalTagLength) |
| 654 |
|
| 655 |
|
| 656 |
switch nextTag[tagIndex][2] { |
| 657 |
case 0: |
| 658 |
|
| 659 |
foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[colorPos]) |
| 660 |
colorPos++ |
| 661 |
case 1: |
| 662 |
|
| 663 |
regionID = regions[regionPos][1] |
| 664 |
_, highlighted = t.highlights[regionID] |
| 665 |
|
| 666 |
|
| 667 |
if highlighted { |
| 668 |
line := len(t.index) |
| 669 |
if t.fromHighlight < 0 { |
| 670 |
t.fromHighlight, t.toHighlight = line, line |
| 671 |
t.posHighlight = runewidth.StringWidth(splitLine[:strippedTagStart]) |
| 672 |
} else if line > t.toHighlight { |
| 673 |
t.toHighlight = line |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
regionPos++ |
| 678 |
case 2: |
| 679 |
|
| 680 |
escapePos++ |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
originalPos += lineLength + totalTagLength |
| 686 |
|
| 687 |
|
| 688 |
line.NextPos = originalPos |
| 689 |
line.Width = runewidth.StringWidth(splitLine) |
| 690 |
t.index = append(t.index, line) |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
if t.wrap && t.wordWrap { |
| 695 |
for _, line := range t.index { |
| 696 |
str := t.buffer[line.Line][line.Pos:line.NextPos] |
| 697 |
spaces := spacePattern.FindAllStringIndex(str, -1) |
| 698 |
if spaces != nil && spaces[len(spaces)-1][1] == len(str) { |
| 699 |
oldNextPos := line.NextPos |
| 700 |
line.NextPos -= spaces[len(spaces)-1][1] - spaces[len(spaces)-1][0] |
| 701 |
line.Width -= runewidth.StringWidth(t.buffer[line.Line][line.NextPos:oldNextPos]) |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
t.longestLine = 0 |
| 709 |
for _, line := range t.index { |
| 710 |
if line.Width > t.longestLine { |
| 711 |
t.longestLine = line.Width |
| 712 |
} |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
func (t *TextView) Draw(screen Screen) { |
| 718 |
t.Lock() |
| 719 |
defer t.Unlock() |
| 720 |
|
| 721 |
screen.SetStyle(t.baseStyle) |
| 722 |
screen.Clear() |
| 723 |
|
| 724 |
|
| 725 |
width, height := screen.Size() |
| 726 |
t.pageSize = height |
| 727 |
|
| 728 |
|
| 729 |
if width != t.lastWidth && t.wrap { |
| 730 |
t.index = nil |
| 731 |
} |
| 732 |
t.lastWidth = width |
| 733 |
|
| 734 |
|
| 735 |
t.reindexBuffer(width) |
| 736 |
|
| 737 |
|
| 738 |
if t.index == nil { |
| 739 |
return |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
if t.regions && t.scrollToHighlights && t.fromHighlight >= 0 { |
| 744 |
|
| 745 |
if t.toHighlight-t.fromHighlight+1 < height { |
| 746 |
|
| 747 |
t.lineOffset = (t.fromHighlight + t.toHighlight - height) / 2 |
| 748 |
} else { |
| 749 |
|
| 750 |
t.lineOffset = t.fromHighlight |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
if t.posHighlight-t.columnOffset > 3*width/4 { |
| 755 |
t.columnOffset = t.posHighlight - width/2 |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
if t.posHighlight-t.columnOffset < 0 { |
| 760 |
t.columnOffset = t.posHighlight - width/4 |
| 761 |
} |
| 762 |
} |
| 763 |
t.scrollToHighlights = false |
| 764 |
|
| 765 |
|
| 766 |
if t.lineOffset+height > len(t.index) { |
| 767 |
t.trackEnd = true |
| 768 |
} |
| 769 |
if t.trackEnd { |
| 770 |
t.lineOffset = len(t.index) - height |
| 771 |
} |
| 772 |
if t.lineOffset < 0 { |
| 773 |
t.lineOffset = 0 |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
if t.align == AlignLeft { |
| 778 |
if t.columnOffset+width > t.longestLine { |
| 779 |
t.columnOffset = t.longestLine - width |
| 780 |
} |
| 781 |
if t.columnOffset < 0 { |
| 782 |
t.columnOffset = 0 |
| 783 |
} |
| 784 |
} else if t.align == AlignRight { |
| 785 |
if t.columnOffset-width < -t.longestLine { |
| 786 |
t.columnOffset = width - t.longestLine |
| 787 |
} |
| 788 |
if t.columnOffset > 0 { |
| 789 |
t.columnOffset = 0 |
| 790 |
} |
| 791 |
} else { |
| 792 |
half := (t.longestLine - width) / 2 |
| 793 |
if half > 0 { |
| 794 |
if t.columnOffset > half { |
| 795 |
t.columnOffset = half |
| 796 |
} |
| 797 |
if t.columnOffset < -half { |
| 798 |
t.columnOffset = -half |
| 799 |
} |
| 800 |
} else { |
| 801 |
t.columnOffset = 0 |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
defaultStyle := t.baseStyle |
| 807 |
for line := t.lineOffset; line < len(t.index); line++ { |
| 808 |
|
| 809 |
if line-t.lineOffset >= height { |
| 810 |
break |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
index := t.index[line] |
| 815 |
text := t.buffer[index.Line][index.Pos:index.NextPos] |
| 816 |
foregroundColor := index.ForegroundColor |
| 817 |
backgroundColor := index.BackgroundColor |
| 818 |
attributes := index.Attributes |
| 819 |
regionID := index.Region |
| 820 |
|
| 821 |
|
| 822 |
var ( |
| 823 |
colorTagIndices [][]int |
| 824 |
colorTags [][]string |
| 825 |
escapeIndices [][]int |
| 826 |
) |
| 827 |
strippedText := text |
| 828 |
if t.dynamicColors { |
| 829 |
colorTagIndices, colorTags, _, _, escapeIndices, strippedText, _ = decomposeString(text, true, false) |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
var ( |
| 834 |
regionIndices [][]int |
| 835 |
regions [][]string |
| 836 |
) |
| 837 |
if t.regions { |
| 838 |
regionIndices = regionPattern.FindAllStringIndex(text, -1) |
| 839 |
regions = regionPattern.FindAllStringSubmatch(text, -1) |
| 840 |
strippedText = regionPattern.ReplaceAllString(strippedText, "") |
| 841 |
if !t.dynamicColors { |
| 842 |
escapeIndices = escapePattern.FindAllStringIndex(text, -1) |
| 843 |
strippedText = escapePattern.ReplaceAllString(strippedText, "[$1$2]") |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
var skip, posX int |
| 849 |
if t.align == AlignLeft { |
| 850 |
posX = -t.columnOffset |
| 851 |
} else if t.align == AlignRight { |
| 852 |
posX = width - index.Width - t.columnOffset |
| 853 |
} else { |
| 854 |
posX = (width-index.Width)/2 - t.columnOffset |
| 855 |
} |
| 856 |
if posX < 0 { |
| 857 |
skip = -posX |
| 858 |
posX = 0 |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
var colorPos, regionPos, escapePos, tagOffset, skipped int |
| 863 |
iterateString(strippedText, func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool { |
| 864 |
|
| 865 |
for { |
| 866 |
if colorPos < len(colorTags) && textPos+tagOffset >= colorTagIndices[colorPos][0] && textPos+tagOffset < colorTagIndices[colorPos][1] { |
| 867 |
|
| 868 |
foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[colorPos]) |
| 869 |
tagOffset += colorTagIndices[colorPos][1] - colorTagIndices[colorPos][0] |
| 870 |
colorPos++ |
| 871 |
} else if regionPos < len(regionIndices) && textPos+tagOffset >= regionIndices[regionPos][0] && textPos+tagOffset < regionIndices[regionPos][1] { |
| 872 |
|
| 873 |
regionID = regions[regionPos][1] |
| 874 |
tagOffset += regionIndices[regionPos][1] - regionIndices[regionPos][0] |
| 875 |
regionPos++ |
| 876 |
} else { |
| 877 |
break |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
if escapePos < len(escapeIndices) && textPos+tagOffset == escapeIndices[escapePos][1]-2 { |
| 883 |
tagOffset++ |
| 884 |
escapePos++ |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
_, _, existingStyle, _ := screen.GetContent(posX, line-t.lineOffset) |
| 889 |
_, background, _ := existingStyle.Decompose() |
| 890 |
style := overlayStyle(defaultStyle.Background(background), foregroundColor, backgroundColor, attributes) |
| 891 |
|
| 892 |
|
| 893 |
var highlighted bool |
| 894 |
if len(regionID) > 0 { |
| 895 |
if _, ok := t.highlights[regionID]; ok { |
| 896 |
highlighted = true |
| 897 |
} |
| 898 |
} |
| 899 |
if highlighted { |
| 900 |
_, _, attrs := style.Decompose() |
| 901 |
reversed := attrs&tcell.AttrReverse != 0 |
| 902 |
style = style.Reverse(!reversed) |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
if !t.wrap && skipped < skip { |
| 907 |
skipped += screenWidth |
| 908 |
return false |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
if posX+screenWidth > width { |
| 913 |
return true |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
for offset := screenWidth - 1; offset >= 0; offset-- { |
| 918 |
if offset == 0 { |
| 919 |
screen.SetContent(posX+offset, line-t.lineOffset, main, comb, style) |
| 920 |
} else { |
| 921 |
screen.SetContent(posX+offset, line-t.lineOffset, ' ', nil, style) |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
posX += screenWidth |
| 927 |
return false |
| 928 |
}) |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
if !t.scrollable && t.lineOffset > 0 { |
| 934 |
t.buffer = t.buffer[t.index[t.lineOffset].Line:] |
| 935 |
t.index = nil |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
func (t *TextView) Submit(event KeyEvent) bool { |
| 940 |
return true |
| 941 |
} |
| 942 |
|
| 943 |
func (t *TextView) OnKeyEvent(event KeyEvent) bool { |
| 944 |
key := event.Key() |
| 945 |
|
| 946 |
if !t.scrollable { |
| 947 |
return false |
| 948 |
} |
| 949 |
|
| 950 |
switch key { |
| 951 |
case tcell.KeyRune: |
| 952 |
switch event.Rune() { |
| 953 |
case 'g': |
| 954 |
t.trackEnd = false |
| 955 |
t.lineOffset = 0 |
| 956 |
t.columnOffset = 0 |
| 957 |
case 'G': |
| 958 |
t.trackEnd = true |
| 959 |
t.columnOffset = 0 |
| 960 |
case 'j': |
| 961 |
t.lineOffset++ |
| 962 |
case 'k': |
| 963 |
t.trackEnd = false |
| 964 |
t.lineOffset-- |
| 965 |
case 'h': |
| 966 |
t.columnOffset-- |
| 967 |
case 'l': |
| 968 |
t.columnOffset++ |
| 969 |
} |
| 970 |
case tcell.KeyHome: |
| 971 |
t.trackEnd = false |
| 972 |
t.lineOffset = 0 |
| 973 |
t.columnOffset = 0 |
| 974 |
case tcell.KeyEnd: |
| 975 |
t.trackEnd = true |
| 976 |
t.columnOffset = 0 |
| 977 |
case tcell.KeyUp: |
| 978 |
t.trackEnd = false |
| 979 |
t.lineOffset-- |
| 980 |
case tcell.KeyDown: |
| 981 |
t.lineOffset++ |
| 982 |
case tcell.KeyLeft: |
| 983 |
t.columnOffset-- |
| 984 |
case tcell.KeyRight: |
| 985 |
t.columnOffset++ |
| 986 |
case tcell.KeyPgDn, tcell.KeyCtrlF: |
| 987 |
t.lineOffset += t.pageSize |
| 988 |
case tcell.KeyPgUp, tcell.KeyCtrlB: |
| 989 |
t.trackEnd = false |
| 990 |
t.lineOffset -= t.pageSize |
| 991 |
default: |
| 992 |
return false |
| 993 |
} |
| 994 |
return true |
| 995 |
} |
| 996 |
|
| 997 |
func (t *TextView) OnMouseEvent(event MouseEvent) bool { |
| 998 |
if !t.scrollable { |
| 999 |
return false |
| 1000 |
} |
| 1001 |
|
| 1002 |
switch event.Buttons() { |
| 1003 |
case tcell.WheelDown: |
| 1004 |
t.lineOffset += 3 |
| 1005 |
case tcell.WheelUp: |
| 1006 |
t.lineOffset -= 3 |
| 1007 |
default: |
| 1008 |
return false |
| 1009 |
} |
| 1010 |
return true |
| 1011 |
} |
| 1012 |
|
| 1013 |
func (t *TextView) OnPasteEvent(event PasteEvent) bool { |
| 1014 |
return false |
| 1015 |
} |