summaryrefslogtreecommitdiffstatshomepage
path: root/util/text/text.go
diff options
context:
space:
mode:
Diffstat (limited to 'util/text/text.go')
-rw-r--r--util/text/text.go32
1 files changed, 21 insertions, 11 deletions
diff --git a/util/text/text.go b/util/text/text.go
index 10b70b01f..c000596cd 100644
--- a/util/text/text.go
+++ b/util/text/text.go
@@ -58,7 +58,7 @@ func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
} else {
// Break a word longer than a line
if wordLength > lineWidth {
- for wordLength > 0 && len(word) > 0 {
+ for wordLength > 0 && wordLen(word) > 0 {
l := minInt(spaceLeft, wordLength)
part, leftover := splitWord(word, l)
word = leftover
@@ -76,6 +76,10 @@ func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
nbLine++
spaceLeft = lineWidth - leftPad
}
+
+ if wordLength <= 0 {
+ break
+ }
}
} else {
// Normal break
@@ -84,20 +88,24 @@ func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
lineBuffer.Reset()
lineBuffer.WriteString(word)
firstWord = false
- spaceLeft = lineWidth - wordLength
+ spaceLeft = lineWidth - leftPad - wordLength
nbLine++
}
}
}
- textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
- lineBuffer.Reset()
+ if lineBuffer.Len() > 0 {
+ textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
+ lineBuffer.Reset()
+ }
+
firstLine = false
}
return textBuffer.String(), nbLine
}
+// wordLen return the length of a word, while ignoring the terminal escape sequences
func wordLen(word string) int {
length := 0
escape := false
@@ -119,8 +127,10 @@ func wordLen(word string) int {
return length
}
+// splitWord split a word at the given length, while ignoring the terminal escape sequences
func splitWord(word string, length int) (string, string) {
- result := ""
+ runes := []rune(word)
+ var result []rune
added := 0
escape := false
@@ -128,12 +138,12 @@ func splitWord(word string, length int) (string, string) {
return "", word
}
- for _, char := range word {
- if char == '\x1b' {
+ for _, r := range runes {
+ if r == '\x1b' {
escape = true
}
- result += string(char)
+ result = append(result, r)
if !escape {
added++
@@ -142,14 +152,14 @@ func splitWord(word string, length int) (string, string) {
}
}
- if char == 'm' {
+ if r == 'm' {
escape = false
}
}
- leftover := word[len(result):]
+ leftover := runes[len(result):]
- return result, leftover
+ return string(result), string(leftover)
}
func minInt(a, b int) int {