Debian Codegraph @

cellbuf/wrap.go

raw
1 package cellbuf
2
3 import (
4 "bytes"
5 "slices"
6 "unicode"
7 "unicode/utf8"
8
9 "github.com/charmbracelet/x/ansi"
10 )
11
12 const nbsp = '\u00a0'
13
14 // Wrap returns a string that is wrapped to the specified limit applying any
15 // ANSI escape sequences in the string. It tries to wrap the string at word
16 // boundaries, but will break words if necessary.
17 //
18 // The breakpoints string is a list of characters that are considered
19 // breakpoints for word wrapping. A hyphen (-) is always considered a
20 // breakpoint.
21 //
22 // Note: breakpoints must be a string of 1-cell wide rune characters.
23 func Wrap(s string, limit int, breakpoints string) string {
24 //nolint:godox
25 // TODO: Use [PenWriter] once we get
26 // https://github.com/charmbracelet/lipgloss/pull/489 out the door and
27 // released.
28 // The problem is that [ansi.Wrap] doesn't keep track of style and link
29 // state, so combining both breaks styled space cells. To fix this, we use
30 // non-breaking space cells for padding and styled blank cells. And since
31 // both wrapping methods respect non-breaking spaces, we can use them to
32 // preserve styled spaces in the output.
33
34 if len(s) == 0 {
35 return ""
36 }
37
38 if limit < 1 {
39 return s
40 }
41
42 p := ansi.GetParser()
43 defer ansi.PutParser(p)
44
45 var (
46 buf bytes.Buffer
47 word bytes.Buffer
48 space bytes.Buffer
49 style, curStyle Style
50 link, curLink Link
51 curWidth int
52 wordLen int
53 )
54
55 hasBlankStyle := func() bool {
56 // Only follow reverse attribute, bg color and underline style
57 return !style.Attrs.Contains(ReverseAttr) && style.Bg == nil && style.UlStyle == NoUnderline
58 }
59
60 addSpace := func() {
61 curWidth += space.Len()
62 buf.Write(space.Bytes())
63 space.Reset()
64 }
65
66 addWord := func() {
67 if word.Len() == 0 {
68 return
69 }
70
71 curLink = link
72 curStyle = style
73
74 addSpace()
75 curWidth += wordLen
76 buf.Write(word.Bytes())
77 word.Reset()
78 wordLen = 0
79 }
80
81 addNewline := func() {
82 if !curStyle.Empty() {
83 buf.WriteString(ansi.ResetStyle)
84 }
85 if !curLink.Empty() {
86 buf.WriteString(ansi.ResetHyperlink())
87 }
88 buf.WriteByte('\n')
89 if !curLink.Empty() {
90 buf.WriteString(ansi.SetHyperlink(curLink.URL, curLink.Params))
91 }
92 if !curStyle.Empty() {
93 buf.WriteString(curStyle.Sequence())
94 }
95 curWidth = 0
96 space.Reset()
97 }
98
99 var state byte
100 for len(s) > 0 {
101 seq, width, n, newState := ansi.DecodeSequence(s, state, p)
102 switch width {
103 case 0:
104 if ansi.Equal(seq, "\t") { //nolint:nestif
105 addWord()
106 space.WriteString(seq)
107 break
108 } else if ansi.Equal(seq, "\n") {
109 if wordLen == 0 {
110 if curWidth+space.Len() > limit {
111 curWidth = 0
112 } else {
113 // preserve whitespaces
114 buf.Write(space.Bytes())
115 }
116 space.Reset()
117 }
118
119 addWord()
120 addNewline()
121 break
122 } else if ansi.HasCsiPrefix(seq) && p.Command() == 'm' {
123 // SGR style sequence [ansi.SGR]
124 ReadStyle(p.Params(), &style)
125 } else if ansi.HasOscPrefix(seq) && p.Command() == 8 {
126 // Hyperlink sequence [ansi.SetHyperlink]
127 ReadLink(p.Data(), &link)
128 }
129
130 word.WriteString(seq)
131 default:
132 if len(seq) == 1 {
133 // ASCII
134 r, _ := utf8.DecodeRuneInString(seq)
135 if r != nbsp && unicode.IsSpace(r) && hasBlankStyle() {
136 addWord()
137 space.WriteRune(r)
138 break
139 } else if r == '-' || runeContainsAny(r, breakpoints) {
140 addSpace()
141 if curWidth+wordLen+width <= limit {
142 addWord()
143 buf.WriteString(seq)
144 curWidth += width
145 break
146 }
147 }
148 }
149
150 if wordLen+width > limit {
151 // Hardwrap the word if it's too long
152 addWord()
153 }
154
155 word.WriteString(seq)
156 wordLen += width
157
158 if curWidth+wordLen+space.Len() > limit {
159 addNewline()
160 }
161 }
162
163 s = s[n:]
164 state = newState
165 }
166
167 if wordLen == 0 {
168 if curWidth+space.Len() > limit {
169 curWidth = 0
170 } else {
171 // preserve whitespaces
172 buf.Write(space.Bytes())
173 }
174 space.Reset()
175 }
176
177 addWord()
178
179 if !curLink.Empty() {
180 buf.WriteString(ansi.ResetHyperlink())
181 }
182 if !curStyle.Empty() {
183 buf.WriteString(ansi.ResetStyle)
184 }
185
186 return buf.String()
187 }
188
189 func runeContainsAny[T string | []rune](r rune, s T) bool {
190 return slices.Contains([]rune(s), r)
191 }