colors.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 ansiStyles = colors.styles = require('./styles');
  27. var defineProps = Object.defineProperties;
  28. colors.supportsColor = require('./system/supports-colors');
  29. if (typeof colors.enabled === "undefined") {
  30. colors.enabled = colors.supportsColor;
  31. }
  32. colors.stripColors = colors.strip = function(str){
  33. return ("" + str).replace(/\x1B\[\d+m/g, '');
  34. };
  35. var stylize = colors.stylize = function stylize (str, style) {
  36. if (!colors.enabled) {
  37. return str+'';
  38. }
  39. return ansiStyles[style].open + str + ansiStyles[style].close;
  40. }
  41. var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
  42. var escapeStringRegexp = function (str) {
  43. if (typeof str !== 'string') {
  44. throw new TypeError('Expected a string');
  45. }
  46. return str.replace(matchOperatorsRe, '\\$&');
  47. }
  48. function build(_styles) {
  49. var builder = function builder() {
  50. return applyStyle.apply(builder, arguments);
  51. };
  52. builder._styles = _styles;
  53. // __proto__ is used because we must return a function, but there is
  54. // no way to create a function with a different prototype.
  55. builder.__proto__ = proto;
  56. return builder;
  57. }
  58. var styles = (function () {
  59. var ret = {};
  60. ansiStyles.grey = ansiStyles.gray;
  61. Object.keys(ansiStyles).forEach(function (key) {
  62. ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
  63. ret[key] = {
  64. get: function () {
  65. return build(this._styles.concat(key));
  66. }
  67. };
  68. });
  69. return ret;
  70. })();
  71. var proto = defineProps(function colors() {}, styles);
  72. function applyStyle() {
  73. var args = arguments;
  74. var argsLen = args.length;
  75. var str = argsLen !== 0 && String(arguments[0]);
  76. if (argsLen > 1) {
  77. for (var a = 1; a < argsLen; a++) {
  78. str += ' ' + args[a];
  79. }
  80. }
  81. if (!colors.enabled || !str) {
  82. return str;
  83. }
  84. var nestedStyles = this._styles;
  85. var i = nestedStyles.length;
  86. while (i--) {
  87. var code = ansiStyles[nestedStyles[i]];
  88. str = code.open + str.replace(code.closeRe, code.open) + code.close;
  89. }
  90. return str;
  91. }
  92. function applyTheme (theme) {
  93. for (var style in theme) {
  94. (function(style){
  95. colors[style] = function(str){
  96. if (typeof theme[style] === 'object'){
  97. var out = str;
  98. for (var i in theme[style]){
  99. out = colors[theme[style][i]](out);
  100. }
  101. return out;
  102. }
  103. return colors[theme[style]](str);
  104. };
  105. })(style)
  106. }
  107. }
  108. colors.setTheme = function (theme) {
  109. if (typeof theme === 'string') {
  110. try {
  111. colors.themes[theme] = require(theme);
  112. applyTheme(colors.themes[theme]);
  113. return colors.themes[theme];
  114. } catch (err) {
  115. console.log(err);
  116. return err;
  117. }
  118. } else {
  119. applyTheme(theme);
  120. }
  121. };
  122. function init() {
  123. var ret = {};
  124. Object.keys(styles).forEach(function (name) {
  125. ret[name] = {
  126. get: function () {
  127. return build([name]);
  128. }
  129. };
  130. });
  131. return ret;
  132. }
  133. var sequencer = function sequencer (map, str) {
  134. var exploded = str.split(""), i = 0;
  135. exploded = exploded.map(map);
  136. return exploded.join("");
  137. };
  138. // custom formatter methods
  139. colors.trap = require('./custom/trap');
  140. colors.zalgo = require('./custom/zalgo');
  141. // maps
  142. colors.maps = {};
  143. colors.maps.america = require('./maps/america');
  144. colors.maps.zebra = require('./maps/zebra');
  145. colors.maps.rainbow = require('./maps/rainbow');
  146. colors.maps.random = require('./maps/random')
  147. for (var map in colors.maps) {
  148. (function(map){
  149. colors[map] = function (str) {
  150. return sequencer(colors.maps[map], str);
  151. }
  152. })(map)
  153. }
  154. defineProps(colors, init());