index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. const colorConvert = require('color-convert');
  3. const wrapAnsi16 = (fn, offset) => function () {
  4. const code = fn.apply(colorConvert, arguments);
  5. return `\u001B[${code + offset}m`;
  6. };
  7. const wrapAnsi256 = (fn, offset) => function () {
  8. const code = fn.apply(colorConvert, arguments);
  9. return `\u001B[${38 + offset};5;${code}m`;
  10. };
  11. const wrapAnsi16m = (fn, offset) => function () {
  12. const rgb = fn.apply(colorConvert, arguments);
  13. return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
  14. };
  15. function assembleStyles() {
  16. const codes = new Map();
  17. const styles = {
  18. modifier: {
  19. reset: [0, 0],
  20. // 21 isn't widely supported and 22 does the same thing
  21. bold: [1, 22],
  22. dim: [2, 22],
  23. italic: [3, 23],
  24. underline: [4, 24],
  25. inverse: [7, 27],
  26. hidden: [8, 28],
  27. strikethrough: [9, 29]
  28. },
  29. color: {
  30. black: [30, 39],
  31. red: [31, 39],
  32. green: [32, 39],
  33. yellow: [33, 39],
  34. blue: [34, 39],
  35. magenta: [35, 39],
  36. cyan: [36, 39],
  37. white: [37, 39],
  38. gray: [90, 39],
  39. // Bright color
  40. redBright: [91, 39],
  41. greenBright: [92, 39],
  42. yellowBright: [93, 39],
  43. blueBright: [94, 39],
  44. magentaBright: [95, 39],
  45. cyanBright: [96, 39],
  46. whiteBright: [97, 39]
  47. },
  48. bgColor: {
  49. bgBlack: [40, 49],
  50. bgRed: [41, 49],
  51. bgGreen: [42, 49],
  52. bgYellow: [43, 49],
  53. bgBlue: [44, 49],
  54. bgMagenta: [45, 49],
  55. bgCyan: [46, 49],
  56. bgWhite: [47, 49],
  57. // Bright color
  58. bgBlackBright: [100, 49],
  59. bgRedBright: [101, 49],
  60. bgGreenBright: [102, 49],
  61. bgYellowBright: [103, 49],
  62. bgBlueBright: [104, 49],
  63. bgMagentaBright: [105, 49],
  64. bgCyanBright: [106, 49],
  65. bgWhiteBright: [107, 49]
  66. }
  67. };
  68. // Fix humans
  69. styles.color.grey = styles.color.gray;
  70. for (const groupName of Object.keys(styles)) {
  71. const group = styles[groupName];
  72. for (const styleName of Object.keys(group)) {
  73. const style = group[styleName];
  74. styles[styleName] = {
  75. open: `\u001B[${style[0]}m`,
  76. close: `\u001B[${style[1]}m`
  77. };
  78. group[styleName] = styles[styleName];
  79. codes.set(style[0], style[1]);
  80. }
  81. Object.defineProperty(styles, groupName, {
  82. value: group,
  83. enumerable: false
  84. });
  85. Object.defineProperty(styles, 'codes', {
  86. value: codes,
  87. enumerable: false
  88. });
  89. }
  90. const rgb2rgb = (r, g, b) => [r, g, b];
  91. styles.color.close = '\u001B[39m';
  92. styles.bgColor.close = '\u001B[49m';
  93. styles.color.ansi = {};
  94. styles.color.ansi256 = {};
  95. styles.color.ansi16m = {
  96. rgb: wrapAnsi16m(rgb2rgb, 0)
  97. };
  98. styles.bgColor.ansi = {};
  99. styles.bgColor.ansi256 = {};
  100. styles.bgColor.ansi16m = {
  101. rgb: wrapAnsi16m(rgb2rgb, 10)
  102. };
  103. for (const key of Object.keys(colorConvert)) {
  104. if (typeof colorConvert[key] !== 'object') {
  105. continue;
  106. }
  107. const suite = colorConvert[key];
  108. if ('ansi16' in suite) {
  109. styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
  110. styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
  111. }
  112. if ('ansi256' in suite) {
  113. styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0);
  114. styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10);
  115. }
  116. if ('rgb' in suite) {
  117. styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0);
  118. styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10);
  119. }
  120. }
  121. return styles;
  122. }
  123. // Make the export immutable
  124. Object.defineProperty(module, 'exports', {
  125. enumerable: true,
  126. get: assembleStyles
  127. });