-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·420 lines (339 loc) · 22.4 KB
/
test.js
File metadata and controls
executable file
·420 lines (339 loc) · 22.4 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import test from 'ava';
import chalk from 'chalk';
import hasAnsi from 'has-ansi';
import stripAnsi from 'strip-ansi';
import wrapAnsi from './index.js';
chalk.level = 1;
// When "hard" is false
const fixture = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
const fixture2 = '12345678\n901234567890';
const fixture3 = '12345678\n901234567890 12345';
const fixture4 = '12345678\n';
const fixture5 = '12345678\n ';
test('wraps string at 20 characters', t => {
const result = wrapAnsi(fixture, 20);
t.is(result, 'The quick brown \u001B[31mfox\u001B[39m\n\u001B[31mjumped over \u001B[39mthe lazy\n\u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the\u001B[39m\n\u001B[32municorn.\u001B[39m');
t.true(stripAnsi(result).split('\n').every(line => line.length <= 20));
});
test('wraps string at 30 characters', t => {
const result = wrapAnsi(fixture, 30);
t.is(result, 'The quick brown \u001B[31mfox jumped\u001B[39m\n\u001B[31mover \u001B[39mthe lazy \u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the unicorn.\u001B[39m');
t.true(stripAnsi(result).split('\n').every(line => line.length <= 30));
});
test('does not break strings longer than "cols" characters', t => {
const result = wrapAnsi(fixture, 5, {hard: false});
t.is(result, 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32municorn.\u001B[39m');
t.true(stripAnsi(result).split('\n').some(line => line.length > 5));
});
test('handles colored string that wraps on to multiple lines', t => {
const result = wrapAnsi(chalk.green('hello world') + ' hey!', 5, {hard: false});
const lines = result.split('\n');
t.true(hasAnsi(lines[0]));
t.true(hasAnsi(lines[1]));
t.false(hasAnsi(lines[2]));
});
test('does not prepend newline if first string is greater than "cols"', t => {
const result = wrapAnsi(chalk.green('hello') + '-world', 5, {hard: false});
t.is(result.split('\n').length, 1);
});
// When "hard" is true
test('breaks strings longer than "cols" characters', t => {
const result = wrapAnsi(fixture, 5, {hard: true});
t.is(result, 'The\nquick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32munico\u001B[39m\n\u001B[32mrn.\u001B[39m');
t.true(stripAnsi(result).split('\n').every(line => line.length <= 5));
});
test('removes last row if it contained only ansi escape codes', t => {
const result = wrapAnsi(chalk.green('helloworld'), 2, {hard: true});
t.true(stripAnsi(result).split('\n').every(x => x.length === 2));
});
test('does not prepend newline if first word is split', t => {
const result = wrapAnsi(chalk.green('hello') + 'world', 5, {hard: true});
t.is(result.split('\n').length, 2);
});
test('takes into account line returns inside input', t => {
t.is(wrapAnsi(fixture2, 10, {hard: true}), '12345678\n9012345678\n90');
});
test('word wrapping', t => {
t.is(wrapAnsi(fixture3, 15), '12345678\n901234567890\n12345');
});
test('does not pre-wrap long words when hard wrapping with wordWrap false', t => {
const defaultResult = wrapAnsi('hi, this https://IsAReallyLongWordButIDoNotKnowHowItShouldBehave.com', 32, {hard: true});
t.is(defaultResult, 'hi, this\nhttps://IsAReallyLongWordButIDoN\notKnowHowItShouldBehave.com');
const result = wrapAnsi('hi, this https://IsAReallyLongWordButIDoNotKnowHowItShouldBehave.com', 32, {hard: true, wordWrap: false});
t.is(result, 'hi, this https://IsAReallyLongWo\nrdButIDoNotKnowHowItShouldBehave\n.com');
const result2 = wrapAnsi('hi, this IsAReallyLongWordButIDoNotKnowHowItShouldBehave', 32, {hard: true, wordWrap: false});
t.is(result2, 'hi, this IsAReallyLongWordButIDo\nNotKnowHowItShouldBehave');
});
test('no word-wrapping', t => {
const result = wrapAnsi(fixture3, 15, {wordWrap: false});
t.is(result, '12345678\n901234567890 12\n345');
const result2 = wrapAnsi(fixture3, 5, {wordWrap: false});
t.is(result2, '12345\n678\n90123\n45678\n90 12\n345');
const result3 = wrapAnsi(fixture5, 5, {wordWrap: false});
t.is(result3, '12345\n678\n');
const result4 = wrapAnsi(fixture, 5, {wordWrap: false});
t.is(result4, 'The q\nuick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[39mthe l\nazy \u001B[32md\u001B[39m\n\u001B[32mog an\u001B[39m\n\u001B[32md the\u001B[39m\n\u001B[32mn ran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe u\u001B[39m\n\u001B[32mnicor\u001B[39m\n\u001B[32mn.\u001B[39m');
});
test('no word-wrapping and no trimming', t => {
const result = wrapAnsi(fixture3, 13, {wordWrap: false, trim: false});
t.is(result, '12345678\n901234567890 \n12345');
const result2 = wrapAnsi(fixture4, 5, {wordWrap: false, trim: false});
t.is(result2, '12345\n678\n');
const result3 = wrapAnsi(fixture5, 5, {wordWrap: false, trim: false});
t.is(result3, '12345\n678\n ');
const result4 = wrapAnsi(fixture, 5, {wordWrap: false, trim: false});
t.is(result4, 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\n\u001B[31mjumpe\u001B[39m\n\u001B[31md ove\u001B[39m\n\u001B[31mr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\n\u001B[32mand t\u001B[39m\n\u001B[32mhen r\u001B[39m\n\u001B[32man aw\u001B[39m\n\u001B[32may wi\u001B[39m\n\u001B[32mth th\u001B[39m\n\u001B[32me uni\u001B[39m\n\u001B[32mcorn.\u001B[39m');
});
test('supports fullwidth characters', t => {
t.is(wrapAnsi('안녕하세', 4, {hard: true}), '안녕\n하세');
});
test('supports unicode surrogate pairs', t => {
t.is(wrapAnsi('a\uD83C\uDE00bc', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc');
t.is(wrapAnsi('a\uD83C\uDE00bc\uD83C\uDE00d\uD83C\uDE00', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc\n\uD83C\uDE00\nd\n\uD83C\uDE00');
});
test('does not split multi-codepoint grapheme clusters across lines', t => {
// ZWJ family emoji (7 codepoints, width 2)
t.is(wrapAnsi('a👨👩👧👦b', 2, {hard: true}), 'a\n👨👩👧👦\nb');
// Flag emoji (2 regional indicators, width 2)
t.is(wrapAnsi('a🇺🇸b', 2, {hard: true}), 'a\n🇺🇸\nb');
// Skin tone modifier (2 codepoints, width 2)
t.is(wrapAnsi('a👋🏽b', 2, {hard: true}), 'a\n👋🏽\nb');
// Tamil combining character (2 codepoints, width 1)
t.is(wrapAnsi('நிநி', 1, {hard: true}), 'நி\nநி');
// Multiple grapheme clusters fitting on one line
t.is(wrapAnsi('🇺🇸🇬🇧', 4, {hard: true}), '🇺🇸🇬🇧');
t.is(wrapAnsi('🇺🇸🇬🇧', 3, {hard: true}), '🇺🇸\n🇬🇧');
// Grapheme cluster at exact column boundary
t.is(wrapAnsi('ab👨👩👧👦cd', 4, {hard: true}), 'ab👨👩👧👦\ncd');
t.is(wrapAnsi('ab🇺🇸cd', 4, {hard: true}), 'ab🇺🇸\ncd');
// Soft wrapping does not split grapheme clusters
t.is(wrapAnsi('test 👨👩👧👦', 4), 'test\n👨👩👧👦');
// Colored grapheme clusters preserve ANSI codes across wraps
t.is(stripAnsi(wrapAnsi(chalk.red('a👨👩👧👦b'), 2, {hard: true})), 'a\n👨👩👧👦\nb');
});
test('#23, properly wraps whitespace with no trimming', t => {
t.is(wrapAnsi(' ', 2, {trim: false}), ' \n ');
t.is(wrapAnsi(' ', 2, {trim: false, hard: true}), ' \n ');
});
test('#24, trims leading and trailing whitespace only on actual wrapped lines and only with trimming', t => {
t.is(wrapAnsi(' foo bar ', 3), 'foo\nbar');
t.is(wrapAnsi(' foo bar ', 6), 'foo\nbar');
t.is(wrapAnsi(' foo bar ', 42), 'foo bar');
t.is(wrapAnsi(' foo bar ', 42, {trim: false}), ' foo bar ');
});
test('#24, trims leading and trailing whitespace inside a color block only on actual wrapped lines and only with trimming', t => {
t.is(wrapAnsi(chalk.blue(' foo bar '), 6), chalk.blue('foo\nbar'));
t.is(wrapAnsi(chalk.blue(' foo bar '), 42), chalk.blue('foo bar'));
t.is(wrapAnsi(chalk.blue(' foo bar '), 42, {trim: false}), chalk.blue(' foo bar '));
});
test('#25, properly wraps whitespace between words with no trimming', t => {
t.is(wrapAnsi('foo bar', 3), 'foo\nbar');
t.is(wrapAnsi('foo bar', 3, {hard: true}), 'foo\nbar');
t.is(wrapAnsi('foo bar', 3, {trim: false}), 'foo\n \nbar');
t.is(wrapAnsi('foo bar', 3, {trim: false, hard: true}), 'foo\n \nbar');
});
test('#26, does not multiply leading spaces with no trimming', t => {
t.is(wrapAnsi(' a ', 10, {trim: false}), ' a ');
t.is(wrapAnsi(' a ', 10, {trim: false}), ' a ');
});
test('#27, does not remove spaces in line with ansi escapes when no trimming', t => {
t.is(wrapAnsi(chalk.bgGreen(` ${chalk.black('OK')} `), 100, {trim: false}), chalk.bgGreen(` ${chalk.black('OK')} `));
t.is(wrapAnsi(chalk.bgGreen(` ${chalk.black('OK')} `), 100, {trim: false}), chalk.bgGreen(` ${chalk.black('OK')} `));
t.is(wrapAnsi(chalk.bgGreen(' hello '), 10, {hard: true, trim: false}), chalk.bgGreen(' hello '));
});
test('#43, preserves nested foreground and background styles on every wrapped line', t => {
const result = wrapAnsi(chalk.bgGreen.black('test'), 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[42m\u001B[30mte\u001B[39m\u001B[49m\n\u001B[42m\u001B[30mst\u001B[39m\u001B[49m');
});
test('#43, preserves stacked modifiers and colors on every wrapped line', t => {
const result = wrapAnsi(chalk.blue.bold('test'), 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[34m\u001B[1mte\u001B[22m\u001B[39m\n\u001B[34m\u001B[1mst\u001B[22m\u001B[39m');
});
test('#43, preserves combined SGR parameters across wrapped lines', t => {
const input = '\u001B[1;34mtest\u001B[39;22m';
const result = wrapAnsi(input, 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[1;34mte\u001B[39m\u001B[22m\n\u001B[1m\u001B[34mst\u001B[39;22m');
});
test('#43, preserves truecolor foreground and background styles on every wrapped line', t => {
const input = '\u001B[48;2;255;0;0m\u001B[38;2;0;0;0mtest\u001B[39m\u001B[49m';
const result = wrapAnsi(input, 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[48;2;255;0;0m\u001B[38;2;0;0;0mte\u001B[39m\u001B[49m\n\u001B[48;2;255;0;0m\u001B[38;2;0;0;0mst\u001B[39m\u001B[49m');
});
test('#43, does not treat malformed extended color parameters as modifiers', t => {
const malformedForeground = wrapAnsi('\u001B[38;2;255mab\u001B[0m', 1, {hard: true, trim: false, wordWrap: false});
const malformedBackground = wrapAnsi('\u001B[48;5mab\u001B[0m', 1, {hard: true, trim: false, wordWrap: false});
t.is(malformedForeground, '\u001B[38;2;255ma\nb\u001B[0m');
t.is(malformedBackground, '\u001B[48;5ma\nb\u001B[0m');
});
test('#43, treats omitted SGR params as reset', t => {
const colorThenReset = wrapAnsi('\u001B[31;mab\u001B[0m', 1, {hard: true, trim: false, wordWrap: false});
const boldResetThenColor = wrapAnsi('\u001B[1;;31mab\u001B[0m', 1, {hard: true, trim: false, wordWrap: false});
t.is(colorThenReset, '\u001B[31;ma\nb\u001B[0m');
t.is(boldResetThenColor, '\u001B[1;;31ma\u001B[39m\n\u001B[31mb\u001B[0m');
});
test('#43, preserves C1 CSI SGR styles across wrapped lines', t => {
const result = wrapAnsi('\u009B1;34mtest\u009B39;22m', 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u009B1;34mte\u001B[39m\u001B[22m\n\u001B[1m\u001B[34mst\u009B39;22m');
});
test('#43, preserves underline color SGR styles across wrapped lines', t => {
const result = wrapAnsi('\u001B[58;5;196mtest\u001B[59m', 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[58;5;196mte\u001B[59m\n\u001B[58;5;196mst\u001B[59m');
});
test('#43, preserves bold and dim styles across wrapped lines', t => {
const result = wrapAnsi('\u001B[1m\u001B[2mtest\u001B[22m', 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[1m\u001B[2mte\u001B[22m\u001B[22m\n\u001B[1m\u001B[2mst\u001B[22m');
});
test('#43, clears both bold and dim when reset 22 appears at wrapped line start', t => {
const result = wrapAnsi('\u001B[1m\u001B[2mab\u001B[22mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[1m\u001B[2ma\u001B[22m\u001B[22m\n\u001B[1m\u001B[2mb\u001B[22m\u001B[22m\n\u001B[22mc\nd');
});
test('#43, does not reopen styles that are reset by a combined SGR sequence at wrapped line start', t => {
const result = wrapAnsi('\u001B[1;2;31mab\u001B[22;39mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[1;2;31ma\u001B[39m\u001B[22m\u001B[22m\n\u001B[1m\u001B[2m\u001B[31mb\u001B[39m\u001B[22m\u001B[22m\n\u001B[22;39mc\nd');
});
test('#43, handles C1 SGR reset at wrapped line start for stacked modifiers', t => {
const result = wrapAnsi('\u009B1m\u009B2mab\u009B22mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u009B1m\u009B2ma\u001B[22m\u001B[22m\n\u001B[1m\u001B[2mb\u001B[22m\u001B[22m\n\u009B22mc\nd');
});
test('#43, does not duplicate reopen output when the same modifier is applied repeatedly', t => {
const result = wrapAnsi('\u001B[1m\u001B[1mtest\u001B[22m', 2, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[1m\u001B[1mte\u001B[22m\n\u001B[1mst\u001B[22m');
});
test('#43, does not reopen modifiers that are immediately reset at wrapped line start', t => {
const result = wrapAnsi('\u001B[1mab\u001B[22mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[1ma\u001B[22m\n\u001B[1mb\u001B[22m\n\u001B[22mc\nd');
});
test('#43, does not reopen colors that are immediately reset at wrapped line start', t => {
const result = wrapAnsi('\u001B[31mab\u001B[39mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[31ma\u001B[39m\n\u001B[31mb\u001B[39m\n\u001B[39mc\nd');
});
test('#43, does not reopen background when reset at wrapped line start while preserving foreground', t => {
const result = wrapAnsi('\u001B[31m\u001B[42mab\u001B[49mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[31m\u001B[42ma\u001B[49m\u001B[39m\n\u001B[31m\u001B[42mb\u001B[49m\u001B[39m\n\u001B[31m\u001B[49mc\u001B[39m\n\u001B[31md');
});
test('#43, does not reopen non-22 modifiers that are immediately reset at wrapped line start', t => {
const result = wrapAnsi('\u001B[9mab\u001B[29mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[9ma\u001B[29m\n\u001B[9mb\u001B[29m\n\u001B[29mc\nd');
});
test('#43, does not reopen styles reset after a leading hyperlink escape at wrapped line start', t => {
const belResult = wrapAnsi('\u001B[31mab\u001B]8;;https://example.com\u0007\u001B[39mc\u001B]8;;\u0007', 1, {hard: true, trim: false, wordWrap: false});
const stResult = wrapAnsi('\u001B[31mab\u001B]8;;https://example.com\u001B\\\u001B[39mc\u001B]8;;\u001B\\', 1, {hard: true, trim: false, wordWrap: false});
t.is(belResult, '\u001B[31ma\u001B[39m\n\u001B[31mb\u001B[39m\n\u001B]8;;https://example.com\u0007\u001B[39mc\u001B]8;;\u0007');
t.is(stResult, '\u001B[31ma\u001B[39m\n\u001B[31mb\u001B[39m\n\u001B]8;;https://example.com\u001B\\\u001B[39mc\u001B]8;;\u001B\\');
});
test('#43, does not reopen styles when a full reset is immediately applied at wrapped line start', t => {
const result = wrapAnsi('\u001B[31;1mab\u001B[0mcd', 1, {hard: true, trim: false, wordWrap: false});
t.is(result, '\u001B[31;1ma\u001B[22m\u001B[39m\n\u001B[31m\u001B[1mb\u001B[22m\u001B[39m\n\u001B[0mc\nd');
});
test('#35, wraps hyperlinks, preserving clickability in supporting terminals', t => {
const result1 = wrapAnsi('Check out \u001B]8;;https://www.example.com\u0007my website\u001B]8;;\u0007, it is \u001B]8;;https://www.example.com\u0007supercalifragilisticexpialidocious\u001B]8;;\u0007.', 16, {hard: true});
t.is(result1, 'Check out \u001B]8;;https://www.example.com\u0007my\u001B]8;;\u0007\n\u001B]8;;https://www.example.com\u0007website\u001B]8;;\u0007, it is\n\u001B]8;;https://www.example.com\u0007supercalifragili\u001B]8;;\u0007\n\u001B]8;;https://www.example.com\u0007sticexpialidocio\u001B]8;;\u0007\n\u001B]8;;https://www.example.com\u0007us\u001B]8;;\u0007.');
const result2 = wrapAnsi(`Check out \u001B]8;;https://www.example.com\u0007my \uD83C\uDE00 ${chalk.bgGreen('website')}\u001B]8;;\u0007, it ${chalk.bgRed('is \u001B]8;;https://www.example.com\u0007super\uD83C\uDE00califragilisticexpialidocious\u001B]8;;\u0007')}.`, 16, {hard: true});
t.is(result2, 'Check out \u001B]8;;https://www.example.com\u0007my 🈀\u001B]8;;\u0007\n\u001B]8;;https://www.example.com\u0007\u001B[42mwebsite\u001B[49m\u001B]8;;\u0007, it \u001B[41mis\u001B[49m\n\u001B[41m\u001B]8;;https://www.example.com\u0007super🈀califragi\u001B]8;;\u0007\u001B[49m\n\u001B[41m\u001B]8;;https://www.example.com\u0007listicexpialidoc\u001B]8;;\u0007\u001B[49m\n\u001B[41m\u001B]8;;https://www.example.com\u0007ious\u001B]8;;\u0007\u001B[49m.');
});
test('wraps ST-terminated hyperlinks correctly', t => {
// Single word wider than columns, forcing hard wrap through wrapWord.
const result = wrapAnsi('\u001B]8;;https://example.com\u001B\\abcdefghij\u001B]8;;\u001B\\', 5, {hard: true});
t.is(result, '\u001B]8;;https://example.com\u001B\\abcde\u001B]8;;\u0007\n\u001B]8;;https://example.com\u0007fghij\u001B]8;;\u001B\\');
// Soft wrap - 10-char visible text should fit in 20 columns without wrapping
const result2 = wrapAnsi('\u001B]8;;https://example.com\u001B\\Click here\u001B]8;;\u001B\\', 20);
t.false(result2.includes('\n'));
t.true(result2.includes('\u001B]8;;https://example.com\u001B\\'));
// Soft wrap that straddles a line break - hyperlink should be closed/reopened
const result3 = wrapAnsi('\u001B]8;;https://example.com\u001B\\hello world\u001B]8;;\u001B\\', 5);
const lines = result3.split('\n');
t.is(lines.length, 2);
t.is(stripAnsi(lines[0]), 'hello');
t.is(stripAnsi(lines[1]), 'world');
// Each line should have the hyperlink open and close tags
t.true(lines[0].includes('\u001B]8;;https://example.com'));
t.true(lines[1].includes('\u001B]8;;https://example.com'));
// Mixed BEL and ST terminators in same string
const result4 = wrapAnsi(
'\u001B]8;;https://a.com\u0007hello\u001B]8;;\u0007 \u001B]8;;https://b.com\u001B\\world\u001B]8;;\u001B\\',
20,
);
t.false(result4.includes('\n'));
});
test('does not mix URLs when wrapping multiple BEL-terminated hyperlinks', t => {
const result = wrapAnsi('\u001B]8;;https://a.com\u0007one\u001B]8;;\u0007 \u001B]8;;https://b.com\u0007twothree\u001B]8;;\u0007', 4, {hard: true});
const lines = result.split('\n');
t.deepEqual(lines.map(line => stripAnsi(line)), ['one', 'twot', 'hree']);
t.true(lines[0].includes('https://a.com'));
t.false(lines[0].includes('https://b.com'));
t.true(lines[1].includes('https://b.com'));
t.false(lines[1].includes('https://a.com'));
t.true(lines[2].includes('https://b.com'));
t.false(lines[2].includes('https://a.com'));
});
test('does not mix URLs when wrapping multiple ST-terminated hyperlinks', t => {
const result = wrapAnsi('\u001B]8;;https://a.com\u001B\\one\u001B]8;;\u001B\\ \u001B]8;;https://b.com\u001B\\twothree\u001B]8;;\u001B\\', 4, {hard: true});
const lines = result.split('\n');
t.deepEqual(lines.map(line => stripAnsi(line)), ['one', 'twot', 'hree']);
t.true(lines[0].includes('https://a.com'));
t.false(lines[0].includes('https://b.com'));
t.true(lines[1].includes('https://b.com'));
t.false(lines[1].includes('https://a.com'));
t.true(lines[2].includes('https://b.com'));
t.false(lines[2].includes('https://a.com'));
});
test('does not scan ahead to a later BEL hyperlink from an invalid escape', t => {
const result = wrapAnsi('\u001Bfoo abc \u001B]8;;https://ok.com\u0007ok\u001B]8;;\u0007', 4, {hard: true});
const lines = result.split('\n');
const abcLine = lines.find(line => stripAnsi(line).includes('abc'));
const okLine = lines.find(line => stripAnsi(line).includes('ok'));
t.truthy(abcLine);
t.truthy(okLine);
t.false(abcLine.includes('https://ok.com'));
t.true(okLine.includes('https://ok.com'));
});
test('does not scan ahead to a later ST hyperlink from an invalid escape', t => {
const result = wrapAnsi('\u001Bfoo abc \u001B]8;;https://ok.com\u001B\\ok\u001B]8;;\u001B\\', 4, {hard: true});
const lines = result.split('\n');
const abcLine = lines.find(line => stripAnsi(line).includes('abc'));
const okLine = lines.find(line => stripAnsi(line).includes('ok'));
t.truthy(abcLine);
t.truthy(okLine);
t.false(abcLine.includes('https://ok.com'));
t.true(okLine.includes('https://ok.com'));
});
test('does not scan ahead from an unterminated OSC prelude to a later BEL hyperlink', t => {
const result = wrapAnsi('\u001B]8;;unterminated abc \u001B]8;;https://ok.com\u0007ok\u001B]8;;\u0007', 5, {hard: true});
const lines = result.split('\n');
const abcLine = lines.find(line => stripAnsi(line).includes('abc'));
const okLine = lines.find(line => stripAnsi(line).includes('ok'));
t.truthy(abcLine);
t.truthy(okLine);
t.false(abcLine.includes('https://ok.com'));
t.true(okLine.includes('https://ok.com'));
});
test('does not scan ahead from an unterminated OSC prelude to a later ST hyperlink', t => {
const result = wrapAnsi('\u001B]8;;unterminated abc \u001B]8;;https://ok.com\u001B\\ok\u001B]8;;\u001B\\', 5, {hard: true});
const lines = result.split('\n');
const abcLine = lines.find(line => stripAnsi(line).includes('abc'));
const okLine = lines.find(line => stripAnsi(line).includes('ok'));
t.truthy(abcLine);
t.truthy(okLine);
t.false(abcLine.includes('https://ok.com'));
t.true(okLine.includes('https://ok.com'));
});
test('covers non-SGR/non-hyperlink ansi escapes', t => {
t.is(wrapAnsi('Hello, \u001B[1D World!', 8), 'Hello,\u001B[1D\nWorld!');
t.is(wrapAnsi('Hello, \u001B[1D World!', 8, {trim: false}), 'Hello, \u001B[1D \nWorld!');
});
test('#39, normalizes newlines', t => {
t.is(wrapAnsi('foobar\r\nfoobar\r\nfoobar\nfoobar', 3, {hard: true}), 'foo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar');
t.is(wrapAnsi('foo bar\r\nfoo bar\r\nfoo bar\nfoo bar', 3), 'foo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar');
});
test('#54, expands tabs before wrapping', t => {
const result = wrapAnsi('\t\t\t\ttestingtesting', 10, {hard: true, trim: false});
t.is(result, ' \n \n \n testingt\nesting');
});
test('#54, uses tab stops while expanding tabs', t => {
const result = wrapAnsi('1234\ttest', 10, {hard: true, trim: false});
t.is(result, '1234 \ntest');
});
test('#54, tab expansion ignores ANSI codes when computing column position', t => {
const result = wrapAnsi(chalk.red('ab') + '\tcd', 20);
t.is(stripAnsi(result), 'ab cd');
});