You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. var escapeStringRegexp = require('escape-string-regexp');
  3. var ansiStyles = require('ansi-styles');
  4. var stripAnsi = require('strip-ansi');
  5. var hasAnsi = require('has-ansi');
  6. var supportsColor = require('supports-color');
  7. var defineProps = Object.defineProperties;
  8. var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
  9. function Chalk(options) {
  10. // detect mode if not set manually
  11. this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
  12. }
  13. // use bright blue on Windows as the normal blue color is illegible
  14. if (isSimpleWindowsTerm) {
  15. ansiStyles.blue.open = '\u001b[94m';
  16. }
  17. var styles = (function () {
  18. var ret = {};
  19. Object.keys(ansiStyles).forEach(function (key) {
  20. ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
  21. ret[key] = {
  22. get: function () {
  23. return build.call(this, this._styles.concat(key));
  24. }
  25. };
  26. });
  27. return ret;
  28. })();
  29. var proto = defineProps(function chalk() {}, styles);
  30. function build(_styles) {
  31. var builder = function () {
  32. return applyStyle.apply(builder, arguments);
  33. };
  34. builder._styles = _styles;
  35. builder.enabled = this.enabled;
  36. // __proto__ is used because we must return a function, but there is
  37. // no way to create a function with a different prototype.
  38. /* eslint-disable no-proto */
  39. builder.__proto__ = proto;
  40. return builder;
  41. }
  42. function applyStyle() {
  43. // support varags, but simply cast to string in case there's only one arg
  44. var args = arguments;
  45. var argsLen = args.length;
  46. var str = argsLen !== 0 && String(arguments[0]);
  47. if (argsLen > 1) {
  48. // don't slice `arguments`, it prevents v8 optimizations
  49. for (var a = 1; a < argsLen; a++) {
  50. str += ' ' + args[a];
  51. }
  52. }
  53. if (!this.enabled || !str) {
  54. return str;
  55. }
  56. var nestedStyles = this._styles;
  57. var i = nestedStyles.length;
  58. // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
  59. // see https://github.com/chalk/chalk/issues/58
  60. // If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
  61. var originalDim = ansiStyles.dim.open;
  62. if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
  63. ansiStyles.dim.open = '';
  64. }
  65. while (i--) {
  66. var code = ansiStyles[nestedStyles[i]];
  67. // Replace any instances already present with a re-opening code
  68. // otherwise only the part of the string until said closing code
  69. // will be colored, and the rest will simply be 'plain'.
  70. str = code.open + str.replace(code.closeRe, code.open) + code.close;
  71. }
  72. // Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
  73. ansiStyles.dim.open = originalDim;
  74. return str;
  75. }
  76. function init() {
  77. var ret = {};
  78. Object.keys(styles).forEach(function (name) {
  79. ret[name] = {
  80. get: function () {
  81. return build.call(this, [name]);
  82. }
  83. };
  84. });
  85. return ret;
  86. }
  87. defineProps(Chalk.prototype, init());
  88. module.exports = new Chalk();
  89. module.exports.styles = ansiStyles;
  90. module.exports.hasColor = hasAnsi;
  91. module.exports.stripColor = stripAnsi;
  92. module.exports.supportsColor = supportsColor;