vendor/github.com/charmbracelet/x/ansi/wrap.go
395 lines · 8146 bytes
1package ansi
2
3import (
4 "bytes"
5 "unicode"
6 "unicode/utf8"
7
8 "github.com/charmbracelet/x/ansi/parser"
9 "github.com/rivo/uniseg"
10)
11
12// nbsp is a non-breaking space
13const nbsp = 0xA0
14
15// Hardwrap wraps a string or a block of text to a given line length, breaking
16// word boundaries. This will preserve ANSI escape codes and will account for
17// wide-characters in the string.
18// When preserveSpace is true, spaces at the beginning of a line will be
19// preserved.
20func Hardwrap(s string, limit int, preserveSpace bool) string {
21 if limit < 1 {
22 return s
23 }
24
25 var (
26 cluster []byte
27 buf bytes.Buffer
28 curWidth int
29 forceNewline bool
30 pstate = parser.GroundState // initial state
31 b = []byte(s)
32 )
33
34 addNewline := func() {
35 buf.WriteByte('\n')
36 curWidth = 0
37 }
38
39 i := 0
40 for i < len(b) {
41 state, action := parser.Table.Transition(pstate, b[i])
42 if state == parser.Utf8State {
43 var width int
44 cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
45 i += len(cluster)
46
47 if curWidth+width > limit {
48 addNewline()
49 }
50 if !preserveSpace && curWidth == 0 && len(cluster) <= 4 {
51 // Skip spaces at the beginning of a line
52 if r, _ := utf8.DecodeRune(cluster); r != utf8.RuneError && unicode.IsSpace(r) {
53 pstate = parser.GroundState
54 continue
55 }
56 }
57
58 buf.Write(cluster)
59 curWidth += width
60 pstate = parser.GroundState
61 continue
62 }
63
64 switch action {
65 case parser.PrintAction, parser.ExecuteAction:
66 if b[i] == '\n' {
67 addNewline()
68 forceNewline = false
69 break
70 }
71
72 if curWidth+1 > limit {
73 addNewline()
74 forceNewline = true
75 }
76
77 // Skip spaces at the beginning of a line
78 if curWidth == 0 {
79 if !preserveSpace && forceNewline && unicode.IsSpace(rune(b[i])) {
80 break
81 }
82 forceNewline = false
83 }
84
85 buf.WriteByte(b[i])
86 curWidth++
87 default:
88 buf.WriteByte(b[i])
89 }
90
91 // We manage the UTF8 state separately manually above.
92 if pstate != parser.Utf8State {
93 pstate = state
94 }
95 i++
96 }
97
98 return buf.String()
99}
100
101// Wordwrap wraps a string or a block of text to a given line length, not
102// breaking word boundaries. This will preserve ANSI escape codes and will
103// account for wide-characters in the string.
104// The breakpoints string is a list of characters that are considered
105// breakpoints for word wrapping. A hyphen (-) is always considered a
106// breakpoint.
107//
108// Note: breakpoints must be a string of 1-cell wide rune characters.
109func Wordwrap(s string, limit int, breakpoints string) string {
110 if limit < 1 {
111 return s
112 }
113
114 var (
115 cluster []byte
116 buf bytes.Buffer
117 word bytes.Buffer
118 space bytes.Buffer
119 curWidth int
120 wordLen int
121 pstate = parser.GroundState // initial state
122 b = []byte(s)
123 )
124
125 addSpace := func() {
126 curWidth += space.Len()
127 buf.Write(space.Bytes())
128 space.Reset()
129 }
130
131 addWord := func() {
132 if word.Len() == 0 {
133 return
134 }
135
136 addSpace()
137 curWidth += wordLen
138 buf.Write(word.Bytes())
139 word.Reset()
140 wordLen = 0
141 }
142
143 addNewline := func() {
144 buf.WriteByte('\n')
145 curWidth = 0
146 space.Reset()
147 }
148
149 i := 0
150 for i < len(b) {
151 state, action := parser.Table.Transition(pstate, b[i])
152 if state == parser.Utf8State {
153 var width int
154 cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
155 i += len(cluster)
156
157 r, _ := utf8.DecodeRune(cluster)
158 if r != utf8.RuneError && unicode.IsSpace(r) && r != nbsp {
159 addWord()
160 space.WriteRune(r)
161 } else if bytes.ContainsAny(cluster, breakpoints) {
162 addSpace()
163 addWord()
164 buf.Write(cluster)
165 curWidth++
166 } else {
167 word.Write(cluster)
168 wordLen += width
169 if curWidth+space.Len()+wordLen > limit &&
170 wordLen < limit {
171 addNewline()
172 }
173 }
174
175 pstate = parser.GroundState
176 continue
177 }
178
179 switch action {
180 case parser.PrintAction, parser.ExecuteAction:
181 r := rune(b[i])
182 switch {
183 case r == '\n':
184 if wordLen == 0 {
185 if curWidth+space.Len() > limit {
186 curWidth = 0
187 } else {
188 buf.Write(space.Bytes())
189 }
190 space.Reset()
191 }
192
193 addWord()
194 addNewline()
195 case unicode.IsSpace(r):
196 addWord()
197 space.WriteByte(b[i])
198 case r == '-':
199 fallthrough
200 case runeContainsAny(r, breakpoints):
201 addSpace()
202 addWord()
203 buf.WriteByte(b[i])
204 curWidth++
205 default:
206 word.WriteByte(b[i])
207 wordLen++
208 if curWidth+space.Len()+wordLen > limit &&
209 wordLen < limit {
210 addNewline()
211 }
212 }
213
214 default:
215 word.WriteByte(b[i])
216 }
217
218 // We manage the UTF8 state separately manually above.
219 if pstate != parser.Utf8State {
220 pstate = state
221 }
222 i++
223 }
224
225 addWord()
226
227 return buf.String()
228}
229
230// Wrap wraps a string or a block of text to a given line length, breaking word
231// boundaries if necessary. This will preserve ANSI escape codes and will
232// account for wide-characters in the string. The breakpoints string is a list
233// of characters that are considered breakpoints for word wrapping. A hyphen
234// (-) is always considered a breakpoint.
235//
236// Note: breakpoints must be a string of 1-cell wide rune characters.
237func Wrap(s string, limit int, breakpoints string) string {
238 if limit < 1 {
239 return s
240 }
241
242 var (
243 cluster []byte
244 buf bytes.Buffer
245 word bytes.Buffer
246 space bytes.Buffer
247 curWidth int // written width of the line
248 wordLen int // word buffer len without ANSI escape codes
249 pstate = parser.GroundState // initial state
250 b = []byte(s)
251 )
252
253 addSpace := func() {
254 curWidth += space.Len()
255 buf.Write(space.Bytes())
256 space.Reset()
257 }
258
259 addWord := func() {
260 if word.Len() == 0 {
261 return
262 }
263
264 addSpace()
265 curWidth += wordLen
266 buf.Write(word.Bytes())
267 word.Reset()
268 wordLen = 0
269 }
270
271 addNewline := func() {
272 buf.WriteByte('\n')
273 curWidth = 0
274 space.Reset()
275 }
276
277 i := 0
278 for i < len(b) {
279 state, action := parser.Table.Transition(pstate, b[i])
280 if state == parser.Utf8State {
281 var width int
282 cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
283 i += len(cluster)
284
285 r, _ := utf8.DecodeRune(cluster)
286 switch {
287 case r != utf8.RuneError && unicode.IsSpace(r) && r != nbsp: // nbsp is a non-breaking space
288 addWord()
289 space.WriteRune(r)
290 case bytes.ContainsAny(cluster, breakpoints):
291 addSpace()
292 if curWidth+wordLen+width > limit {
293 word.Write(cluster)
294 wordLen += width
295 } else {
296 addWord()
297 buf.Write(cluster)
298 curWidth += width
299 }
300 default:
301 if wordLen+width > limit {
302 // Hardwrap the word if it's too long
303 addWord()
304 }
305
306 word.Write(cluster)
307 wordLen += width
308
309 if curWidth+wordLen+space.Len() > limit {
310 addNewline()
311 }
312 }
313
314 pstate = parser.GroundState
315 continue
316 }
317
318 switch action {
319 case parser.PrintAction, parser.ExecuteAction:
320 switch r := rune(b[i]); {
321 case r == '\n':
322 if wordLen == 0 {
323 if curWidth+space.Len() > limit {
324 curWidth = 0
325 } else {
326 // preserve whitespaces
327 buf.Write(space.Bytes())
328 }
329 space.Reset()
330 }
331
332 addWord()
333 addNewline()
334 case unicode.IsSpace(r):
335 addWord()
336 space.WriteRune(r)
337 case r == '-':
338 fallthrough
339 case runeContainsAny(r, breakpoints):
340 addSpace()
341 if curWidth+wordLen >= limit {
342 // We can't fit the breakpoint in the current line, treat
343 // it as part of the word.
344 word.WriteRune(r)
345 wordLen++
346 } else {
347 addWord()
348 buf.WriteRune(r)
349 curWidth++
350 }
351 default:
352 word.WriteRune(r)
353 wordLen++
354
355 if wordLen == limit {
356 // Hardwrap the word if it's too long
357 addWord()
358 }
359
360 if curWidth+wordLen+space.Len() > limit {
361 addNewline()
362 }
363 }
364
365 default:
366 word.WriteByte(b[i])
367 }
368
369 // We manage the UTF8 state separately manually above.
370 if pstate != parser.Utf8State {
371 pstate = state
372 }
373 i++
374 }
375
376 if word.Len() != 0 {
377 // Preserve ANSI wrapped spaces at the end of string
378 if curWidth+space.Len() > limit {
379 buf.WriteByte('\n')
380 }
381 addSpace()
382 }
383 buf.Write(word.Bytes())
384
385 return buf.String()
386}
387
388func runeContainsAny(r rune, s string) bool {
389 for _, c := range s {
390 if c == r {
391 return true
392 }
393 }
394 return false
395}