index.js 4.0 KB

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