index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use strict';
  2. const escapeStringRegexp = require('escape-string-regexp');
  3. const ansiStyles = require('ansi-styles');
  4. const supportsColor = require('supports-color');
  5. const template = require('./templates.js');
  6. const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
  7. // `supportsColor.level` → `ansiStyles.color[name]` mapping
  8. const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
  9. // `color-convert` models to exclude from the Chalk API due to conflicts and such
  10. const skipModels = new Set(['gray']);
  11. const styles = Object.create(null);
  12. function applyOptions(obj, options) {
  13. options = options || {};
  14. // Detect level if not set manually
  15. const scLevel = supportsColor ? supportsColor.level : 0;
  16. obj.level = options.level === undefined ? scLevel : options.level;
  17. obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
  18. }
  19. function Chalk(options) {
  20. // We check for this.template here since calling `chalk.constructor()`
  21. // by itself will have a `this` of a previously constructed chalk object
  22. if (!this || !(this instanceof Chalk) || this.template) {
  23. const chalk = {};
  24. applyOptions(chalk, options);
  25. chalk.template = function () {
  26. const args = [].slice.call(arguments);
  27. return chalkTag.apply(null, [chalk.template].concat(args));
  28. };
  29. Object.setPrototypeOf(chalk, Chalk.prototype);
  30. Object.setPrototypeOf(chalk.template, chalk);
  31. chalk.template.constructor = Chalk;
  32. return chalk.template;
  33. }
  34. applyOptions(this, options);
  35. }
  36. // Use bright blue on Windows as the normal blue color is illegible
  37. if (isSimpleWindowsTerm) {
  38. ansiStyles.blue.open = '\u001B[94m';
  39. }
  40. for (const key of Object.keys(ansiStyles)) {
  41. ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
  42. styles[key] = {
  43. get() {
  44. const codes = ansiStyles[key];
  45. return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key);
  46. }
  47. };
  48. }
  49. ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
  50. for (const model of Object.keys(ansiStyles.color.ansi)) {
  51. if (skipModels.has(model)) {
  52. continue;
  53. }
  54. styles[model] = {
  55. get() {
  56. const level = this.level;
  57. return function () {
  58. const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
  59. const codes = {
  60. open,
  61. close: ansiStyles.color.close,
  62. closeRe: ansiStyles.color.closeRe
  63. };
  64. return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
  65. };
  66. }
  67. };
  68. }
  69. ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
  70. for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
  71. if (skipModels.has(model)) {
  72. continue;
  73. }
  74. const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
  75. styles[bgModel] = {
  76. get() {
  77. const level = this.level;
  78. return function () {
  79. const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
  80. const codes = {
  81. open,
  82. close: ansiStyles.bgColor.close,
  83. closeRe: ansiStyles.bgColor.closeRe
  84. };
  85. return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
  86. };
  87. }
  88. };
  89. }
  90. const proto = Object.defineProperties(() => {}, styles);
  91. function build(_styles, key) {
  92. const builder = function () {
  93. return applyStyle.apply(builder, arguments);
  94. };
  95. builder._styles = _styles;
  96. const self = this;
  97. Object.defineProperty(builder, 'level', {
  98. enumerable: true,
  99. get() {
  100. return self.level;
  101. },
  102. set(level) {
  103. self.level = level;
  104. }
  105. });
  106. Object.defineProperty(builder, 'enabled', {
  107. enumerable: true,
  108. get() {
  109. return self.enabled;
  110. },
  111. set(enabled) {
  112. self.enabled = enabled;
  113. }
  114. });
  115. // See below for fix regarding invisible grey/dim combination on Windows
  116. builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
  117. // `__proto__` is used because we must return a function, but there is
  118. // no way to create a function with a different prototype
  119. builder.__proto__ = proto; // eslint-disable-line no-proto
  120. return builder;
  121. }
  122. function applyStyle() {
  123. // Support varags, but simply cast to string in case there's only one arg
  124. const args = arguments;
  125. const argsLen = args.length;
  126. let str = String(arguments[0]);
  127. if (argsLen === 0) {
  128. return '';
  129. }
  130. if (argsLen > 1) {
  131. // Don't slice `arguments`, it prevents V8 optimizations
  132. for (let a = 1; a < argsLen; a++) {
  133. str += ' ' + args[a];
  134. }
  135. }
  136. if (!this.enabled || this.level <= 0 || !str) {
  137. return str;
  138. }
  139. // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
  140. // see https://github.com/chalk/chalk/issues/58
  141. // If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
  142. const originalDim = ansiStyles.dim.open;
  143. if (isSimpleWindowsTerm && this.hasGrey) {
  144. ansiStyles.dim.open = '';
  145. }
  146. for (const code of this._styles.slice().reverse()) {
  147. // Replace any instances already present with a re-opening code
  148. // otherwise only the part of the string until said closing code
  149. // will be colored, and the rest will simply be 'plain'.
  150. str = code.open + str.replace(code.closeRe, code.open) + code.close;
  151. // Close the styling before a linebreak and reopen
  152. // after next line to fix a bleed issue on macOS
  153. // https://github.com/chalk/chalk/pull/92
  154. str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`);
  155. }
  156. // Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
  157. ansiStyles.dim.open = originalDim;
  158. return str;
  159. }
  160. function chalkTag(chalk, strings) {
  161. if (!Array.isArray(strings)) {
  162. // If chalk() was called by itself or with a string,
  163. // return the string itself as a string.
  164. return [].slice.call(arguments, 1).join(' ');
  165. }
  166. const args = [].slice.call(arguments, 2);
  167. const parts = [strings.raw[0]];
  168. for (let i = 1; i < strings.length; i++) {
  169. parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
  170. parts.push(String(strings.raw[i]));
  171. }
  172. return template(chalk, parts.join(''));
  173. }
  174. Object.defineProperties(Chalk.prototype, styles);
  175. module.exports = Chalk(); // eslint-disable-line new-cap
  176. module.exports.supportsColor = supportsColor;