2018-09-11 23:04:16 +03:00
|
|
|
package text
|
2018-08-03 18:29:11 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-08-11 23:27:45 +03:00
|
|
|
// Wrap a text for an exact line size
|
|
|
|
// Handle properly terminal color escape code
|
2018-09-11 23:04:16 +03:00
|
|
|
func Wrap(text string, lineWidth int) (string, int) {
|
|
|
|
return WrapLeftPadded(text, lineWidth, 0)
|
2018-08-09 15:35:55 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:27:45 +03:00
|
|
|
// Wrap a text for an exact line size with a left padding
|
|
|
|
// Handle properly terminal color escape code
|
2018-09-11 23:04:16 +03:00
|
|
|
func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
|
2018-08-03 18:29:11 +03:00
|
|
|
var textBuffer bytes.Buffer
|
|
|
|
var lineBuffer bytes.Buffer
|
|
|
|
nbLine := 1
|
|
|
|
firstLine := true
|
2018-08-09 15:35:55 +03:00
|
|
|
pad := strings.Repeat(" ", leftPad)
|
2018-08-03 18:29:11 +03:00
|
|
|
|
|
|
|
// tabs are formatted as 4 spaces
|
|
|
|
text = strings.Replace(text, "\t", " ", 4)
|
|
|
|
|
|
|
|
for _, line := range strings.Split(text, "\n") {
|
2018-08-09 15:35:55 +03:00
|
|
|
spaceLeft := lineWidth - leftPad
|
2018-08-03 18:29:11 +03:00
|
|
|
|
|
|
|
if !firstLine {
|
|
|
|
textBuffer.WriteString("\n")
|
|
|
|
nbLine++
|
|
|
|
}
|
|
|
|
|
|
|
|
firstWord := true
|
|
|
|
|
|
|
|
for _, word := range strings.Split(line, " ") {
|
2018-08-11 23:27:45 +03:00
|
|
|
wordLength := wordLen(word)
|
|
|
|
|
|
|
|
if !firstWord {
|
|
|
|
lineBuffer.WriteString(" ")
|
|
|
|
spaceLeft -= 1
|
2018-08-09 15:06:55 +03:00
|
|
|
|
2018-08-11 23:27:45 +03:00
|
|
|
if spaceLeft <= 0 {
|
|
|
|
textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
|
|
|
|
textBuffer.WriteString("\n")
|
|
|
|
lineBuffer.Reset()
|
|
|
|
spaceLeft = lineWidth - leftPad
|
|
|
|
nbLine++
|
|
|
|
firstLine = false
|
2018-08-03 18:29:11 +03:00
|
|
|
}
|
2018-08-11 23:27:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Word fit in the current line
|
|
|
|
if spaceLeft >= wordLength {
|
|
|
|
lineBuffer.WriteString(word)
|
|
|
|
spaceLeft -= wordLength
|
2018-08-03 18:29:11 +03:00
|
|
|
firstWord = false
|
|
|
|
} else {
|
2018-08-11 23:27:45 +03:00
|
|
|
// Break a word longer than a line
|
|
|
|
if wordLength > lineWidth {
|
|
|
|
for wordLength > 0 && len(word) > 0 {
|
|
|
|
l := minInt(spaceLeft, wordLength)
|
|
|
|
part, leftover := splitWord(word, l)
|
|
|
|
word = leftover
|
|
|
|
wordLength = wordLen(word)
|
2018-08-03 18:29:11 +03:00
|
|
|
|
|
|
|
lineBuffer.WriteString(part)
|
2018-08-09 15:35:55 +03:00
|
|
|
textBuffer.WriteString(pad)
|
2018-08-03 18:29:11 +03:00
|
|
|
textBuffer.Write(lineBuffer.Bytes())
|
|
|
|
lineBuffer.Reset()
|
|
|
|
|
2018-08-11 23:27:45 +03:00
|
|
|
spaceLeft -= l
|
|
|
|
|
|
|
|
if spaceLeft <= 0 {
|
2018-08-03 18:29:11 +03:00
|
|
|
textBuffer.WriteString("\n")
|
|
|
|
nbLine++
|
2018-08-11 23:27:45 +03:00
|
|
|
spaceLeft = lineWidth - leftPad
|
2018-08-03 18:29:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-11 23:27:45 +03:00
|
|
|
// Normal break
|
2018-08-09 15:35:55 +03:00
|
|
|
textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
|
2018-08-03 18:29:11 +03:00
|
|
|
textBuffer.WriteString("\n")
|
|
|
|
lineBuffer.Reset()
|
2018-08-11 23:27:45 +03:00
|
|
|
lineBuffer.WriteString(word)
|
2018-08-03 18:29:11 +03:00
|
|
|
firstWord = false
|
2018-08-11 23:27:45 +03:00
|
|
|
spaceLeft = lineWidth - wordLength
|
2018-08-03 18:29:11 +03:00
|
|
|
nbLine++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-11 23:27:45 +03:00
|
|
|
|
2018-08-09 15:35:55 +03:00
|
|
|
textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
|
2018-08-03 18:29:11 +03:00
|
|
|
lineBuffer.Reset()
|
|
|
|
firstLine = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return textBuffer.String(), nbLine
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:27:45 +03:00
|
|
|
func wordLen(word string) int {
|
|
|
|
length := 0
|
|
|
|
escape := false
|
|
|
|
|
|
|
|
for _, char := range word {
|
|
|
|
if char == '\x1b' {
|
|
|
|
escape = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !escape {
|
|
|
|
length++
|
|
|
|
}
|
|
|
|
|
|
|
|
if char == 'm' {
|
|
|
|
escape = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return length
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitWord(word string, length int) (string, string) {
|
|
|
|
result := ""
|
|
|
|
added := 0
|
|
|
|
escape := false
|
|
|
|
|
|
|
|
if length == 0 {
|
|
|
|
return "", word
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, char := range word {
|
|
|
|
if char == '\x1b' {
|
|
|
|
escape = true
|
|
|
|
}
|
|
|
|
|
|
|
|
result += string(char)
|
|
|
|
|
|
|
|
if !escape {
|
|
|
|
added++
|
|
|
|
if added == length {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if char == 'm' {
|
|
|
|
escape = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leftover := word[len(result):]
|
|
|
|
|
|
|
|
return result, leftover
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:29:11 +03:00
|
|
|
func minInt(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|