| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func Wrap(s string, limit int, breakpoints string) string { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 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") { |
| 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 |
|
| 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 |
|
| 124 |
ReadStyle(p.Params(), &style) |
| 125 |
} else if ansi.HasOscPrefix(seq) && p.Command() == 8 { |
| 126 |
|
| 127 |
ReadLink(p.Data(), &link) |
| 128 |
} |
| 129 |
|
| 130 |
word.WriteString(seq) |
| 131 |
default: |
| 132 |
if len(seq) == 1 { |
| 133 |
|
| 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 |
|
| 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 |
|
| 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 |
} |