Use TrimRightFunc when wrapping text in TextView

This commit is contained in:
Trevor Slocum 2020-10-05 18:12:29 -07:00
parent 659788f590
commit 50a085333b
2 changed files with 6 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"regexp"
"sync"
"unicode"
"unicode/utf8"
"github.com/gdamore/tcell/v2"
@ -970,10 +971,10 @@ func (t *TextView) reindexBuffer(width int) {
if t.wrap && t.wordWrap {
for _, line := range t.index {
str := t.buffer[line.Line][line.Pos:line.NextPos]
spaces := spacePattern.FindAllIndex(str, -1)
if spaces != nil && spaces[len(spaces)-1][1] == len(str) {
trimmed := bytes.TrimRightFunc(str, unicode.IsSpace)
if len(trimmed) != len(str) {
oldNextPos := line.NextPos
line.NextPos -= spaces[len(spaces)-1][1] - spaces[len(spaces)-1][0]
line.NextPos -= len(str) - len(trimmed)
line.Width -= stringWidth(string(t.buffer[line.Line][line.NextPos:oldNextPos]))
}
}

View File

@ -297,6 +297,8 @@ func BenchmarkTextViewDraw(b *testing.B) {
}
func TestTextViewMaxLines(t *testing.T) {
t.Parallel()
tv := NewTextView()
// append 100 lines with no limit set:
@ -341,7 +343,6 @@ func TestTextViewMaxLines(t *testing.T) {
if !bytes.Equal(lines[0], []byte("L181")) {
t.Errorf("expected to get L181, got %s", lines[0])
}
}
func generateTestCases() []*textViewTestCase {