1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
package text
import (
"bytes"
"github.com/mattn/go-runewidth"
"strings"
)
// Wrap a text for an exact line size
// Handle properly terminal color escape code
func Wrap(text string, lineWidth int) (string, int) {
return WrapLeftPadded(text, lineWidth, 0)
}
// Wrap a text for an exact line size with a left padding
// Handle properly terminal color escape code
func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
var textBuffer bytes.Buffer
nbLine := 0
pad := strings.Repeat(" ", leftPad)
// tabs are formatted as 4 spaces
text = strings.Replace(text, "\t", " ", 4)
wrapped := wrapText(text, lineWidth-leftPad)
for _, line := range strings.Split(wrapped, "\n") {
textBuffer.WriteString(pad + line)
textBuffer.WriteString("\n")
nbLine++
}
return textBuffer.String(), nbLine
}
// Wrap text so that each line fills at most w cells. Lines break at word
// boundary or multibyte chars.
//
// Wrapping Algorithm: Treat the text as a sequence of words, with each word be
// an alphanumeric word, or a multibyte char. We scan through the text and
// construct the word, and flush the word into the paragraph once a word is
// ready. A word is ready when a word boundary is detected: a boundary char such
// as '\n', '\t', and ' ' is encountered; a multibyte char is found; or a
// multibyte to single-byte switch is encountered. '\n' is handled in a special
// manner.
func wrapText(s string, w int) string {
word := ""
out := ""
width := 0
firstWord := true
isMultibyteWord := false
flushWord := func() {
wl := wordLen(word)
if isMultibyteWord {
if width+wl > w {
out += "\n" + word
width = wl
} else {
out += word
width += wl
}
} else {
if width == 0 {
out += word
width += wl
} else if width+wl+1 > w {
out += "\n" + word
width = wl
} else {
out += " " + word
width += wl + 1
}
}
word = ""
}
for _, r := range []rune(s) {
cw := runewidth.RuneWidth(r)
if firstWord {
word = string(r)
isMultibyteWord = cw > 1
firstWord = false
continue
}
if r == '\n' {
flushWord()
out += "\n"
width = 0
} else if r == ' ' || r == '\t' {
flushWord()
} else if cw > 1 {
flushWord()
word = string(r)
isMultibyteWord = true
word = string(r)
} else if cw == 1 && isMultibyteWord {
flushWord()
word = string(r)
isMultibyteWord = false
} else {
word += string(r)
}
}
// The text may end without newlines, ensure flushing it or we can lose the
// last word.
flushWord()
return out
}
// wordLen return the length of a word, while ignoring the terminal escape
// sequences
func wordLen(word string) int {
length := 0
escape := false
for _, char := range word {
if char == '\x1b' {
escape = true
}
if !escape {
length += runewidth.RuneWidth(rune(char))
}
if char == 'm' {
escape = false
}
}
return length
}
// splitWord split a word at the given length, while ignoring the terminal escape sequences
func splitWord(word string, length int) (string, string) {
runes := []rune(word)
var result []rune
added := 0
escape := false
if length == 0 {
return "", word
}
for _, r := range runes {
if r == '\x1b' {
escape = true
}
width := runewidth.RuneWidth(r)
if width+added > length {
// wide character made the length overflow
break
}
result = append(result, r)
if !escape {
added += width
if added >= length {
break
}
}
if r == 'm' {
escape = false
}
}
leftover := runes[len(result):]
return string(result), string(leftover)
}
func minInt(a, b int) int {
if a > b {
return b
}
return a
}
|