index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
  3. const identity = val => val;
  4. /* eslint-disable no-control-regex */
  5. // this is a modified version of https://github.com/chalk/ansi-regex (MIT License)
  6. const ANSI_REGEX = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g;
  7. const create = () => {
  8. const colors = { enabled: true, visible: true, styles: {}, keys: {} };
  9. if ('FORCE_COLOR' in process.env) {
  10. colors.enabled = process.env.FORCE_COLOR !== '0';
  11. }
  12. const ansi = style => {
  13. let open = style.open = `\u001b[${style.codes[0]}m`;
  14. let close = style.close = `\u001b[${style.codes[1]}m`;
  15. let regex = style.regex = new RegExp(`\\u001b\\[${style.codes[1]}m`, 'g');
  16. style.wrap = (input, newline) => {
  17. if (input.includes(close)) input = input.replace(regex, close + open);
  18. let output = open + input + close;
  19. // see https://github.com/chalk/chalk/pull/92, thanks to the
  20. // chalk contributors for this fix. However, we've confirmed that
  21. // this issue is also present in Windows terminals
  22. return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output;
  23. };
  24. return style;
  25. };
  26. const wrap = (style, input, newline) => {
  27. return typeof style === 'function' ? style(input) : style.wrap(input, newline);
  28. };
  29. const style = (input, stack) => {
  30. if (input === '' || input == null) return '';
  31. if (colors.enabled === false) return input;
  32. if (colors.visible === false) return '';
  33. let str = '' + input;
  34. let nl = str.includes('\n');
  35. let n = stack.length;
  36. if (n > 0 && stack.includes('unstyle')) {
  37. stack = [...new Set(['unstyle', ...stack])].reverse();
  38. }
  39. while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl);
  40. return str;
  41. };
  42. const define = (name, codes, type) => {
  43. colors.styles[name] = ansi({ name, codes });
  44. let keys = colors.keys[type] || (colors.keys[type] = []);
  45. keys.push(name);
  46. Reflect.defineProperty(colors, name, {
  47. configurable: true,
  48. enumerable: true,
  49. set(value) {
  50. colors.alias(name, value);
  51. },
  52. get() {
  53. let color = input => style(input, color.stack);
  54. Reflect.setPrototypeOf(color, colors);
  55. color.stack = this.stack ? this.stack.concat(name) : [name];
  56. return color;
  57. }
  58. });
  59. };
  60. define('reset', [0, 0], 'modifier');
  61. define('bold', [1, 22], 'modifier');
  62. define('dim', [2, 22], 'modifier');
  63. define('italic', [3, 23], 'modifier');
  64. define('underline', [4, 24], 'modifier');
  65. define('inverse', [7, 27], 'modifier');
  66. define('hidden', [8, 28], 'modifier');
  67. define('strikethrough', [9, 29], 'modifier');
  68. define('black', [30, 39], 'color');
  69. define('red', [31, 39], 'color');
  70. define('green', [32, 39], 'color');
  71. define('yellow', [33, 39], 'color');
  72. define('blue', [34, 39], 'color');
  73. define('magenta', [35, 39], 'color');
  74. define('cyan', [36, 39], 'color');
  75. define('white', [37, 39], 'color');
  76. define('gray', [90, 39], 'color');
  77. define('grey', [90, 39], 'color');
  78. define('bgBlack', [40, 49], 'bg');
  79. define('bgRed', [41, 49], 'bg');
  80. define('bgGreen', [42, 49], 'bg');
  81. define('bgYellow', [43, 49], 'bg');
  82. define('bgBlue', [44, 49], 'bg');
  83. define('bgMagenta', [45, 49], 'bg');
  84. define('bgCyan', [46, 49], 'bg');
  85. define('bgWhite', [47, 49], 'bg');
  86. define('blackBright', [90, 39], 'bright');
  87. define('redBright', [91, 39], 'bright');
  88. define('greenBright', [92, 39], 'bright');
  89. define('yellowBright', [93, 39], 'bright');
  90. define('blueBright', [94, 39], 'bright');
  91. define('magentaBright', [95, 39], 'bright');
  92. define('cyanBright', [96, 39], 'bright');
  93. define('whiteBright', [97, 39], 'bright');
  94. define('bgBlackBright', [100, 49], 'bgBright');
  95. define('bgRedBright', [101, 49], 'bgBright');
  96. define('bgGreenBright', [102, 49], 'bgBright');
  97. define('bgYellowBright', [103, 49], 'bgBright');
  98. define('bgBlueBright', [104, 49], 'bgBright');
  99. define('bgMagentaBright', [105, 49], 'bgBright');
  100. define('bgCyanBright', [106, 49], 'bgBright');
  101. define('bgWhiteBright', [107, 49], 'bgBright');
  102. colors.ansiRegex = ANSI_REGEX;
  103. colors.hasColor = colors.hasAnsi = str => {
  104. colors.ansiRegex.lastIndex = 0;
  105. return typeof str === 'string' && str !== '' && colors.ansiRegex.test(str);
  106. };
  107. colors.alias = (name, color) => {
  108. let fn = typeof color === 'string' ? colors[color] : color;
  109. if (typeof fn !== 'function') {
  110. throw new TypeError('Expected alias to be the name of an existing color (string) or a function');
  111. }
  112. if (!fn.stack) {
  113. Reflect.defineProperty(fn, 'name', { value: name });
  114. colors.styles[name] = fn;
  115. fn.stack = [name];
  116. }
  117. Reflect.defineProperty(colors, name, {
  118. configurable: true,
  119. enumerable: true,
  120. set(value) {
  121. colors.alias(name, value);
  122. },
  123. get() {
  124. let color = input => style(input, color.stack);
  125. Reflect.setPrototypeOf(color, colors);
  126. color.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack;
  127. return color;
  128. }
  129. });
  130. };
  131. colors.theme = custom => {
  132. if (!isObject(custom)) throw new TypeError('Expected theme to be an object');
  133. for (let name of Object.keys(custom)) {
  134. colors.alias(name, custom[name]);
  135. }
  136. return colors;
  137. };
  138. colors.alias('unstyle', str => {
  139. if (typeof str === 'string' && str !== '') {
  140. colors.ansiRegex.lastIndex = 0;
  141. return str.replace(colors.ansiRegex, '');
  142. }
  143. return '';
  144. });
  145. colors.alias('noop', str => str);
  146. colors.none = colors.clear = colors.noop;
  147. colors.stripColor = colors.unstyle;
  148. colors.symbols = require('./symbols');
  149. colors.define = define;
  150. return colors;
  151. };
  152. module.exports = create();
  153. module.exports.create = create;