|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -// Regexes used for ansi escape code splitting |
| 3 | +// Regex used for ansi escape code splitting |
4 | 4 | // eslint-disable-next-line no-control-regex
|
5 |
| -const metaKeyCodeReAnywhere = /(?:\x1b)([a-zA-Z0-9])/; |
6 |
| -const functionKeyCodeReAnywhere = new RegExp('(?:\x1b+)(O|N|\\[|\\[\\[)(?:' + [ |
7 |
| - '(\\d+)(?:;(\\d+))?([~^$])', |
8 |
| - '(?:M([@ #!a`])(.)(.))', // mouse |
9 |
| - '(?:1;)?(\\d+)?([a-zA-Z])' |
10 |
| -].join('|') + ')'); |
| 5 | +// Adopted from https://github.com./chalk/ansi-regex/blob/master/index.js |
| 6 | +// License: MIT, authors: @sindresorhus, Qix-, and arjunmehta |
| 7 | +// Matches all ansi escape code sequences in a string |
| 8 | +const ansi = |
| 9 | + /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; |
11 | 10 |
|
12 | 11 |
|
13 | 12 | module.exports = {
|
14 | 13 | emitKeys,
|
15 |
| - getStringWidth, |
16 |
| - isFullWidthCodePoint, |
17 | 14 | stripVTControlCharacters
|
18 | 15 | };
|
19 | 16 |
|
| 17 | +if (process.binding('config').hasIntl) { |
| 18 | + const icu = process.binding('icu'); |
| 19 | + module.exports.getStringWidth = function getStringWidth(str, options) { |
| 20 | + options = options || {}; |
| 21 | + if (!Number.isInteger(str)) |
| 22 | + str = stripVTControlCharacters(String(str)); |
| 23 | + return icu.getStringWidth(str, |
| 24 | + Boolean(options.ambiguousAsFullWidth), |
| 25 | + Boolean(options.expandEmojiSequence)); |
| 26 | + }; |
| 27 | + module.exports.isFullWidthCodePoint = |
| 28 | + function isFullWidthCodePoint(code, options) { |
| 29 | + if (typeof code !== 'number') |
| 30 | + return false; |
| 31 | + return icu.getStringWidth(code, options) === 2; |
| 32 | + }; |
| 33 | +} else { |
| 34 | + /** |
| 35 | + * Returns the number of columns required to display the given string. |
| 36 | + */ |
| 37 | + module.exports.getStringWidth = function getStringWidth(str) { |
| 38 | + if (Number.isInteger(str)) |
| 39 | + return module.exports.isFullWidthCodePoint(str) ? 2 : 1; |
20 | 40 |
|
21 |
| -/** |
22 |
| - * Returns the number of columns required to display the given string. |
23 |
| - */ |
24 |
| -function getStringWidth(str) { |
25 |
| - let width = 0; |
| 41 | + let width = 0; |
26 | 42 |
|
27 |
| - str = stripVTControlCharacters(str); |
| 43 | + str = stripVTControlCharacters(String(str)); |
28 | 44 |
|
29 |
| - for (var i = 0; i < str.length; i++) { |
30 |
| - const code = str.codePointAt(i); |
| 45 | + for (var i = 0; i < str.length; i++) { |
| 46 | + const code = str.codePointAt(i); |
31 | 47 |
|
32 |
| - if (code >= 0x10000) { // surrogates |
33 |
| - i++; |
34 |
| - } |
| 48 | + if (code >= 0x10000) { // surrogates |
| 49 | + i++; |
| 50 | + } |
35 | 51 |
|
36 |
| - if (isFullWidthCodePoint(code)) { |
37 |
| - width += 2; |
38 |
| - } else { |
39 |
| - width++; |
| 52 | + if (module.exports.isFullWidthCodePoint(code)) { |
| 53 | + width += 2; |
| 54 | + } else { |
| 55 | + width++; |
| 56 | + } |
40 | 57 | }
|
41 |
| - } |
42 |
| - |
43 |
| - return width; |
44 |
| -} |
45 | 58 |
|
| 59 | + return width; |
| 60 | + }; |
46 | 61 |
|
47 |
| -/** |
48 |
| - * Returns true if the character represented by a given |
49 |
| - * Unicode code point is full-width. Otherwise returns false. |
50 |
| - */ |
51 |
| -function isFullWidthCodePoint(code) { |
52 |
| - if (isNaN(code)) { |
53 |
| - return false; |
54 |
| - } |
| 62 | + /** |
| 63 | + * Returns true if the character represented by a given |
| 64 | + * Unicode code point is full-width. Otherwise returns false. |
| 65 | + */ |
| 66 | + module.exports.isFullWidthCodePoint = function isFullWidthCodePoint(code) { |
| 67 | + if (!Number.isInteger(code)) { |
| 68 | + return false; |
| 69 | + } |
55 | 70 |
|
56 |
| - // Code points are derived from: |
57 |
| - // http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt |
58 |
| - if (code >= 0x1100 && ( |
59 |
| - code <= 0x115f || // Hangul Jamo |
60 |
| - 0x2329 === code || // LEFT-POINTING ANGLE BRACKET |
61 |
| - 0x232a === code || // RIGHT-POINTING ANGLE BRACKET |
62 |
| - // CJK Radicals Supplement .. Enclosed CJK Letters and Months |
63 |
| - (0x2e80 <= code && code <= 0x3247 && code !== 0x303f) || |
64 |
| - // Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A |
65 |
| - 0x3250 <= code && code <= 0x4dbf || |
66 |
| - // CJK Unified Ideographs .. Yi Radicals |
67 |
| - 0x4e00 <= code && code <= 0xa4c6 || |
68 |
| - // Hangul Jamo Extended-A |
69 |
| - 0xa960 <= code && code <= 0xa97c || |
70 |
| - // Hangul Syllables |
71 |
| - 0xac00 <= code && code <= 0xd7a3 || |
72 |
| - // CJK Compatibility Ideographs |
73 |
| - 0xf900 <= code && code <= 0xfaff || |
74 |
| - // Vertical Forms |
75 |
| - 0xfe10 <= code && code <= 0xfe19 || |
76 |
| - // CJK Compatibility Forms .. Small Form Variants |
77 |
| - 0xfe30 <= code && code <= 0xfe6b || |
78 |
| - // Halfwidth and Fullwidth Forms |
79 |
| - 0xff01 <= code && code <= 0xff60 || |
80 |
| - 0xffe0 <= code && code <= 0xffe6 || |
81 |
| - // Kana Supplement |
82 |
| - 0x1b000 <= code && code <= 0x1b001 || |
83 |
| - // Enclosed Ideographic Supplement |
84 |
| - 0x1f200 <= code && code <= 0x1f251 || |
85 |
| - // CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane |
86 |
| - 0x20000 <= code && code <= 0x3fffd)) { |
87 |
| - return true; |
88 |
| - } |
| 71 | + // Code points are derived from: |
| 72 | + // http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt |
| 73 | + if (code >= 0x1100 && ( |
| 74 | + code <= 0x115f || // Hangul Jamo |
| 75 | + 0x2329 === code || // LEFT-POINTING ANGLE BRACKET |
| 76 | + 0x232a === code || // RIGHT-POINTING ANGLE BRACKET |
| 77 | + // CJK Radicals Supplement .. Enclosed CJK Letters and Months |
| 78 | + (0x2e80 <= code && code <= 0x3247 && code !== 0x303f) || |
| 79 | + // Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A |
| 80 | + 0x3250 <= code && code <= 0x4dbf || |
| 81 | + // CJK Unified Ideographs .. Yi Radicals |
| 82 | + 0x4e00 <= code && code <= 0xa4c6 || |
| 83 | + // Hangul Jamo Extended-A |
| 84 | + 0xa960 <= code && code <= 0xa97c || |
| 85 | + // Hangul Syllables |
| 86 | + 0xac00 <= code && code <= 0xd7a3 || |
| 87 | + // CJK Compatibility Ideographs |
| 88 | + 0xf900 <= code && code <= 0xfaff || |
| 89 | + // Vertical Forms |
| 90 | + 0xfe10 <= code && code <= 0xfe19 || |
| 91 | + // CJK Compatibility Forms .. Small Form Variants |
| 92 | + 0xfe30 <= code && code <= 0xfe6b || |
| 93 | + // Halfwidth and Fullwidth Forms |
| 94 | + 0xff01 <= code && code <= 0xff60 || |
| 95 | + 0xffe0 <= code && code <= 0xffe6 || |
| 96 | + // Kana Supplement |
| 97 | + 0x1b000 <= code && code <= 0x1b001 || |
| 98 | + // Enclosed Ideographic Supplement |
| 99 | + 0x1f200 <= code && code <= 0x1f251 || |
| 100 | + // CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane |
| 101 | + 0x20000 <= code && code <= 0x3fffd)) { |
| 102 | + return true; |
| 103 | + } |
89 | 104 |
|
90 |
| - return false; |
| 105 | + return false; |
| 106 | + }; |
91 | 107 | }
|
92 | 108 |
|
93 |
| - |
94 | 109 | /**
|
95 | 110 | * Tries to remove all VT control characters. Use to estimate displayed
|
96 | 111 | * string width. May be buggy due to not running a real state machine
|
97 | 112 | */
|
98 | 113 | function stripVTControlCharacters(str) {
|
99 |
| - str = str.replace(new RegExp(functionKeyCodeReAnywhere.source, 'g'), ''); |
100 |
| - return str.replace(new RegExp(metaKeyCodeReAnywhere.source, 'g'), ''); |
| 114 | + return str.replace(ansi, ''); |
101 | 115 | }
|
102 | 116 |
|
103 | 117 |
|
|
0 commit comments