diff options
Diffstat (limited to 'node_modules/slice-ansi/index.js')
-rwxr-xr-x | node_modules/slice-ansi/index.js | 102 |
1 files changed, 55 insertions, 47 deletions
diff --git a/node_modules/slice-ansi/index.js b/node_modules/slice-ansi/index.js index c65de4d4a..634ee9c7b 100755 --- a/node_modules/slice-ansi/index.js +++ b/node_modules/slice-ansi/index.js @@ -1,58 +1,63 @@ 'use strict'; +const isFullwidthCodePoint = require('is-fullwidth-code-point'); -var ESCAPES = [ - '\u001b', - '\u009b' +const ESCAPES = [ + '\u001B', + '\u009B' ]; -var END_CODE = 39; +const END_CODE = 39; +const ASTRAL_REGEX = /[\uD800-\uDBFF][\uDC00-\uDFFF]/; -var ESCAPE_CODES = { - 0: 0, - 1: 22, - 2: 22, - 3: 23, - 4: 24, - 7: 27, - 8: 28, - 9: 29, - 30: 39, - 31: 39, - 32: 39, - 33: 39, - 34: 39, - 35: 39, - 36: 39, - 37: 39, - 90: 39, - 40: 49, - 41: 49, - 42: 49, - 43: 49, - 44: 49, - 45: 49, - 46: 49, - 47: 49 -}; +const ESCAPE_CODES = new Map([ + [0, 0], + [1, 22], + [2, 22], + [3, 23], + [4, 24], + [7, 27], + [8, 28], + [9, 29], + [30, 39], + [31, 39], + [32, 39], + [33, 39], + [34, 39], + [35, 39], + [36, 39], + [37, 39], + [90, 39], + [40, 49], + [41, 49], + [42, 49], + [43, 49], + [44, 49], + [45, 49], + [46, 49], + [47, 49] +]); + +const wrapAnsi = code => `${ESCAPES[0]}[${code}m`; + +module.exports = (str, begin, end) => { + const arr = Array.from(str.normalize()); + + end = typeof end === 'number' ? end : arr.length; -function wrapAnsi(code) { - return ESCAPES[0] + '[' + code + 'm'; -} + let insideEscape = false; + let escapeCode; + let visible = 0; + let output = ''; -module.exports = function (str, begin, end) { - end = end || str.length; - var insideEscape = false; - var escapeCode; - var visible = 0; - var output = ''; + for (const item of arr.entries()) { + const i = item[0]; + const x = item[1]; - for (var i = 0; i < str.length; i++) { - var leftEscape = false; - var x = str[i]; + let leftEscape = false; if (ESCAPES.indexOf(x) !== -1) { insideEscape = true; - var code = /[0-9][^m]*/.exec(str.slice(i, i + 4)); + const code = /\d[^m]*/.exec(str.slice(i, i + 4)); escapeCode = code === END_CODE ? null : code; } else if (insideEscape && x === 'm') { insideEscape = false; @@ -63,13 +68,17 @@ module.exports = function (str, begin, end) { ++visible; } + if (!ASTRAL_REGEX.test(x) && isFullwidthCodePoint(x.codePointAt())) { + ++visible; + } + if (visible > begin && visible <= end) { output += x; - } else if (visible === begin && escapeCode !== undefined && escapeCode !== END_CODE) { + } else if (visible === begin && !insideEscape && escapeCode !== undefined && escapeCode !== END_CODE) { output += wrapAnsi(escapeCode); } else if (visible >= end) { if (escapeCode !== undefined) { - output += wrapAnsi(ESCAPE_CODES[escapeCode] || END_CODE); + output += wrapAnsi(ESCAPE_CODES.get(parseInt(escapeCode, 10)) || END_CODE); } break; } @@ -77,4 +86,3 @@ module.exports = function (str, begin, end) { return output; }; - |