1
0

index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 ansi2ansi = n => n;
  91. const rgb2rgb = (r, g, b) => [r, g, b];
  92. styles.color.close = '\u001B[39m';
  93. styles.bgColor.close = '\u001B[49m';
  94. styles.color.ansi = {
  95. ansi: wrapAnsi16(ansi2ansi, 0)
  96. };
  97. styles.color.ansi256 = {
  98. ansi256: wrapAnsi256(ansi2ansi, 0)
  99. };
  100. styles.color.ansi16m = {
  101. rgb: wrapAnsi16m(rgb2rgb, 0)
  102. };
  103. styles.bgColor.ansi = {
  104. ansi: wrapAnsi16(ansi2ansi, 10)
  105. };
  106. styles.bgColor.ansi256 = {
  107. ansi256: wrapAnsi256(ansi2ansi, 10)
  108. };
  109. styles.bgColor.ansi16m = {
  110. rgb: wrapAnsi16m(rgb2rgb, 10)
  111. };
  112. for (let key of Object.keys(colorConvert)) {
  113. if (typeof colorConvert[key] !== 'object') {
  114. continue;
  115. }
  116. const suite = colorConvert[key];
  117. if (key === 'ansi16') {
  118. key = 'ansi';
  119. }
  120. if ('ansi16' in suite) {
  121. styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
  122. styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
  123. }
  124. if ('ansi256' in suite) {
  125. styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0);
  126. styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10);
  127. }
  128. if ('rgb' in suite) {
  129. styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0);
  130. styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10);
  131. }
  132. }
  133. return styles;
  134. }
  135. // Make the export immutable
  136. Object.defineProperty(module, 'exports', {
  137. enumerable: true,
  138. get: assembleStyles
  139. });