index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const ANSI_BACKGROUND_OFFSET = 10;
  2. const wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`;
  3. const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
  4. const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
  5. const styles = {
  6. modifier: {
  7. reset: [0, 0],
  8. // 21 isn't widely supported and 22 does the same thing
  9. bold: [1, 22],
  10. dim: [2, 22],
  11. italic: [3, 23],
  12. underline: [4, 24],
  13. overline: [53, 55],
  14. inverse: [7, 27],
  15. hidden: [8, 28],
  16. strikethrough: [9, 29],
  17. },
  18. color: {
  19. black: [30, 39],
  20. red: [31, 39],
  21. green: [32, 39],
  22. yellow: [33, 39],
  23. blue: [34, 39],
  24. magenta: [35, 39],
  25. cyan: [36, 39],
  26. white: [37, 39],
  27. // Bright color
  28. blackBright: [90, 39],
  29. gray: [90, 39], // Alias of `blackBright`
  30. grey: [90, 39], // Alias of `blackBright`
  31. redBright: [91, 39],
  32. greenBright: [92, 39],
  33. yellowBright: [93, 39],
  34. blueBright: [94, 39],
  35. magentaBright: [95, 39],
  36. cyanBright: [96, 39],
  37. whiteBright: [97, 39],
  38. },
  39. bgColor: {
  40. bgBlack: [40, 49],
  41. bgRed: [41, 49],
  42. bgGreen: [42, 49],
  43. bgYellow: [43, 49],
  44. bgBlue: [44, 49],
  45. bgMagenta: [45, 49],
  46. bgCyan: [46, 49],
  47. bgWhite: [47, 49],
  48. // Bright color
  49. bgBlackBright: [100, 49],
  50. bgGray: [100, 49], // Alias of `bgBlackBright`
  51. bgGrey: [100, 49], // Alias of `bgBlackBright`
  52. bgRedBright: [101, 49],
  53. bgGreenBright: [102, 49],
  54. bgYellowBright: [103, 49],
  55. bgBlueBright: [104, 49],
  56. bgMagentaBright: [105, 49],
  57. bgCyanBright: [106, 49],
  58. bgWhiteBright: [107, 49],
  59. },
  60. };
  61. export const modifierNames = Object.keys(styles.modifier);
  62. export const foregroundColorNames = Object.keys(styles.color);
  63. export const backgroundColorNames = Object.keys(styles.bgColor);
  64. export const colorNames = [...foregroundColorNames, ...backgroundColorNames];
  65. function assembleStyles() {
  66. const codes = new Map();
  67. for (const [groupName, group] of Object.entries(styles)) {
  68. for (const [styleName, style] of Object.entries(group)) {
  69. styles[styleName] = {
  70. open: `\u001B[${style[0]}m`,
  71. close: `\u001B[${style[1]}m`,
  72. };
  73. group[styleName] = styles[styleName];
  74. codes.set(style[0], style[1]);
  75. }
  76. Object.defineProperty(styles, groupName, {
  77. value: group,
  78. enumerable: false,
  79. });
  80. }
  81. Object.defineProperty(styles, 'codes', {
  82. value: codes,
  83. enumerable: false,
  84. });
  85. styles.color.close = '\u001B[39m';
  86. styles.bgColor.close = '\u001B[49m';
  87. styles.color.ansi = wrapAnsi16();
  88. styles.color.ansi256 = wrapAnsi256();
  89. styles.color.ansi16m = wrapAnsi16m();
  90. styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
  91. styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
  92. styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
  93. // From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
  94. Object.defineProperties(styles, {
  95. rgbToAnsi256: {
  96. value: (red, green, blue) => {
  97. // We use the extended greyscale palette here, with the exception of
  98. // black and white. normal palette only has 4 greyscale shades.
  99. if (red === green && green === blue) {
  100. if (red < 8) {
  101. return 16;
  102. }
  103. if (red > 248) {
  104. return 231;
  105. }
  106. return Math.round(((red - 8) / 247) * 24) + 232;
  107. }
  108. return 16
  109. + (36 * Math.round(red / 255 * 5))
  110. + (6 * Math.round(green / 255 * 5))
  111. + Math.round(blue / 255 * 5);
  112. },
  113. enumerable: false,
  114. },
  115. hexToRgb: {
  116. value: hex => {
  117. const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
  118. if (!matches) {
  119. return [0, 0, 0];
  120. }
  121. let [colorString] = matches;
  122. if (colorString.length === 3) {
  123. colorString = [...colorString].map(character => character + character).join('');
  124. }
  125. const integer = Number.parseInt(colorString, 16);
  126. return [
  127. /* eslint-disable no-bitwise */
  128. (integer >> 16) & 0xFF,
  129. (integer >> 8) & 0xFF,
  130. integer & 0xFF,
  131. /* eslint-enable no-bitwise */
  132. ];
  133. },
  134. enumerable: false,
  135. },
  136. hexToAnsi256: {
  137. value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
  138. enumerable: false,
  139. },
  140. ansi256ToAnsi: {
  141. value: code => {
  142. if (code < 8) {
  143. return 30 + code;
  144. }
  145. if (code < 16) {
  146. return 90 + (code - 8);
  147. }
  148. let red;
  149. let green;
  150. let blue;
  151. if (code >= 232) {
  152. red = (((code - 232) * 10) + 8) / 255;
  153. green = red;
  154. blue = red;
  155. } else {
  156. code -= 16;
  157. const remainder = code % 36;
  158. red = Math.floor(code / 36) / 5;
  159. green = Math.floor(remainder / 6) / 5;
  160. blue = (remainder % 6) / 5;
  161. }
  162. const value = Math.max(red, green, blue) * 2;
  163. if (value === 0) {
  164. return 30;
  165. }
  166. // eslint-disable-next-line no-bitwise
  167. let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
  168. if (value === 2) {
  169. result += 60;
  170. }
  171. return result;
  172. },
  173. enumerable: false,
  174. },
  175. rgbToAnsi: {
  176. value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
  177. enumerable: false,
  178. },
  179. hexToAnsi: {
  180. value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
  181. enumerable: false,
  182. },
  183. });
  184. return styles;
  185. }
  186. const ansiStyles = assembleStyles();
  187. export default ansiStyles;