-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(table): Wrap ANSI codes for multiline cells (#657)
- Loading branch information
1 parent
4e63862
commit 5118a1c
Showing
6 changed files
with
278 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ansiRegexSource } from "../_utils.ts"; | ||
import { assertEquals } from "../../dev_deps.ts"; | ||
|
||
Deno.test(`table - ansiRegexSource`, () => { | ||
const DIGITS = String.raw`\d+`; | ||
// All open and close ANSI codes taken from calls to `code(...)` in | ||
// https://deno.land/std@0.196.0/fmt/colors.ts | ||
const ansiCodes = [ | ||
{ open: [0], close: 0 }, | ||
{ open: [1], close: 22 }, | ||
{ open: [2], close: 22 }, | ||
{ open: [3], close: 23 }, | ||
{ open: [4], close: 24 }, | ||
{ open: [7], close: 27 }, | ||
{ open: [8], close: 28 }, | ||
{ open: [9], close: 29 }, | ||
{ open: [30], close: 39 }, | ||
{ open: [31], close: 39 }, | ||
{ open: [32], close: 39 }, | ||
{ open: [33], close: 39 }, | ||
{ open: [34], close: 39 }, | ||
{ open: [35], close: 39 }, | ||
{ open: [36], close: 39 }, | ||
{ open: [37], close: 39 }, | ||
{ open: [90], close: 39 }, | ||
{ open: [91], close: 39 }, | ||
{ open: [92], close: 39 }, | ||
{ open: [93], close: 39 }, | ||
{ open: [94], close: 39 }, | ||
{ open: [95], close: 39 }, | ||
{ open: [96], close: 39 }, | ||
{ open: [97], close: 39 }, | ||
{ open: [40], close: 49 }, | ||
{ open: [41], close: 49 }, | ||
{ open: [42], close: 49 }, | ||
{ open: [43], close: 49 }, | ||
{ open: [44], close: 49 }, | ||
{ open: [45], close: 49 }, | ||
{ open: [46], close: 49 }, | ||
{ open: [47], close: 49 }, | ||
{ open: [100], close: 49 }, | ||
{ open: [101], close: 49 }, | ||
{ open: [102], close: 49 }, | ||
{ open: [103], close: 49 }, | ||
{ open: [104], close: 49 }, | ||
{ open: [105], close: 49 }, | ||
{ open: [106], close: 49 }, | ||
{ open: [107], close: 49 }, | ||
{ open: [38, 5, DIGITS], close: 39 }, | ||
{ open: [48, 5, DIGITS], close: 49 }, | ||
{ open: [38, 2, DIGITS, DIGITS, DIGITS], close: 39 }, | ||
{ open: [48, 2, DIGITS, DIGITS, DIGITS], close: 49 }, | ||
]; | ||
|
||
const expect = String.raw`\x1b\[(?:${ | ||
[...new Set(ansiCodes.map(({ close }) => close))] | ||
.map((kind) => { | ||
const opens = ansiCodes | ||
.filter(({ close }) => close === kind) | ||
.map(({ open }) => open.join(";")); | ||
return String.raw`(?<_${kind}>${ | ||
[...new Set([String(kind), ...opens])].sort((a, b) => | ||
a.localeCompare(b, "en-US", { numeric: true }) | ||
).join("|") | ||
})`; | ||
}) | ||
.join("|") | ||
})m`; | ||
|
||
assertEquals(ansiRegexSource, expect); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Table } from "../table.ts"; | ||
import { assertEquals } from "../../dev_deps.ts"; | ||
|
||
const tests: { | ||
description: string; | ||
content: string[]; | ||
width: number; | ||
expect: string; | ||
}[] = [ | ||
{ | ||
description: "wrapping of ANSI codes within cell", | ||
// colors.red("Hello, world!") | ||
content: ["\x1b[31mHello, world!\x1b[39m"], | ||
width: 6, | ||
expect: ` | ||
┌────────┐ | ||
│ \x1b[31mHello,\x1b[39m │ | ||
│ \x1b[31mworld!\x1b[39m │ | ||
└────────┘`.slice(1), | ||
}, | ||
{ | ||
description: "wrapping of chained ANSI codes within cell", | ||
// colors.cyan.bold.underline("Hello, world!") | ||
content: ["\x1b[4m\x1b[1m\x1b[36mHello, world!\x1b[39m\x1b[22m\x1b[24m"], | ||
width: 6, | ||
expect: ` | ||
┌────────┐ | ||
│ \x1b[4m\x1b[1m\x1b[36mHello,\x1b[39m\x1b[22m\x1b[24m │ | ||
│ \x1b[4m\x1b[1m\x1b[36mworld!\x1b[39m\x1b[22m\x1b[24m │ | ||
└────────┘`.slice(1), | ||
}, | ||
{ | ||
description: "wrapping of chained ANSI codes with intra-word line breaking", | ||
// colors.cyan("Wrapping over multiple lines") | ||
content: ["\x1b[36mWrapping over multiple lines\x1b[39m"], | ||
width: 6, | ||
expect: ` | ||
┌────────┐ | ||
│ \x1b[36mWrappi\x1b[39m │ | ||
│ \x1b[36mng\x1b[39m │ | ||
│ \x1b[36mover\x1b[39m │ | ||
│ \x1b[36mmultip\x1b[39m │ | ||
│ \x1b[36mle\x1b[39m │ | ||
│ \x1b[36mlines\x1b[39m │ | ||
└────────┘`.slice(1), | ||
}, | ||
{ | ||
description: "wrapping of nested ANSI color codes within cell", | ||
// colors.red(`Red ${colors.yellow("and yellow")} text`) | ||
content: [ | ||
"\x1b[31mRed \x1b[33mand yellow\x1b[31m text\x1b[39m", | ||
"Cell 2", | ||
], | ||
width: 12, | ||
expect: ` | ||
┌──────────────┬────────┐ | ||
│ \x1b[31mRed \x1b[33mand\x1b[39m │ Cell 2 │ | ||
│ \x1b[33myellow\x1b[31m text\x1b[39m │ │ | ||
└──────────────┴────────┘`.slice(1), | ||
}, | ||
{ | ||
description: "wrapping of nested mixed ANSI codes within cell", | ||
// colors.rgb24(`Colorful ${colors.underline("and underlined")} text`, 0xff8800) | ||
content: [ | ||
"\x1b[38;2;255;136;0mColorful \x1b[4mand underlined\x1b[24m text\x1b[39m", | ||
], | ||
width: 15, | ||
expect: ` | ||
┌─────────────────┐ | ||
│ \x1b[38;2;255;136;0mColorful \x1b[4mand\x1b[24m\x1b[39m │ | ||
│ \x1b[38;2;255;136;0m\x1b[4munderlined\x1b[24m text\x1b[39m │ | ||
└─────────────────┘`.slice(1), | ||
}, | ||
]; | ||
|
||
for (const { description, content, width, expect } of tests) { | ||
const actual = Table.from([content]) | ||
.border(true) | ||
.columns([{ maxWidth: width, minWidth: width }]) | ||
.toString(); | ||
|
||
// // Uncomment for visual checking of output | ||
// console.log(`actual\n${actual}`); | ||
// console.log(`expect\n${expect}`); | ||
// console.log(JSON.stringify({ actual, expect }, null, '\t')); | ||
|
||
Deno.test(`table - ${description}`, () => { | ||
assertEquals(actual, expect); | ||
}); | ||
} |