Browse Source

Use TrimRightFunc when wrapping text in TextView

tablepad
Trevor Slocum 2 years ago
parent
commit
50a085333b
  1. 7
      textview.go
  2. 3
      textview_test.go

7
textview.go

@ -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]))
}
}

3
textview_test.go

@ -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 {

Loading…
Cancel
Save