Skip to content

Commit

Permalink
Fix plain text length issue
Browse files Browse the repository at this point in the history
Make sure that plain text length function correctly uses `RuneCountInString`
from the `utf8` package.
  • Loading branch information
HeavyWombat committed Jun 17, 2019
1 parent 5105ac0 commit c128055
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 21 additions & 6 deletions convenience.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,35 @@

package bunt

import colorful "github.com/lucasb-eyer/go-colorful"
import (
"regexp"
"strings"
"unicode/utf8"

colorful "github.com/lucasb-eyer/go-colorful"
)

// StyleOption defines style option for strings
type StyleOption func(*String)

// PlainTextLength returns the length of the input text without any escape
// sequences. The function will panic in the unlikely case of a parse issue.
// sequences.
func PlainTextLength(text string) int {
result, err := ParseString(text)
if err != nil {
panic(err)
return utf8.RuneCountInString(RemoveAllEscapeSequences(text))
}

// RemoveAllEscapeSequences return the input string with all escape sequences
// removed.
func RemoveAllEscapeSequences(input string) string {
escapeSeqFinderRegExp := regexp.MustCompile(`\x1b\[([\d;]*)m`)

for loc := escapeSeqFinderRegExp.FindStringIndex(input); loc != nil; loc = escapeSeqFinderRegExp.FindStringIndex(input) {
start := loc[0]
end := loc[1]
input = strings.Replace(input, input[start:end], "", -1)
}

return len(*result)
return input
}

// Substring returns a substring of a text that may contains escape sequences.
Expand Down
4 changes: 4 additions & 0 deletions convenience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ var _ = Describe("convenience functions", func() {
It("should return the right size when used on strings created by the bunt package", func() {
Expect(PlainTextLength(Sprintf("*This* text is too long"))).To(BeEquivalentTo(len(Sprintf("This text is too long"))))
})

It("should return the correct length based on the rune count", func() {
Expect(PlainTextLength("fünf")).To(BeEquivalentTo(4))
})
})

Context("style function", func() {
Expand Down

0 comments on commit c128055

Please sign in to comment.