Skip to content

Commit

Permalink
Add countAnsiEscapeCodes option (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored May 6, 2023
1 parent f85812f commit 6e6993b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
9 changes: 8 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ export type Options = {
@default true
*/
readonly ambiguousIsNarrow: boolean;
readonly ambiguousIsNarrow?: boolean;

/**
Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted.
@default false
*/
readonly countAnsiEscapeCodes?: boolean;
};

/**
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ export default function stringWidth(string, options) {

options = {
ambiguousIsNarrow: true,
countAnsiEscapeCodes: false,
...options,
};

string = stripAnsi(string);
if (!options.countAnsiEscapeCodes) {
string = stripAnsi(string);
}

if (string.length === 0) {
return 0;
Expand Down
5 changes: 3 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import {expectType} from 'tsd';
import stringWidth from './index.js';

expectType<number>(stringWidth('古'));

expectType<number>(stringWidth('★', {ambiguousIsNarrow: true}));
expectType<number>(stringWidth('★', {}));
expectType<number>(stringWidth('★', {ambiguousIsNarrow: false}));
expectType<number>(stringWidth('\u001B[31m\u001B[39m', {countAnsiEscapeCodes: true}));
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ Type: `object`
##### ambiguousIsNarrow

Type: `boolean`\
Default: `false`
Default: `true`

Count [ambiguous width characters](https://www.unicode.org/reports/tr11/#Ambiguous) as having narrow width (count of 1) instead of wide width (count of 2).

##### countAnsiEscapeCodes

Type: `boolean`\
Default: `false`

Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted.

## Related

- [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test('main', t => {
t.is(stringWidth('안녕하세요'), 10);
t.is(stringWidth('A\uD83C\uDE00BC'), 5, 'surrogate');
t.is(stringWidth('\u001B[31m\u001B[39m'), 0);
t.is(stringWidth('\u001B[31m\u001B[39m', {countAnsiEscapeCodes: true}), 8);
t.is(stringWidth('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007'), 5);
t.is(stringWidth('\u{231A}'), 2, '⌚ default emoji presentation character (Emoji_Presentation)');
t.is(stringWidth('\u{2194}\u{FE0F}'), 2, '↔️ default text presentation character rendered as emoji');
Expand Down

0 comments on commit 6e6993b

Please sign in to comment.