index.js 5.8 KB

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