Skip to content

Commit f20b3b8

Browse files
committed
fix(utils): fix long word wrapping with ansis styles in tables
1 parent fd8b0bc commit f20b3b8

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ansis from 'ansis';
22

33
export function formatAsciiLink(url: string): string {
4-
return ansis.underline.blueBright(url);
4+
// no underline because terminals recognize URLs, and nested ansis styles aren't handled by wrap-ansi
5+
return ansis.blueBright(url);
56
}

packages/utils/src/lib/text-formats/ascii/table.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ function wrapText(text: string, width: number | undefined): string {
158158
if (!width || getTextWidth(text) <= width) {
159159
return text;
160160
}
161+
if (text !== ansis.strip(text)) {
162+
return wrapAnsi(text, width, { hard: true });
163+
}
161164
const words = extractWords(text);
162165
const longWords = words.filter(word => word.length > width);
163166
const replacements = longWords.map(original => {

packages/utils/src/lib/text-formats/ascii/table.unit.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,69 @@ Code coverage:
453453
├──────────┼────────────┼─────────┼────────────┼──────────┼─────────┼──────────┤
454454
│ 81 │ 64 │ 92 │ 100 │ 95 │ 62 │ 6 │
455455
└──────────┴────────────┴─────────┴────────────┴──────────┴─────────┴──────────┘
456+
`.trim(),
457+
);
458+
});
459+
460+
it('should wrap ansi styles correctly', () => {
461+
const output = formatAsciiTable(
462+
{
463+
rows: [
464+
[''],
465+
[ansis.bold('💡 Integrate the Portal')],
466+
[''],
467+
[`${ansis.gray('❯')} Configure upload in code-pushup.config.ts`],
468+
[
469+
ansis.underline(
470+
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#portal-integration',
471+
),
472+
],
473+
[
474+
`${ansis.gray('❯')} npx code-pushup upload${ansis.gray(' - Upload previously collected report to the Portal')}`,
475+
],
476+
[
477+
ansis.underline(
478+
'https://github.com/code-pushup/cli/tree/main/packages/cli#upload-command',
479+
),
480+
],
481+
[''],
482+
],
483+
},
484+
{ padding: 4 },
485+
);
486+
487+
expect(ansis.strip(output)).toBe(
488+
`
489+
┌──────────────────────────────────────────────────────────────────────────────┐
490+
│ │
491+
│ 💡 Integrate the Portal │
492+
│ │
493+
│ ❯ Configure upload in code-pushup.config.ts │
494+
│ https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#po │
495+
│ rtal-integration │
496+
│ ❯ npx code-pushup upload - Upload previously collected report to the │
497+
│ Portal │
498+
│ https://github.com/code-pushup/cli/tree/main/packages/cli#upload-comma │
499+
│ nd │
500+
│ │
501+
└──────────────────────────────────────────────────────────────────────────────┘
502+
`.trim(),
503+
);
504+
expect(output).toBe(
505+
`
506+
${ansis.dim('┌──────────────────────────────────────────────────────────────────────────────┐')}
507+
${ansis.dim('│')} ${ansis.dim('│')}
508+
${ansis.dim('│')} ${ansis.bold('💡 Integrate the Portal')} ${ansis.dim('│')}
509+
${ansis.dim('│')} ${ansis.dim('│')}
510+
${ansis.dim('│')} ${ansis.gray('❯')} Configure upload in code-pushup.config.ts ${ansis.dim('│')}
511+
${ansis.dim('│')} ${ansis.underline('https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#po')} ${ansis.dim('│')}
512+
${ansis.dim('│')} ${ansis.underline('rtal-integration')} ${ansis.dim('│')}
513+
${ansis.dim('│')} ${ansis.gray('❯')} npx code-pushup upload${ansis.gray(' - Upload previously collected report to the')} ${ansis.dim('│')}
514+
${ansis.dim('│')} ${ansis.gray('Portal')} ${ansis.dim('│')}
515+
${ansis.dim('│')} ${ansis.underline('https://github.com/code-pushup/cli/tree/main/packages/cli#upload-comma')} ${ansis.dim('│')}
516+
${ansis.dim('│')} ${ansis.underline('nd')} ${ansis.dim('│')}
517+
${ansis.dim('│')} ${ansis.dim('│')}
518+
${ansis.dim('└──────────────────────────────────────────────────────────────────────────────┘')}
456519
`.trim(),
457520
);
458521
});

0 commit comments

Comments
 (0)