index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1677462587637, function(require, module, exports) {
  8. const stringWidth = require('string-width');
  9. const stripAnsi = require('strip-ansi');
  10. const ansiStyles = require('ansi-styles');
  11. const ESCAPES = new Set([
  12. '\u001B',
  13. '\u009B'
  14. ]);
  15. const END_CODE = 39;
  16. const ANSI_ESCAPE_BELL = '\u0007';
  17. const ANSI_CSI = '[';
  18. const ANSI_OSC = ']';
  19. const ANSI_SGR_TERMINATOR = 'm';
  20. const ANSI_ESCAPE_LINK = `${ANSI_OSC}8;;`;
  21. const wrapAnsi = code => `${ESCAPES.values().next().value}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
  22. const wrapAnsiHyperlink = uri => `${ESCAPES.values().next().value}${ANSI_ESCAPE_LINK}${uri}${ANSI_ESCAPE_BELL}`;
  23. // Calculate the length of words split on ' ', ignoring
  24. // the extra characters added by ansi escape codes
  25. const wordLengths = string => string.split(' ').map(character => stringWidth(character));
  26. // Wrap a long word across multiple rows
  27. // Ansi escape codes do not count towards length
  28. const wrapWord = (rows, word, columns) => {
  29. const characters = [...word];
  30. let isInsideEscape = false;
  31. let isInsideLinkEscape = false;
  32. let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
  33. for (const [index, character] of characters.entries()) {
  34. const characterLength = stringWidth(character);
  35. if (visible + characterLength <= columns) {
  36. rows[rows.length - 1] += character;
  37. } else {
  38. rows.push(character);
  39. visible = 0;
  40. }
  41. if (ESCAPES.has(character)) {
  42. isInsideEscape = true;
  43. isInsideLinkEscape = characters.slice(index + 1).join('').startsWith(ANSI_ESCAPE_LINK);
  44. }
  45. if (isInsideEscape) {
  46. if (isInsideLinkEscape) {
  47. if (character === ANSI_ESCAPE_BELL) {
  48. isInsideEscape = false;
  49. isInsideLinkEscape = false;
  50. }
  51. } else if (character === ANSI_SGR_TERMINATOR) {
  52. isInsideEscape = false;
  53. }
  54. continue;
  55. }
  56. visible += characterLength;
  57. if (visible === columns && index < characters.length - 1) {
  58. rows.push('');
  59. visible = 0;
  60. }
  61. }
  62. // It's possible that the last row we copy over is only
  63. // ansi escape characters, handle this edge-case
  64. if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
  65. rows[rows.length - 2] += rows.pop();
  66. }
  67. };
  68. // Trims spaces from a string ignoring invisible sequences
  69. const stringVisibleTrimSpacesRight = string => {
  70. const words = string.split(' ');
  71. let last = words.length;
  72. while (last > 0) {
  73. if (stringWidth(words[last - 1]) > 0) {
  74. break;
  75. }
  76. last--;
  77. }
  78. if (last === words.length) {
  79. return string;
  80. }
  81. return words.slice(0, last).join(' ') + words.slice(last).join('');
  82. };
  83. // The wrap-ansi module can be invoked in either 'hard' or 'soft' wrap mode
  84. //
  85. // 'hard' will never allow a string to take up more than columns characters
  86. //
  87. // 'soft' allows long words to expand past the column length
  88. const exec = (string, columns, options = {}) => {
  89. if (options.trim !== false && string.trim() === '') {
  90. return '';
  91. }
  92. let returnValue = '';
  93. let escapeCode;
  94. let escapeUrl;
  95. const lengths = wordLengths(string);
  96. let rows = [''];
  97. for (const [index, word] of string.split(' ').entries()) {
  98. if (options.trim !== false) {
  99. rows[rows.length - 1] = rows[rows.length - 1].trimStart();
  100. }
  101. let rowLength = stringWidth(rows[rows.length - 1]);
  102. if (index !== 0) {
  103. if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) {
  104. // If we start with a new word but the current row length equals the length of the columns, add a new row
  105. rows.push('');
  106. rowLength = 0;
  107. }
  108. if (rowLength > 0 || options.trim === false) {
  109. rows[rows.length - 1] += ' ';
  110. rowLength++;
  111. }
  112. }
  113. // In 'hard' wrap mode, the length of a line is never allowed to extend past 'columns'
  114. if (options.hard && lengths[index] > columns) {
  115. const remainingColumns = (columns - rowLength);
  116. const breaksStartingThisLine = 1 + Math.floor((lengths[index] - remainingColumns - 1) / columns);
  117. const breaksStartingNextLine = Math.floor((lengths[index] - 1) / columns);
  118. if (breaksStartingNextLine < breaksStartingThisLine) {
  119. rows.push('');
  120. }
  121. wrapWord(rows, word, columns);
  122. continue;
  123. }
  124. if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) {
  125. if (options.wordWrap === false && rowLength < columns) {
  126. wrapWord(rows, word, columns);
  127. continue;
  128. }
  129. rows.push('');
  130. }
  131. if (rowLength + lengths[index] > columns && options.wordWrap === false) {
  132. wrapWord(rows, word, columns);
  133. continue;
  134. }
  135. rows[rows.length - 1] += word;
  136. }
  137. if (options.trim !== false) {
  138. rows = rows.map(stringVisibleTrimSpacesRight);
  139. }
  140. const pre = [...rows.join('\n')];
  141. for (const [index, character] of pre.entries()) {
  142. returnValue += character;
  143. if (ESCAPES.has(character)) {
  144. const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`).exec(pre.slice(index).join('')) || {groups: {}};
  145. if (groups.code !== undefined) {
  146. const code = Number.parseFloat(groups.code);
  147. escapeCode = code === END_CODE ? undefined : code;
  148. } else if (groups.uri !== undefined) {
  149. escapeUrl = groups.uri.length === 0 ? undefined : groups.uri;
  150. }
  151. }
  152. const code = ansiStyles.codes.get(Number(escapeCode));
  153. if (pre[index + 1] === '\n') {
  154. if (escapeUrl) {
  155. returnValue += wrapAnsiHyperlink('');
  156. }
  157. if (escapeCode && code) {
  158. returnValue += wrapAnsi(code);
  159. }
  160. } else if (character === '\n') {
  161. if (escapeCode && code) {
  162. returnValue += wrapAnsi(escapeCode);
  163. }
  164. if (escapeUrl) {
  165. returnValue += wrapAnsiHyperlink(escapeUrl);
  166. }
  167. }
  168. }
  169. return returnValue;
  170. };
  171. // For each newline, invoke the method separately
  172. module.exports = (string, columns, options) => {
  173. return String(string)
  174. .normalize()
  175. .replace(/\r\n/g, '\n')
  176. .split('\n')
  177. .map(line => exec(line, columns, options))
  178. .join('\n');
  179. };
  180. }, function(modId) {var map = {}; return __REQUIRE__(map[modId], modId); })
  181. return __REQUIRE__(1677462587637);
  182. })()
  183. //miniprogram-npm-outsideDeps=["string-width","strip-ansi","ansi-styles"]
  184. //# sourceMappingURL=index.js.map