Skip to content

Commit 28e8e6e

Browse files
authored
fix: Correct ansi cell width calculation (#214)
1 parent c73498f commit 28e8e6e

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/alignSpanningCell.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import stringWidth from 'string-width';
12
import {
23
alignString,
34
} from './alignString';
@@ -58,7 +59,7 @@ export const alignVerticalRangeContent = (range: RangeConfig, content: string[],
5859

5960
return padCellVertically(content, availableRangeHeight, verticalAlignment).map((line) => {
6061
if (line.length === 0) {
61-
return ' '.repeat(content[0].length);
62+
return ' '.repeat(stringWidth(content[0]));
6263
}
6364

6465
return line;

test/alignSpanningCell.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
expect,
33
} from 'chai';
4+
import chalk from 'chalk';
45
import {
56
alignVerticalRangeContent,
67
wrapRangeContent,
@@ -267,4 +268,34 @@ describe('alignVerticalRangeContent', () => {
267268
]);
268269
});
269270
});
271+
272+
it('with ansi string', () => {
273+
const result = alignVerticalRangeContent(baseRangeConfig, [
274+
` ${chalk.red('ECMAScript')} (or ES) `,
275+
' is a ',
276+
' general-purpose ',
277+
' programming ',
278+
' language, ',
279+
' standardised by ',
280+
` ${chalk.bgGreen('Ecma')} ${chalk.bold.blue('International')} `,
281+
' according to the ',
282+
' document ECMA-262. ',
283+
], {...baseSpanningCellContext,
284+
rowHeights: [12]});
285+
286+
expect(result).to.deep.equal([
287+
` ${chalk.red('ECMAScript')} (or ES) `,
288+
' is a ',
289+
' general-purpose ',
290+
' programming ',
291+
' language, ',
292+
' standardised by ',
293+
` ${chalk.bgGreen('Ecma')} ${chalk.bold.blue('International')} `,
294+
' according to the ',
295+
' document ECMA-262. ',
296+
' ',
297+
' ',
298+
' ',
299+
]);
300+
});
270301
});

0 commit comments

Comments
 (0)