index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. const { FORCE_COLOR, NODE_DISABLE_COLORS, TERM } = process.env;
  3. const $ = {
  4. enabled: !NODE_DISABLE_COLORS && TERM !== 'dumb' && FORCE_COLOR !== '0',
  5. // modifiers
  6. reset: init(0, 0),
  7. bold: init(1, 22),
  8. dim: init(2, 22),
  9. italic: init(3, 23),
  10. underline: init(4, 24),
  11. inverse: init(7, 27),
  12. hidden: init(8, 28),
  13. strikethrough: init(9, 29),
  14. // colors
  15. black: init(30, 39),
  16. red: init(31, 39),
  17. green: init(32, 39),
  18. yellow: init(33, 39),
  19. blue: init(34, 39),
  20. magenta: init(35, 39),
  21. cyan: init(36, 39),
  22. white: init(37, 39),
  23. gray: init(90, 39),
  24. grey: init(90, 39),
  25. // background colors
  26. bgBlack: init(40, 49),
  27. bgRed: init(41, 49),
  28. bgGreen: init(42, 49),
  29. bgYellow: init(43, 49),
  30. bgBlue: init(44, 49),
  31. bgMagenta: init(45, 49),
  32. bgCyan: init(46, 49),
  33. bgWhite: init(47, 49)
  34. };
  35. function run(arr, str) {
  36. let i=0, tmp, beg='', end='';
  37. for (; i < arr.length; i++) {
  38. tmp = arr[i];
  39. beg += tmp.open;
  40. end += tmp.close;
  41. if (str.includes(tmp.close)) {
  42. str = str.replace(tmp.rgx, tmp.close + tmp.open);
  43. }
  44. }
  45. return beg + str + end;
  46. }
  47. function chain(has, keys) {
  48. let ctx = { has, keys };
  49. ctx.reset = $.reset.bind(ctx);
  50. ctx.bold = $.bold.bind(ctx);
  51. ctx.dim = $.dim.bind(ctx);
  52. ctx.italic = $.italic.bind(ctx);
  53. ctx.underline = $.underline.bind(ctx);
  54. ctx.inverse = $.inverse.bind(ctx);
  55. ctx.hidden = $.hidden.bind(ctx);
  56. ctx.strikethrough = $.strikethrough.bind(ctx);
  57. ctx.black = $.black.bind(ctx);
  58. ctx.red = $.red.bind(ctx);
  59. ctx.green = $.green.bind(ctx);
  60. ctx.yellow = $.yellow.bind(ctx);
  61. ctx.blue = $.blue.bind(ctx);
  62. ctx.magenta = $.magenta.bind(ctx);
  63. ctx.cyan = $.cyan.bind(ctx);
  64. ctx.white = $.white.bind(ctx);
  65. ctx.gray = $.gray.bind(ctx);
  66. ctx.grey = $.grey.bind(ctx);
  67. ctx.bgBlack = $.bgBlack.bind(ctx);
  68. ctx.bgRed = $.bgRed.bind(ctx);
  69. ctx.bgGreen = $.bgGreen.bind(ctx);
  70. ctx.bgYellow = $.bgYellow.bind(ctx);
  71. ctx.bgBlue = $.bgBlue.bind(ctx);
  72. ctx.bgMagenta = $.bgMagenta.bind(ctx);
  73. ctx.bgCyan = $.bgCyan.bind(ctx);
  74. ctx.bgWhite = $.bgWhite.bind(ctx);
  75. return ctx;
  76. }
  77. function init(open, close) {
  78. let blk = {
  79. open: `\x1b[${open}m`,
  80. close: `\x1b[${close}m`,
  81. rgx: new RegExp(`\\x1b\\[${close}m`, 'g')
  82. };
  83. return function (txt) {
  84. if (this !== void 0 && this.has !== void 0) {
  85. this.has.includes(open) || (this.has.push(open),this.keys.push(blk));
  86. return txt === void 0 ? this : $.enabled ? run(this.keys, txt+'') : txt+'';
  87. }
  88. return txt === void 0 ? chain([open], [blk]) : $.enabled ? run([blk], txt+'') : txt+'';
  89. };
  90. }
  91. module.exports = $;