index.mjs 2.7 KB

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