Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. 'use strict';
  2. const escapeStringRegexp = require('escape-string-regexp');
  3. const ansiStyles = require('ansi-styles');
  4. const stdoutColor = require('supports-color').stdout;
  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 = stdoutColor ? stdoutColor.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], this._empty, key);
  46. }
  47. };
  48. }
  49. styles.visible = {
  50. get() {
  51. return build.call(this, this._styles || [], true, 'visible');
  52. }
  53. };
  54. ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
  55. for (const model of Object.keys(ansiStyles.color.ansi)) {
  56. if (skipModels.has(model)) {
  57. continue;
  58. }
  59. styles[model] = {
  60. get() {
  61. const level = this.level;
  62. return function () {
  63. const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
  64. const codes = {
  65. open,
  66. close: ansiStyles.color.close,
  67. closeRe: ansiStyles.color.closeRe
  68. };
  69. return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
  70. };
  71. }
  72. };
  73. }
  74. ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
  75. for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
  76. if (skipModels.has(model)) {
  77. continue;
  78. }
  79. const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
  80. styles[bgModel] = {
  81. get() {
  82. const level = this.level;
  83. return function () {
  84. const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
  85. const codes = {
  86. open,
  87. close: ansiStyles.bgColor.close,
  88. closeRe: ansiStyles.bgColor.closeRe
  89. };
  90. return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
  91. };
  92. }
  93. };
  94. }
  95. const proto = Object.defineProperties(() => {}, styles);
  96. function build(_styles, _empty, key) {
  97. const builder = function () {
  98. return applyStyle.apply(builder, arguments);
  99. };
  100. builder._styles = _styles;
  101. builder._empty = _empty;
  102. const self = this;
  103. Object.defineProperty(builder, 'level', {
  104. enumerable: true,
  105. get() {
  106. return self.level;
  107. },
  108. set(level) {
  109. self.level = level;
  110. }
  111. });
  112. Object.defineProperty(builder, 'enabled', {
  113. enumerable: true,
  114. get() {
  115. return self.enabled;
  116. },
  117. set(enabled) {
  118. self.enabled = enabled;
  119. }
  120. });
  121. // See below for fix regarding invisible grey/dim combination on Windows
  122. builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
  123. // `__proto__` is used because we must return a function, but there is
  124. // no way to create a function with a different prototype
  125. builder.__proto__ = proto; // eslint-disable-line no-proto
  126. return builder;
  127. }
  128. function applyStyle() {
  129. // Support varags, but simply cast to string in case there's only one arg
  130. const args = arguments;
  131. const argsLen = args.length;
  132. let str = String(arguments[0]);
  133. if (argsLen === 0) {
  134. return '';
  135. }
  136. if (argsLen > 1) {
  137. // Don't slice `arguments`, it prevents V8 optimizations
  138. for (let a = 1; a < argsLen; a++) {
  139. str += ' ' + args[a];
  140. }
  141. }
  142. if (!this.enabled || this.level <= 0 || !str) {
  143. return this._empty ? '' : str;
  144. }
  145. // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
  146. // see https://github.com/chalk/chalk/issues/58
  147. // If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
  148. const originalDim = ansiStyles.dim.open;
  149. if (isSimpleWindowsTerm && this.hasGrey) {
  150. ansiStyles.dim.open = '';
  151. }
  152. for (const code of this._styles.slice().reverse()) {
  153. // Replace any instances already present with a re-opening code
  154. // otherwise only the part of the string until said closing code
  155. // will be colored, and the rest will simply be 'plain'.
  156. str = code.open + str.replace(code.closeRe, code.open) + code.close;
  157. // Close the styling before a linebreak and reopen
  158. // after next line to fix a bleed issue on macOS
  159. // https://github.com/chalk/chalk/pull/92
  160. str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`);
  161. }
  162. // Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
  163. ansiStyles.dim.open = originalDim;
  164. return str;
  165. }
  166. function chalkTag(chalk, strings) {
  167. if (!Array.isArray(strings)) {
  168. // If chalk() was called by itself or with a string,
  169. // return the string itself as a string.
  170. return [].slice.call(arguments, 1).join(' ');
  171. }
  172. const args = [].slice.call(arguments, 2);
  173. const parts = [strings.raw[0]];
  174. for (let i = 1; i < strings.length; i++) {
  175. parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
  176. parts.push(String(strings.raw[i]));
  177. }
  178. return template(chalk, parts.join(''));
  179. }
  180. Object.defineProperties(Chalk.prototype, styles);
  181. module.exports = Chalk(); // eslint-disable-line new-cap
  182. module.exports.supportsColor = stdoutColor;
  183. module.exports.default = module.exports; // For TypeScript