Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add countAnsiEscapeCodes option #48

Merged
merged 5 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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