pixl-cli 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/cli.js +2 -15
  2. package/package.json +1 -1
  3. package/util.js +25 -1
  4. package/width.js +42 -1
  5. package/wrap.js +5 -24
  6. package/agents/string-width/.editorconfig +0 -12
  7. package/agents/string-width/.gitattributes +0 -1
  8. package/agents/string-width/.github/security.md +0 -3
  9. package/agents/string-width/.github/workflows/main.yml +0 -22
  10. package/agents/string-width/index.d.ts +0 -39
  11. package/agents/string-width/index.js +0 -207
  12. package/agents/string-width/index.test-d.ts +0 -7
  13. package/agents/string-width/license +0 -9
  14. package/agents/string-width/package.json +0 -65
  15. package/agents/string-width/readme.md +0 -66
  16. package/agents/string-width/test.js +0 -339
  17. package/agents/widest-line/.editorconfig +0 -12
  18. package/agents/widest-line/.gitattributes +0 -1
  19. package/agents/widest-line/.github/security.md +0 -3
  20. package/agents/widest-line/.github/workflows/main.yml +0 -21
  21. package/agents/widest-line/index.d.ts +0 -12
  22. package/agents/widest-line/index.js +0 -11
  23. package/agents/widest-line/license +0 -9
  24. package/agents/widest-line/package.json +0 -60
  25. package/agents/widest-line/readme.md +0 -26
  26. package/agents/widest-line/test.js +0 -8
  27. package/agents/word-wrap/.editorconfig +0 -13
  28. package/agents/word-wrap/.eslintrc.json +0 -122
  29. package/agents/word-wrap/.gitattributes +0 -10
  30. package/agents/word-wrap/.github/workflows/publish.yml +0 -19
  31. package/agents/word-wrap/.travis.yml +0 -13
  32. package/agents/word-wrap/.verb.md +0 -114
  33. package/agents/word-wrap/LICENSE +0 -21
  34. package/agents/word-wrap/README.md +0 -201
  35. package/agents/word-wrap/bower.json +0 -60
  36. package/agents/word-wrap/index.d.ts +0 -50
  37. package/agents/word-wrap/index.js +0 -61
  38. package/agents/word-wrap/package.json +0 -77
  39. package/agents/word-wrap/test.js +0 -69
  40. package/test.js +0 -20
package/cli.js CHANGED
@@ -298,25 +298,12 @@ var cli = module.exports = {
298
298
  max_col_widths[longest_col_idx]--;
299
299
  }
300
300
 
301
- // finally prune affected columns, trying to preserve ANSI color inside column value
301
+ // finally prune affected columns by display width, preserving ANSI and emoji
302
302
  rows.forEach( function(cols, idx) {
303
303
  cols.forEach( function(col, idy) {
304
304
  col = '' + col;
305
305
  if (stringWidth(col) > max_col_widths[idy]) {
306
- var suffix = '';
307
- var prefix = '';
308
-
309
- while (col.match(/^(\u001b\[[^m]*?m)/)) {
310
- prefix += RegExp.$1;
311
- col = col.replace(/^(\u001b\[[^m]*?m)/, '');
312
- }
313
-
314
- while (col.match(/(\u001b\[[^m]*?m)$/)) {
315
- suffix = RegExp.$1 + suffix;
316
- col = col.replace(/(\u001b\[[^m]*?m)$/, '');
317
- }
318
-
319
- cols[idy] = prefix + col.substring(0, max_col_widths[idy] - 1) + '…' + suffix;
306
+ cols[idy] = Width.truncate(col, max_col_widths[idy], '');
320
307
  } // too wide
321
308
  });
322
309
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixl-cli",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Tools for building command-line apps for Node.js.",
5
5
  "author": "Joseph Huckaby <jhuckaby@gmail.com>",
6
6
  "homepage": "https://github.com/jhuckaby/pixl-cli",
package/util.js CHANGED
@@ -9,7 +9,31 @@ var ansiPattern = new RegExp([
9
9
  // Split strings into user-perceived characters, including complete emoji sequences.
10
10
  var graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
11
11
 
12
+ function splitAnsiGraphemes(text) {
13
+ // Split a string into complete ANSI sequences and Unicode grapheme clusters.
14
+ // ANSI sequences are kept intact so callers can preserve them while editing text.
15
+ var units = [];
16
+ var pattern = new RegExp(ansiPattern.source, 'g');
17
+ var offset = 0;
18
+ var match = null;
19
+
20
+ var addText = function(value) {
21
+ for (var item of graphemeSegmenter.segment(value)) {
22
+ units.push({ text: item.segment, ansi: false });
23
+ }
24
+ };
25
+
26
+ while ((match = pattern.exec(text))) {
27
+ if (match.index > offset) addText( text.substring(offset, match.index) );
28
+ units.push({ text: match[0], ansi: true });
29
+ offset = pattern.lastIndex;
30
+ }
31
+ if (offset < text.length) addText( text.substring(offset) );
32
+ return units;
33
+ }
34
+
12
35
  module.exports = {
13
36
  ansiPattern: ansiPattern,
14
- graphemeSegmenter: graphemeSegmenter
37
+ graphemeSegmenter: graphemeSegmenter,
38
+ splitAnsiGraphemes: splitAnsiGraphemes
15
39
  };
package/width.js CHANGED
@@ -65,11 +65,52 @@ function widestLine(text) {
65
65
  return width;
66
66
  }
67
67
 
68
+ function truncate(text, width, suffix) {
69
+ // Truncate a string to an exact terminal display width. Preserve complete ANSI
70
+ // sequences and grapheme clusters, and retain trailing SGR codes to reset styles.
71
+ if (typeof(text) != 'string') text = '' + text;
72
+ if (suffix == null) suffix = '';
73
+ if (stringWidth(text) <= width) return text;
74
+
75
+ var suffixWidth = stringWidth(suffix);
76
+ var contentWidth = Math.max(0, width - suffixWidth);
77
+ var outputWidth = 0;
78
+ var output = '';
79
+ var units = Util.splitAnsiGraphemes(text);
80
+ var trailingAnsi = [];
81
+
82
+ for (var idx = units.length - 1; idx >= 0; idx--) {
83
+ if (!units[idx].ansi) break;
84
+ if (units[idx].text.match(/^(?:\u001B\[|\u009B)[^m]*m$/)) {
85
+ trailingAnsi.unshift(units[idx].text);
86
+ }
87
+ }
88
+
89
+ for (var idx = 0, len = units.length; idx < len; idx++) {
90
+ var unit = units[idx];
91
+ if (unit.ansi) {
92
+ output += unit.text;
93
+ continue;
94
+ }
95
+
96
+ var unitWidth = stringWidth(unit.text);
97
+ if ((outputWidth + unitWidth) > contentWidth) break;
98
+ output += unit.text;
99
+ outputWidth += unitWidth;
100
+ }
101
+
102
+ // A wide grapheme may not fit the final available cell. Pad that cell so the
103
+ // ellipsis still lands at the exact requested width and tables remain aligned.
104
+ if (outputWidth < contentWidth) output += ' '.repeat(contentWidth - outputWidth);
105
+ return output + suffix + trailingAnsi.join('');
106
+ }
107
+
68
108
  // Preserve the compatibility aliases provided by the old CommonJS dependencies.
69
109
  stringWidth.default = stringWidth;
70
110
  widestLine.default = widestLine;
71
111
 
72
112
  module.exports = {
73
113
  stringWidth: stringWidth,
74
- widestLine: widestLine
114
+ widestLine: widestLine,
115
+ truncate: truncate
75
116
  };
package/wrap.js CHANGED
@@ -2,34 +2,15 @@
2
2
 
3
3
  var Util = require('./util');
4
4
  var stringWidth = require('./width').stringWidth;
5
- var ansiPattern = Util.ansiPattern;
6
- var graphemeSegmenter = Util.graphemeSegmenter;
7
5
 
8
6
  function getWrapTokens(text) {
9
7
  // Split text into alternating words and breakable whitespace, while preserving
10
8
  // ANSI sequences as zero-width units in their original positions.
11
- var units = [];
12
- var pattern = new RegExp(ansiPattern.source, 'g');
13
- var offset = 0;
14
- var match = null;
15
-
16
- var addText = function(value) {
17
- for (var item of graphemeSegmenter.segment(value)) {
18
- units.push({
19
- text: item.segment,
20
- width: stringWidth(item.segment),
21
- breakable: !!item.segment.match(/^(?:\s|\u200B)+$/u),
22
- ansi: false
23
- });
24
- }
25
- };
26
-
27
- while ((match = pattern.exec(text))) {
28
- if (match.index > offset) addText( text.substring(offset, match.index) );
29
- units.push({ text: match[0], width: 0, breakable: false, ansi: true });
30
- offset = pattern.lastIndex;
31
- }
32
- if (offset < text.length) addText( text.substring(offset) );
9
+ var units = Util.splitAnsiGraphemes(text);
10
+ units.forEach( function(unit) {
11
+ unit.width = unit.ansi ? 0 : stringWidth(unit.text);
12
+ unit.breakable = !unit.ansi && !!unit.text.match(/^(?:\s|\u200B)+$/u);
13
+ } );
33
14
 
34
15
  var tokens = [];
35
16
  var token = null;
@@ -1,12 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- indent_style = tab
5
- end_of_line = lf
6
- charset = utf-8
7
- trim_trailing_whitespace = true
8
- insert_final_newline = true
9
-
10
- [*.yml]
11
- indent_style = space
12
- indent_size = 2
@@ -1 +0,0 @@
1
- * text=auto eol=lf
@@ -1,3 +0,0 @@
1
- # Security Policy
2
-
3
- To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
@@ -1,22 +0,0 @@
1
- name: CI
2
- on:
3
- - push
4
- - pull_request
5
- jobs:
6
- test:
7
- name: Node.js ${{ matrix.node-version }}
8
- runs-on: ubuntu-latest
9
- strategy:
10
- fail-fast: false
11
- matrix:
12
- node-version:
13
- - 24
14
- - 22
15
- - 20
16
- steps:
17
- - uses: actions/checkout@v4
18
- - uses: actions/setup-node@v4
19
- with:
20
- node-version: ${{ matrix.node-version }}
21
- - run: npm install
22
- - run: npm test
@@ -1,39 +0,0 @@
1
- export type Options = {
2
- /**
3
- 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).
4
-
5
- @default true
6
-
7
- > Ambiguous characters behave like wide or narrow characters depending on the context (language tag, script identification, associated font, source of data, or explicit markup; all can provide the context). __If the context cannot be established reliably, they should be treated as narrow characters by default.__
8
- > - http://www.unicode.org/reports/tr11/
9
- */
10
- readonly ambiguousIsNarrow?: boolean;
11
-
12
- /**
13
- Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted.
14
-
15
- @default false
16
- */
17
- readonly countAnsiEscapeCodes?: boolean;
18
- };
19
-
20
- /**
21
- Get the visual width of a string - the number of columns required to display it.
22
-
23
- Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
24
-
25
- @example
26
- ```
27
- import stringWidth from 'string-width';
28
-
29
- stringWidth('a');
30
- //=> 1
31
-
32
- stringWidth('古');
33
- //=> 2
34
-
35
- stringWidth('\u001B[1m古\u001B[22m');
36
- //=> 2
37
- ```
38
- */
39
- export default function stringWidth(string: string, options?: Options): number;
@@ -1,207 +0,0 @@
1
- import stripAnsi from 'strip-ansi';
2
- import {eastAsianWidth} from 'get-east-asian-width';
3
-
4
- /**
5
- Logic:
6
- - Segment graphemes to match how terminals render clusters.
7
- - Width rules:
8
- 1. Skip non-printing clusters (Default_Ignorable, Control, pure nonspacing/enclosing Mark, lone Surrogates). Tabs are ignored by design.
9
- 2. RGI emoji clusters (\p{RGI_Emoji}) are double-width.
10
- 3. Minimally-qualified/unqualified emoji clusters (ZWJ sequences with 2+ Extended_Pictographic, or keycap sequences) are double-width.
11
- 4. Hangul jamo collapse each standard modern Hangul L+V or L+V+T syllable piece to width 2.
12
- Unmatched repeated leading/vowel/trailing jamo stay additive because that matches how the terminals we target render them.
13
- 5. Otherwise use East Asian Width of the cluster's first visible code point, and add widths for trailing spacing marks and Halfwidth/Fullwidth Forms within the same cluster (e.g., dakuten/handakuten/prolonged sound mark).
14
- */
15
-
16
- const segmenter = new Intl.Segmenter();
17
-
18
- // Whole-cluster zero-width
19
- const zeroWidthClusterRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Nonspacing_Mark}|\p{Enclosing_Mark}|\p{Surrogate})+$/v;
20
-
21
- // Pick the base scalar if the cluster starts with Prepend/Format/Marks
22
- const leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Nonspacing_Mark}\p{Enclosing_Mark}\p{Surrogate}]+/v;
23
- const spacingMarkRegex = /\p{Spacing_Mark}/v;
24
-
25
- // RGI emoji sequences
26
- const rgiEmojiRegex = /^\p{RGI_Emoji}$/v;
27
-
28
- // Detect minimally-qualified/unqualified emoji sequences (missing VS16 but still render as double-width)
29
- const unqualifiedKeycapRegex = /^[\d#*]\u20E3$/;
30
- const extendedPictographicRegex = /\p{Extended_Pictographic}/gu;
31
-
32
- function isDoubleWidthNonRgiEmojiSequence(segment) {
33
- // Real emoji clusters are < 30 chars; guard against pathological input
34
- if (segment.length > 50) {
35
- return false;
36
- }
37
-
38
- if (unqualifiedKeycapRegex.test(segment)) {
39
- return true;
40
- }
41
-
42
- // ZWJ sequences with 2+ Extended_Pictographic
43
- if (segment.includes('\u200D')) {
44
- const pictographics = segment.match(extendedPictographicRegex);
45
- return pictographics !== null && pictographics.length >= 2;
46
- }
47
-
48
- return false;
49
- }
50
-
51
- function baseVisible(segment) {
52
- return segment.replace(leadingNonPrintingRegex, '');
53
- }
54
-
55
- function isZeroWidthCluster(segment) {
56
- return zeroWidthClusterRegex.test(segment);
57
- }
58
-
59
- function isHangulLeadingJamo(codePoint) {
60
- return (codePoint >= 0x11_00 && codePoint <= 0x11_5F)
61
- || (codePoint >= 0xA9_60 && codePoint <= 0xA9_7C);
62
- }
63
-
64
- function isHangulVowelJamo(codePoint) {
65
- return (codePoint >= 0x11_60 && codePoint <= 0x11_A7)
66
- || (codePoint >= 0xD7_B0 && codePoint <= 0xD7_C6);
67
- }
68
-
69
- function isHangulTrailingJamo(codePoint) {
70
- return (codePoint >= 0x11_A8 && codePoint <= 0x11_FF)
71
- || (codePoint >= 0xD7_CB && codePoint <= 0xD7_FB);
72
- }
73
-
74
- function isHangulJamo(codePoint) {
75
- return isHangulLeadingJamo(codePoint)
76
- || isHangulVowelJamo(codePoint)
77
- || isHangulTrailingJamo(codePoint);
78
- }
79
-
80
- function hangulClusterWidth(visibleSegment, eastAsianWidthOptions) {
81
- const codePoints = [];
82
-
83
- for (const character of visibleSegment) {
84
- if (zeroWidthClusterRegex.test(character)) {
85
- continue;
86
- }
87
-
88
- codePoints.push(character.codePointAt(0));
89
- }
90
-
91
- if (codePoints.length === 0) {
92
- return undefined;
93
- }
94
-
95
- let width = 0;
96
-
97
- for (let index = 0; index < codePoints.length; index++) {
98
- const codePoint = codePoints[index];
99
- if (!isHangulJamo(codePoint)) {
100
- if (width === 0) {
101
- return undefined;
102
- }
103
-
104
- // Mixed cluster (e.g., L + precomposed syllable): use EAW for non-jamo remainder
105
- for (let remaining = index; remaining < codePoints.length; remaining++) {
106
- width += eastAsianWidth(codePoints[remaining], eastAsianWidthOptions);
107
- }
108
-
109
- return width;
110
- }
111
-
112
- // Modern Hangul L+V(+T) shapes as one syllable block. Unmatched jamo stay additive:
113
- // U+1100 U+1100 U+1161 => U+1100 + (U+1100 U+1161) => 2 + 2.
114
- if (
115
- isHangulLeadingJamo(codePoint)
116
- && isHangulVowelJamo(codePoints[index + 1])
117
- ) {
118
- width += 2;
119
- index += isHangulTrailingJamo(codePoints[index + 2]) ? 2 : 1;
120
- continue;
121
- }
122
-
123
- width += eastAsianWidth(codePoint, eastAsianWidthOptions);
124
- }
125
-
126
- return width;
127
- }
128
-
129
- function trailingWidth(visibleSegment, eastAsianWidthOptions) {
130
- let extra = 0;
131
- let first = true;
132
-
133
- for (const character of visibleSegment) {
134
- if (first) {
135
- first = false;
136
- continue;
137
- }
138
-
139
- if (
140
- spacingMarkRegex.test(character)
141
- || (character >= '\uFF00' && character <= '\uFFEF')
142
- ) {
143
- extra += eastAsianWidth(character.codePointAt(0), eastAsianWidthOptions);
144
- }
145
- }
146
-
147
- return extra;
148
- }
149
-
150
- export default function stringWidth(input, options = {}) {
151
- if (typeof input !== 'string' || input.length === 0) {
152
- return 0;
153
- }
154
-
155
- const {
156
- ambiguousIsNarrow = true,
157
- countAnsiEscapeCodes = false,
158
- } = options;
159
-
160
- let string = input;
161
-
162
- // Avoid calling stripAnsi when there are no ANSI escape sequences (ESC = 0x1B, CSI = 0x9B)
163
- if (!countAnsiEscapeCodes && (string.includes('\u001B') || string.includes('\u009B'))) {
164
- string = stripAnsi(string);
165
- }
166
-
167
- if (string.length === 0) {
168
- return 0;
169
- }
170
-
171
- // Fast path: printable ASCII (0x20–0x7E) needs no segmenter, regex, or EAW lookup — width equals length.
172
- if (/^[\u0020-\u007E]*$/.test(string)) {
173
- return string.length;
174
- }
175
-
176
- let width = 0;
177
- const eastAsianWidthOptions = {ambiguousAsWide: !ambiguousIsNarrow};
178
-
179
- for (const {segment} of segmenter.segment(string)) {
180
- // Zero-width / non-printing clusters
181
- if (isZeroWidthCluster(segment)) {
182
- continue;
183
- }
184
-
185
- // Emoji width logic
186
- if (rgiEmojiRegex.test(segment) || isDoubleWidthNonRgiEmojiSequence(segment)) {
187
- width += 2;
188
- continue;
189
- }
190
-
191
- const visibleSegment = baseVisible(segment);
192
- const hangulWidth = hangulClusterWidth(visibleSegment, eastAsianWidthOptions);
193
- if (hangulWidth !== undefined) {
194
- width += hangulWidth;
195
- continue;
196
- }
197
-
198
- // Everything else: EAW of the cluster’s first visible scalar
199
- const codePoint = visibleSegment.codePointAt(0);
200
- width += eastAsianWidth(codePoint, eastAsianWidthOptions);
201
-
202
- // Add width for trailing spacing marks and Halfwidth/Fullwidth Forms (e.g., ゙, ゚, ー)
203
- width += trailingWidth(visibleSegment, eastAsianWidthOptions);
204
- }
205
-
206
- return width;
207
- }
@@ -1,7 +0,0 @@
1
- import {expectType} from 'tsd';
2
- import stringWidth from './index.js';
3
-
4
- expectType<number>(stringWidth('古'));
5
- expectType<number>(stringWidth('★', {}));
6
- expectType<number>(stringWidth('★', {ambiguousIsNarrow: false}));
7
- expectType<number>(stringWidth('\u001B[31m\u001B[39m', {countAnsiEscapeCodes: true}));
@@ -1,9 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,65 +0,0 @@
1
- {
2
- "name": "string-width",
3
- "version": "8.2.2",
4
- "description": "Get the visual width of a string - the number of columns required to display it",
5
- "license": "MIT",
6
- "repository": "sindresorhus/string-width",
7
- "funding": "https://github.com/sponsors/sindresorhus",
8
- "author": {
9
- "name": "Sindre Sorhus",
10
- "email": "sindresorhus@gmail.com",
11
- "url": "https://sindresorhus.com"
12
- },
13
- "type": "module",
14
- "exports": {
15
- "types": "./index.d.ts",
16
- "default": "./index.js"
17
- },
18
- "sideEffects": false,
19
- "engines": {
20
- "node": ">=20"
21
- },
22
- "scripts": {
23
- "test": "xo && ava && tsd"
24
- },
25
- "files": [
26
- "index.js",
27
- "index.d.ts"
28
- ],
29
- "keywords": [
30
- "string",
31
- "character",
32
- "unicode",
33
- "width",
34
- "visual",
35
- "column",
36
- "columns",
37
- "fullwidth",
38
- "full-width",
39
- "wcwidth",
40
- "wcswidth",
41
- "full",
42
- "ansi",
43
- "escape",
44
- "codes",
45
- "cli",
46
- "command-line",
47
- "terminal",
48
- "console",
49
- "cjk",
50
- "chinese",
51
- "japanese",
52
- "korean",
53
- "fixed-width",
54
- "east-asian-width"
55
- ],
56
- "dependencies": {
57
- "get-east-asian-width": "^1.5.0",
58
- "strip-ansi": "^7.1.2"
59
- },
60
- "devDependencies": {
61
- "ava": "^6.4.1",
62
- "tsd": "^0.33.0",
63
- "xo": "^1.2.3"
64
- }
65
- }
@@ -1,66 +0,0 @@
1
- # string-width
2
-
3
- > Get the visual width of a string - the number of columns required to display it
4
-
5
- Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and do not affect the width.
6
-
7
- Useful to be able to measure the actual width of command-line output.
8
-
9
- ## Install
10
-
11
- ```sh
12
- npm install string-width
13
- ```
14
-
15
- ## Usage
16
-
17
- ```js
18
- import stringWidth from 'string-width';
19
-
20
- stringWidth('a');
21
- //=> 1
22
-
23
- stringWidth('古');
24
- //=> 2
25
-
26
- stringWidth('\u001B[1m古\u001B[22m');
27
- //=> 2
28
- ```
29
-
30
- ## API
31
-
32
- ### stringWidth(string, options?)
33
-
34
- #### string
35
-
36
- Type: `string`
37
-
38
- The string to be counted.
39
-
40
- #### options
41
-
42
- Type: `object`
43
-
44
- ##### ambiguousIsNarrow
45
-
46
- Type: `boolean`\
47
- Default: `true`
48
-
49
- 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).
50
-
51
- > Ambiguous characters behave like wide or narrow characters depending on the context (language tag, script identification, associated font, source of data, or explicit markup; all can provide the context). **If the context cannot be established reliably, they should be treated as narrow characters by default.**
52
- > - http://www.unicode.org/reports/tr11/
53
-
54
- ##### countAnsiEscapeCodes
55
-
56
- Type: `boolean`\
57
- Default: `false`
58
-
59
- Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted.
60
-
61
- ## Related
62
-
63
- - [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module
64
- - [string-length](https://github.com/sindresorhus/string-length) - Get the real length of a string
65
- - [widest-line](https://github.com/sindresorhus/widest-line) - Get the visual width of the widest line in a string
66
- - [get-east-asian-width](https://github.com/sindresorhus/get-east-asian-width) - Determine the East Asian Width of a Unicode character