Debian Codegraph @

ansi/wrap_test.go

raw
1 package ansi_test
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/charmbracelet/x/ansi"
8 )
9
10 var cases = []struct {
11 name string
12 input string
13 limit int
14 expected string
15 preserveSpace bool
16 }{
17 {"empty string", "", 0, "", true},
18 {"passthrough", "foobar\n ", 0, "foobar\n ", true},
19 {"pass", "foo", 4, "foo", true},
20 {"simple", "foobarfoo", 4, "foob\narfo\no", true},
21 {"lf", "f\no\nobar", 3, "f\no\noba\nr", true},
22 {"lf_space", "foo bar\n baz", 3, "foo\n ba\nr\n b\naz", true},
23 {"tab", "foo\tbar", 3, "foo\n\tbar", true},
24 {"unicode_space", "foo\xc2\xa0bar", 3, "foo\nbar", false},
25 {"style_nochange", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", 7, "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", true},
26 {"style", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m", 3, "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mju\nst \nano\nthe\nr t\nest\x1B[38;2;249;38;114m\n)\x1B[0m", true},
27 {"style_lf", "I really \x1B[38;2;249;38;114mlove\x1B[0m Go!", 8, "I really\n\x1b[38;2;249;38;114mlove\x1b[0m Go!", false},
28 {"style_emoji", "I really \x1B[38;2;249;38;114mlove u🫧\x1B[0m", 8, "I really\n\x1b[38;2;249;38;114mlove u🫧\x1b[0m", false},
29 {"hyperlink", "I really \x1B]8;;https://example.com/\x1B\\love\x1B]8;;\x1B\\ Go!", 10, "I really \x1b]8;;https://example.com/\x1b\\l\nove\x1b]8;;\x1b\\ Go!", false},
30 {"dcs", "\x1BPq#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~$#2??}}GG}}??}}??-#1!14@\x1B\\foobar", 3, "\x1BPq#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~$#2??}}GG}}??}}??-#1!14@\x1B\\foo\nbar", false},
31 {"begin_with_space", " foo", 4, " foo", false},
32 {"style_dont_affect_wrap", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", 7, "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", false},
33 {"preserve_style", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m", 3, "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mju\nst \nano\nthe\nr t\nest\x1B[38;2;249;38;114m\n)\x1B[0m", false},
34 {"emoji", "foo🫧foobar", 4, "foo\n🫧fo\nobar", false},
35 {"osc8_wrap", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\สวัสดีสวัสดี\x1b]8;;\x1b\\", 8, "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\\nสวัสดีสวัสดี\x1b]8;;\x1b\\", false},
36 {"column", "VERTICAL", 1, "V\nE\nR\nT\nI\nC\nA\nL", false},
37 }
38
39 func TestHardwrap(t *testing.T) {
40 for i, tt := range cases {
41 t.Run(tt.name, func(t *testing.T) {
42 if got := ansi.Hardwrap(tt.input, tt.limit, tt.preserveSpace); got != tt.expected {
43 t.Errorf("case %d, expected %q, got %q", i+1, tt.expected, got)
44 }
45 })
46 }
47 }
48
49 var wwCases = []struct {
50 name string
51 input string
52 limit int
53 breakPoints string
54 expected string
55 }{
56 {"empty string", "", 0, "", ""},
57 {"passthrough", "foobar\n ", 0, "", "foobar\n "},
58 {"pass", "foo", 3, "", "foo"},
59 {"toolong", "foobarfoo", 4, "", "foobarfoo"},
60 {"white space", "foo bar foo", 4, "", "foo\nbar\nfoo"},
61 {"broken_at_spaces", "foo bars foobars", 4, "", "foo\nbars\nfoobars"},
62 {"hyphen", "foo-foobar", 4, "-", "foo-\nfoobar"},
63 {"emoji_breakpoint", "foo😃 foobar", 4, "😃", "foo😃\nfoobar"},
64 {"wide_emoji_breakpoint", "foo🫧 foobar", 4, "🫧", "foo🫧\nfoobar"},
65 {"space_breakpoint", "foo --bar", 9, "-", "foo --bar"},
66 {"simple", "foo bars foobars", 4, "", "foo\nbars\nfoobars"},
67 {"limit", "foo bar", 5, "", "foo\nbar"},
68 {"remove white spaces", "foo \nb ar ", 4, "", "foo\nb\nar"},
69 {"white space trail width", "foo\nb\t a\n bar", 4, "", "foo\nb\t a\n bar"},
70 {"explicit_line_break", "foo bar foo\n", 4, "", "foo\nbar\nfoo\n"},
71 {"explicit_breaks", "\nfoo bar\n\n\nfoo\n", 4, "", "\nfoo\nbar\n\n\nfoo\n"},
72 {"example", " This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* foo \nbar ", 6, "", " This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* foo\nbar"},
73 {"style_code_dont_affect_length", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", 7, "", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m"},
74 {"style_code_dont_get_wrapped", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m", 3, "", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust\nanother\ntest\x1B[38;2;249;38;114m)\x1B[0m"},
75 {"osc8_wrap", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\ สวัสดีสวัสดี\x1b]8;;\x1b\\", 8, "", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\\nสวัสดีสวัสดี\x1b]8;;\x1b\\"},
76 }
77
78 func TestWordwrap(t *testing.T) {
79 for i, tt := range wwCases {
80 t.Run(tt.name, func(t *testing.T) {
81 if got := ansi.Wordwrap(tt.input, tt.limit, tt.breakPoints); got != tt.expected {
82 t.Errorf("case %d, expected %q, got %q", i+1, tt.expected, got)
83 }
84 })
85 }
86 }
87
88 func TestWrapWordwrap(t *testing.T) {
89 input := "the quick brown foxxxxxxxxxxxxxxxx jumped over the lazy dog."
90 limit := 16
91 output := ansi.Wrap(input, limit, "")
92 if output != "the quick brown\nfoxxxxxxxxxxxxxx\nxx jumped over\nthe lazy dog." {
93 t.Errorf("expected %q, got %q", "the quick brown\nfoxxxxxxxxxxxxxx\nxx jumped over\nthe lazy dog.", output)
94 }
95 }
96
97 var wrapCases = []struct {
98 name string
99 input string
100 expected string
101 width int
102 }{
103 {
104 name: "simple",
105 input: "I really \x1B[38;2;249;38;114mlove\x1B[0m Go!",
106 expected: "I really\n\x1B[38;2;249;38;114mlove\x1B[0m Go!",
107 width: 8,
108 },
109 {
110 name: "passthrough",
111 input: "hello world",
112 expected: "hello world",
113 width: 11,
114 },
115 {
116 name: "asian",
117 input: "こんにち",
118 expected: "こんに\nち",
119 width: 7,
120 },
121 {
122 name: "emoji",
123 input: "😃👰🏻‍♀️🫧",
124 expected: "😃\n👰🏻‍♀️\n🫧",
125 width: 2,
126 },
127 {
128 name: "long style",
129 input: "\x1B[38;2;249;38;114ma really long string\x1B[0m",
130 expected: "\x1B[38;2;249;38;114ma really\nlong\nstring\x1B[0m",
131 width: 10,
132 },
133 {
134 name: "long style nbsp",
135 input: "\x1B[38;2;249;38;114ma really\u00a0long string\x1B[0m",
136 expected: "\x1b[38;2;249;38;114ma\nreally\u00a0lon\ng string\x1b[0m",
137 width: 10,
138 },
139 {
140 name: "longer",
141 input: "the quick brown foxxxxxxxxxxxxxxxx jumped over the lazy dog.",
142 expected: "the quick brown\nfoxxxxxxxxxxxxxx\nxx jumped over\nthe lazy dog.",
143 width: 16,
144 },
145 {
146 name: "longer asian",
147 input: "猴 猴 猴猴 猴猴猴猴猴猴猴猴猴 猴猴猴 猴猴 猴’ 猴猴 猴.",
148 expected: "猴 猴 猴猴\n猴猴猴猴猴猴猴猴\n猴 猴猴猴 猴猴\n猴’ 猴猴 猴.",
149 width: 16,
150 },
151 {
152 name: "long input",
153 input: "Rotated keys for a-good-offensive-cheat-code-incorporated/animal-like-law-on-the-rocks.",
154 expected: "Rotated keys for a-good-offensive-cheat-code-incorporated/animal-like-law-\non-the-rocks.",
155 width: 76,
156 },
157 {
158 name: "long input2",
159 input: "Rotated keys for a-good-offensive-cheat-code-incorporated/crypto-line-operating-system.",
160 expected: "Rotated keys for a-good-offensive-cheat-code-incorporated/crypto-line-\noperating-system.",
161 width: 76,
162 },
163 {
164 name: "hyphen breakpoint",
165 input: "a-good-offensive-cheat-code",
166 expected: "a-good-\noffensive-\ncheat-code",
167 width: 10,
168 },
169 {
170 name: "exact",
171 input: "\x1b[91mfoo\x1b[0",
172 expected: "\x1b[91mfoo\x1b[0",
173 width: 3,
174 },
175 {
176 // XXX: Should we preserve spaces on text wrapping?
177 name: "extra space",
178 input: "foo ",
179 expected: "foo",
180 width: 3,
181 },
182 {
183 name: "extra space style",
184 input: "\x1b[mfoo \x1b[m",
185 expected: "\x1b[mfoo\x1b[m",
186 width: 3,
187 },
188 {
189 name: "paragraph with styles",
190 input: "Lorem ipsum dolor \x1b[1msit\x1b[m amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \x1b[31mUt enim\x1b[m ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \x1b[38;5;200mcommodo consequat\x1b[m. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. \x1b[1;2;33mExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\x1b[m",
191 expected: "Lorem ipsum dolor \x1b[1msit\x1b[m amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor\nincididunt ut labore et dolore\nmagna aliqua. \x1b[31mUt enim\x1b[m ad minim\nveniam, quis nostrud\nexercitation ullamco laboris\nnisi ut aliquip ex ea \x1b[38;5;200mcommodo\nconsequat\x1b[m. Duis aute irure\ndolor in reprehenderit in\nvoluptate velit esse cillum\ndolore eu fugiat nulla\npariatur. \x1b[1;2;33mExcepteur sint\noccaecat cupidatat non\nproident, sunt in culpa qui\nofficia deserunt mollit anim\nid est laborum.\x1b[m",
192 width: 30,
193 },
194 {
195 // \u202f and \u205f - Single width spaces
196 // \u3000 - Double width space
197 name: "Multi Byte spaces",
198 input: "A\u202fB\u202fC\u202fDA\u205f\u205fB\u205fC\u205fDA\u3000B\u3000C\u3000D",
199 expected: "" +
200 "A\u202fB\u202fC\n" +
201 "DA\u205f\u205fB\u205fC\n" +
202 "DA\u3000B\n" +
203 "C\u3000D",
204 width: 7,
205 },
206 {"hyphen break", "foo-bar", "foo-\nbar", 5},
207 {"double space", "f bar foobaz", "f bar\nfoobaz", 6},
208 {"passthrough", "foobar\n ", "foobar\n ", 0},
209 {"pass", "foo", "foo", 3},
210 {"toolong", "foobarfoo", "foob\narfo\no", 4},
211 {"white space", "foo bar foo", "foo\nbar\nfoo", 4},
212 {"broken_at_spaces", "foo bars foobars", "foo\nbars\nfoob\nars", 4},
213 {"hyphen", "foob-foobar", "foob\n-foo\nbar", 4},
214 {"wide_emoji_breakpoint", "foo🫧 foobar", "foo\n🫧\nfoob\nar", 4},
215 {"space_breakpoint", "foo --bar", "foo --bar", 9},
216 {"simple", "foo bars foobars", "foo\nbars\nfoob\nars", 4},
217 {"limit", "foo bar", "foo\nbar", 5},
218 {"remove white spaces", "foo \nb ar ", "foo\nb\nar", 4},
219 {"white space trail width", "foo\nb\t a\n bar", "foo\nb\t a\n bar", 4},
220 {"explicit_line_break", "foo bar foo\n", "foo\nbar\nfoo\n", 4},
221 {"explicit_breaks", "\nfoo bar\n\n\nfoo\n", "\nfoo\nbar\n\n\nfoo\n", 4},
222 {"example", " This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* foo \nbar ", " This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* foo\nbar", 6},
223 {"style_code_dont_affect_length", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", "\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m", 7},
224 {"style_code_dont_get_wrapped", "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m", "\x1b[38;2;249;38;114m(\x1b[0m\x1b[38;2;248;248;242mjust\nanother\ntest\x1b[38;2;249;38;114m)\x1b[0m", 7},
225 {"osc8_wrap", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\ สวัสดีสวัสดี\x1b]8;;\x1b\\", "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\\nสวัสดีสวัสดี\x1b]8;;\x1b\\", 8},
226 {"tab", "foo\tbar", "foo\nbar", 3},
227 {"Narrow NBSP", "0\u202f1\u202f2\u202f3\u202f4", "0\u202f1\u202f2\u202f3\n4", 7},
228 // Paragraph Separator usually takes one character width
229 // while printing it on terminal, but ansi considers this zero width.
230 {"Paragraph Separator", "0\u20291\u20292\u20293\u20294", "0\u20291\u20292\u20293\u20294", 7},
231 {"Medium Mathematical Space", "0\u205f1\u205f2\u205f3\u205f4", "0\u205f1\u205f2\u205f3\n4", 7},
232 {"Ideagraphic space", "0\u30001\u30002\u30003\u3000", "0\u30001\u30002\n3\u3000", 7},
233 {
234 name: "japanese with white spaces narrow",
235 input: `耐許ヱヨカハ調出あゆ監件び理別よン國給災レホチ権輝モエフ会割もフ響3現エツ文時しだびほ経機ムイメフ敗文ヨク現義なさド請情ゆじょて憶主管州けでふく。排ゃわつげ美刊ヱミ出見ツ南者オ抜豆ハトロネ論索モネニイ任償スヲ話破リヤヨ秒止口イセソス止央のさ食周健でてつだ官送ト読聴遊容ひるべ。際ぐドらづ市居ネムヤ研校35岩6繹ごわク報拐イ革深52球ゃレスご究東スラ衝3間ラ録占たス。
236 禁にンご忘康ざほぎル騰般ねど事超スんいう真表何カモ自浩ヲシミ図客線るふ静王ぱーま写村月掛焼詐面ぞゃ。昇強ごントほ価保キ族85岡モテ恋困ひりこな刊並せご出来ぼぎむう点目ヲウ止環公ニレ事応タス必書タメムノ当84無信升ちひょ。価ーぐ中客テサ告覧ヨトハ極整ラ得95稿はかラせ江利ス宏丸霊ミ考整ス静将ず業巨職ノラホ収嗅ざな。`,
237 expected: `耐許ヱヨカハ
238 調出あゆ監件
239 び理別よン國
240 給災レホチ権
241 輝モエフ会割
242 もフ響3現エツ
243 文時しだびほ
244 経機ムイメフ
245 敗文ヨク現義
246 なさド請情ゆ
247 じょて憶主管
248 州けでふく。
249 排ゃわつげ美
250 刊ヱミ出見ツ
251 南者オ抜豆ハ
252 トロネ論索モ
253 ネニイ任償ス
254 ヲ話破リヤヨ
255 秒止口イセソ
256 ス止央のさ食
257 周健でてつだ
258 官送ト読聴遊
259 容ひるべ。際
260 ぐドらづ市居
261 ネムヤ研校35
262 岩6繹ごわク報
263 拐イ革深52球
264 ゃレスご究東
265 スラ衝3間ラ録
266 占たス。
267 禁にンご忘康
268 ざほぎル騰般
269 ねど事超スん
270 いう真表何カ
271 モ自浩ヲシミ
272 図客線るふ静
273 王ぱーま写村
274 月掛焼詐面ぞ
275 ゃ。昇強ごン
276 トほ価保キ族8
277 5岡モテ恋困ひ
278 りこな刊並せ
279 ご出来ぼぎむ
280 う点目ヲウ止
281 環公ニレ事応
282 タス必書タメ
283 ムノ当84無信
284 升ちひょ。価
285 ーぐ中客テサ
286 告覧ヨトハ極
287 整ラ得95稿は
288 かラせ江利ス
289 宏丸霊ミ考整
290 ス静将ず業巨
291 職ノラホ収嗅
292 ざな。`,
293 width: 13,
294 },
295 {
296 name: "japanese with white spaces wide",
297 input: `耐許ヱヨカハ調出あゆ監件び理別よン國給災レホチ権輝モエフ会割もフ響3現エツ文時しだびほ経機ムイメフ敗文ヨク現義なさド請情ゆじょて憶主管州けでふく。排ゃわつげ美刊ヱミ出見ツ南者オ抜豆ハトロネ論索モネニイ任償スヲ話破リヤヨ秒止口イセソス止央のさ食周健でてつだ官送ト読聴遊容ひるべ。際ぐドらづ市居ネムヤ研校35岩6繹ごわク報拐イ革深52球ゃレスご究東スラ衝3間ラ録占たス。
298 禁にンご忘康ざほぎル騰般ねど事超スんいう真表何カモ自浩ヲシミ図客線るふ静王ぱーま写村月掛焼詐面ぞゃ。昇強ごントほ価保キ族85岡モテ恋困ひりこな刊並せご出来ぼぎむう点目ヲウ止環公ニレ事応タス必書タメムノ当84無信升ちひょ。価ーぐ中客テサ告覧ヨトハ極整ラ得95稿はかラせ江利ス宏丸霊ミ考整ス静将ず業巨職ノラホ収嗅ざな。`,
299 expected: `耐許ヱヨカハ調出あゆ監件び理別
300 よン國給災レホチ権輝モエフ会割
301 もフ響3現エツ文時しだびほ経機
302 ムイメフ敗文ヨク現義なさド請情
303 ゆじょて憶主管州けでふく。排ゃ
304 わつげ美刊ヱミ出見ツ南者オ抜豆
305 ハトロネ論索モネニイ任償スヲ話
306 破リヤヨ秒止口イセソス止央のさ
307 食周健でてつだ官送ト読聴遊容ひ
308 るべ。際ぐドらづ市居ネムヤ研校
309 35岩6繹ごわク報拐イ革深52球ゃ
310 レスご究東スラ衝3間ラ録占たス
311 。
312 禁にンご忘康ざほぎル騰般ねど事
313 超スんいう真表何カモ自浩ヲシミ
314 図客線るふ静王ぱーま写村月掛焼
315 詐面ぞゃ。昇強ごントほ価保キ族
316 85岡モテ恋困ひりこな刊並せご出
317 来ぼぎむう点目ヲウ止環公ニレ事
318 応タス必書タメムノ当84無信升ち
319 ひょ。価ーぐ中客テサ告覧ヨトハ
320 極整ラ得95稿はかラせ江利ス宏丸
321 霊ミ考整ス静将ず業巨職ノラホ収
322 嗅ざな。`,
323 width: 30,
324 },
325 }
326
327 func TestWrap(t *testing.T) {
328 format := "case %d, input:\n%s\n\nexpected:\n%s\n\ngot:\n%s\n\n"
329 for i, tc := range wrapCases {
330 t.Run(tc.name, func(t *testing.T) {
331 format := format
332 if strings.ContainsFunc(tc.input, func(r rune) bool {
333 return (r < 32 && r != '\n' && r != '\t') || // C0 control characters except LF and TAB
334 r == 127 || // DEL
335 (r >= 0x80 && r <= 0x9F) // C1 control characters
336 }) {
337 format = strings.ReplaceAll(format, "%s", "%q")
338 }
339 output := ansi.Wrap(tc.input, tc.width, "")
340 if output != tc.expected {
341 t.Errorf(format, i+1, tc.input, tc.expected, output)
342 }
343 })
344 }
345 }
346
347 func BenchmarkWrap(b *testing.B) {
348 input := "the quick brown foxxxxxxxxxxxxxxxx jumped over the lazy dog."
349 limit := 16
350 for i := 0; i < b.N; i++ {
351 _ = ansi.Wrap(input, limit, "")
352 }
353 }