colors.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. The MIT License (MIT)
  3. Original Library
  4. - Copyright (c) Marak Squires
  5. Additional functionality
  6. - Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. */
  23. var colors = {};
  24. module['exports'] = colors;
  25. colors.themes = {};
  26. var util = require('util');
  27. var ansiStyles = colors.styles = require('./styles');
  28. var defineProps = Object.defineProperties;
  29. var newLineRegex = new RegExp(/[\r\n]+/g);
  30. colors.supportsColor = require('./system/supports-colors').supportsColor;
  31. if (typeof colors.enabled === 'undefined') {
  32. colors.enabled = colors.supportsColor() !== false;
  33. }
  34. colors.enable = function() {
  35. colors.enabled = true;
  36. };
  37. colors.disable = function() {
  38. colors.enabled = false;
  39. };
  40. colors.stripColors = colors.strip = function(str) {
  41. return ('' + str).replace(/\x1B\[\d+m/g, '');
  42. };
  43. // eslint-disable-next-line no-unused-vars
  44. var stylize = colors.stylize = function stylize(str, style) {
  45. if (!colors.enabled) {
  46. return str+'';
  47. }
  48. return ansiStyles[style].open + str + ansiStyles[style].close;
  49. };
  50. var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
  51. var escapeStringRegexp = function(str) {
  52. if (typeof str !== 'string') {
  53. throw new TypeError('Expected a string');
  54. }
  55. return str.replace(matchOperatorsRe, '\\$&');
  56. };
  57. function build(_styles) {
  58. var builder = function builder() {
  59. return applyStyle.apply(builder, arguments);
  60. };
  61. builder._styles = _styles;
  62. // __proto__ is used because we must return a function, but there is
  63. // no way to create a function with a different prototype.
  64. builder.__proto__ = proto;
  65. return builder;
  66. }
  67. var styles = (function() {
  68. var ret = {};
  69. ansiStyles.grey = ansiStyles.gray;
  70. Object.keys(ansiStyles).forEach(function(key) {
  71. ansiStyles[key].closeRe =
  72. new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
  73. ret[key] = {
  74. get: function() {
  75. return build(this._styles.concat(key));
  76. },
  77. };
  78. });
  79. return ret;
  80. })();
  81. var proto = defineProps(function colors() {}, styles);
  82. function applyStyle() {
  83. var args = Array.prototype.slice.call(arguments);
  84. var str = args.map(function(arg) {
  85. if (arg !== undefined && arg.constructor === String) {
  86. return arg;
  87. } else {
  88. return util.inspect(arg);
  89. }
  90. }).join(' ');
  91. if (!colors.enabled || !str) {
  92. return str;
  93. }
  94. var newLinesPresent = str.indexOf('\n') != -1;
  95. var nestedStyles = this._styles;
  96. var i = nestedStyles.length;
  97. while (i--) {
  98. var code = ansiStyles[nestedStyles[i]];
  99. str = code.open + str.replace(code.closeRe, code.open) + code.close;
  100. if (newLinesPresent) {
  101. str = str.replace(newLineRegex, code.close + '\n' + code.open);
  102. }
  103. }
  104. return str;
  105. }
  106. colors.setTheme = function(theme) {
  107. if (typeof theme === 'string') {
  108. console.log('colors.setTheme now only accepts an object, not a string. ' +
  109. 'If you are trying to set a theme from a file, it is now your (the ' +
  110. 'caller\'s) responsibility to require the file. The old syntax ' +
  111. 'looked like colors.setTheme(__dirname + ' +
  112. '\'/../themes/generic-logging.js\'); The new syntax looks like '+
  113. 'colors.setTheme(require(__dirname + ' +
  114. '\'/../themes/generic-logging.js\'));');
  115. return;
  116. }
  117. for (var style in theme) {
  118. (function(style) {
  119. colors[style] = function(str) {
  120. if (typeof theme[style] === 'object') {
  121. var out = str;
  122. for (var i in theme[style]) {
  123. out = colors[theme[style][i]](out);
  124. }
  125. return out;
  126. }
  127. return colors[theme[style]](str);
  128. };
  129. })(style);
  130. }
  131. };
  132. function init() {
  133. var ret = {};
  134. Object.keys(styles).forEach(function(name) {
  135. ret[name] = {
  136. get: function() {
  137. return build([name]);
  138. },
  139. };
  140. });
  141. return ret;
  142. }
  143. var sequencer = function sequencer(map, str) {
  144. var exploded = str.split('');
  145. exploded = exploded.map(map);
  146. return exploded.join('');
  147. };
  148. // custom formatter methods
  149. colors.trap = require('./custom/trap');
  150. colors.zalgo = require('./custom/zalgo');
  151. // maps
  152. colors.maps = {};
  153. colors.maps.america = require('./maps/america');
  154. colors.maps.zebra = require('./maps/zebra');
  155. colors.maps.rainbow = require('./maps/rainbow');
  156. colors.maps.random = require('./maps/random');
  157. for (var map in colors.maps) {
  158. (function(map) {
  159. colors[map] = function(str) {
  160. return sequencer(colors.maps[map], str);
  161. };
  162. })(map);
  163. }
  164. defineProps(colors, init());