Skip to content

Commit 7d4eb71

Browse files
committed
fix(utils): align table columns with unicode characters
1 parent 7a98181 commit 7d4eb71

File tree

5 files changed

+85
-31
lines changed

5 files changed

+85
-31
lines changed

package-lock.json

Lines changed: 63 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"rimraf": "^6.0.1",
4242
"semver": "^7.6.3",
4343
"simple-git": "^3.26.0",
44+
"string-width": "^8.1.0",
4445
"ts-morph": "^24.0.0",
4546
"tslib": "^2.6.2",
4647
"vscode-material-icons": "^0.1.1",

packages/utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"multi-progress-bars": "^5.0.3",
3838
"semver": "^7.6.0",
3939
"simple-git": "^3.20.0",
40+
"string-width": "^8.1.0",
4041
"ora": "^9.0.0",
4142
"zod": "^4.0.5"
4243
},

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ansis from 'ansis';
22
import type { TableCellAlignment } from 'build-md';
3+
import stringWidth from 'string-width';
34
import type {
45
Table,
56
TableAlignment,
@@ -133,7 +134,7 @@ function alignText(
133134
if (!width) {
134135
return text;
135136
}
136-
const missing = width - getTextWidth(text);
137+
const missing = width - stringWidth(text);
137138
switch (alignment) {
138139
case 'left':
139140
return `${text}${' '.repeat(missing)}`;
@@ -154,15 +155,11 @@ function getColumnWidths(table: NormalizedTable): number[] {
154155
...table.rows.map(row => row[index]),
155156
].filter(cell => cell != null);
156157
const texts = cells.map(cell => cell.text);
157-
const widths = texts.map(getTextWidth);
158+
const widths = texts.map(text => stringWidth(text));
158159
return Math.max(...widths);
159160
});
160161
}
161162

162-
function getTextWidth(text: string): number {
163-
return ansis.strip(text).length;
164-
}
165-
166163
function normalizeTable(table: Table): NormalizedTable {
167164
const rows = normalizeTableRows(table.rows, table.columns);
168165
const columns = normalizeTableColumns(table.columns);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,23 @@ Code coverage:
330330
/ 2.4 s ✖
331331
/about 720 ms ✔
332332
/contact n/a
333+
`.trim(),
334+
);
335+
});
336+
337+
it('should align columns with unicode characters correctly', () => {
338+
const output = formatAsciiTable({
339+
rows: [
340+
['❌', '/', '1.2 s'],
341+
['✅', '/contact', '612 ms'],
342+
],
343+
});
344+
expect(ansis.strip(output)).toBe(
345+
`
346+
┌────┬──────────┬────────┐
347+
│ ❌ │ / │ 1.2 s │
348+
│ ✅ │ /contact │ 612 ms │
349+
└────┴──────────┴────────┘
333350
`.trim(),
334351
);
335352
});

0 commit comments

Comments
 (0)