Skip to content

Commit

Permalink
fix: Correct ansi cell width calculation (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
nam-hle authored Nov 2, 2022
1 parent c73498f commit 28e8e6e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/alignSpanningCell.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import stringWidth from 'string-width';
import {
alignString,
} from './alignString';
Expand Down Expand Up @@ -58,7 +59,7 @@ export const alignVerticalRangeContent = (range: RangeConfig, content: string[],

return padCellVertically(content, availableRangeHeight, verticalAlignment).map((line) => {
if (line.length === 0) {
return ' '.repeat(content[0].length);
return ' '.repeat(stringWidth(content[0]));
}

return line;
Expand Down
31 changes: 31 additions & 0 deletions test/alignSpanningCell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
expect,
} from 'chai';
import chalk from 'chalk';
import {
alignVerticalRangeContent,
wrapRangeContent,
Expand Down Expand Up @@ -267,4 +268,34 @@ describe('alignVerticalRangeContent', () => {
]);
});
});

it('with ansi string', () => {
const result = alignVerticalRangeContent(baseRangeConfig, [
` ${chalk.red('ECMAScript')} (or ES) `,
' is a ',
' general-purpose ',
' programming ',
' language, ',
' standardised by ',
` ${chalk.bgGreen('Ecma')} ${chalk.bold.blue('International')} `,
' according to the ',
' document ECMA-262. ',
], {...baseSpanningCellContext,
rowHeights: [12]});

expect(result).to.deep.equal([
` ${chalk.red('ECMAScript')} (or ES) `,
' is a ',
' general-purpose ',
' programming ',
' language, ',
' standardised by ',
` ${chalk.bgGreen('Ecma')} ${chalk.bold.blue('International')} `,
' according to the ',
' document ECMA-262. ',
' ',
' ',
' ',
]);
});
});

0 comments on commit 28e8e6e

Please sign in to comment.