index.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. let enabled = true;
  2. // Support both browser and node environments
  3. const globalVar = typeof self !== 'undefined'
  4. ? self
  5. : typeof window !== 'undefined'
  6. ? window
  7. : typeof global !== 'undefined'
  8. ? global
  9. : {};
  10. /**
  11. * Detect how much colors the current terminal supports
  12. */
  13. let supportLevel = 0 /* none */;
  14. if (globalVar.process && globalVar.process.env && globalVar.process.stdout) {
  15. const { FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, COLORTERM } = globalVar.process.env;
  16. if (NODE_DISABLE_COLORS || NO_COLOR || FORCE_COLOR === '0') {
  17. enabled = false;
  18. }
  19. else if (FORCE_COLOR === '1' ||
  20. FORCE_COLOR === '2' ||
  21. FORCE_COLOR === '3') {
  22. enabled = true;
  23. }
  24. else if (TERM === 'dumb') {
  25. enabled = false;
  26. }
  27. else if ('CI' in globalVar.process.env &&
  28. [
  29. 'TRAVIS',
  30. 'CIRCLECI',
  31. 'APPVEYOR',
  32. 'GITLAB_CI',
  33. 'GITHUB_ACTIONS',
  34. 'BUILDKITE',
  35. 'DRONE',
  36. ].some(vendor => vendor in globalVar.process.env)) {
  37. enabled = true;
  38. }
  39. else {
  40. enabled = process.stdout.isTTY;
  41. }
  42. if (enabled) {
  43. // Windows supports 24bit True Colors since Windows 10 revision #14931,
  44. // see https://devblogs.microsoft.com/commandline/24-bit-color-in-the-windows-console/
  45. if (process.platform === 'win32') {
  46. supportLevel = 3 /* trueColor */;
  47. }
  48. else {
  49. if (COLORTERM && (COLORTERM === 'truecolor' || COLORTERM === '24bit')) {
  50. supportLevel = 3 /* trueColor */;
  51. }
  52. else if (TERM && (TERM.endsWith('-256color') || TERM.endsWith('256'))) {
  53. supportLevel = 2 /* ansi256 */;
  54. }
  55. else {
  56. supportLevel = 1 /* ansi */;
  57. }
  58. }
  59. }
  60. }
  61. export let options = {
  62. enabled,
  63. supportLevel,
  64. };
  65. function kolorist(start, end, level = 1 /* ansi */) {
  66. const open = `\x1b[${start}m`;
  67. const close = `\x1b[${end}m`;
  68. const regex = new RegExp(`\\x1b\\[${end}m`, 'g');
  69. return (str) => {
  70. return options.enabled && options.supportLevel >= level
  71. ? open + ('' + str).replace(regex, open) + close
  72. : '' + str;
  73. };
  74. }
  75. // Lower colors into 256 color space
  76. // Taken from https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
  77. // which is MIT licensed and copyright by Heather Arthur and Josh Junon
  78. function rgbToAnsi256(r, g, b) {
  79. // We use the extended greyscale palette here, with the exception of
  80. // black and white. normal palette only has 4 greyscale shades.
  81. if (r >> 4 === g >> 4 && g >> 4 === b >> 4) {
  82. if (r < 8) {
  83. return 16;
  84. }
  85. if (r > 248) {
  86. return 231;
  87. }
  88. return Math.round(((r - 8) / 247) * 24) + 232;
  89. }
  90. const ansi = 16 +
  91. 36 * Math.round((r / 255) * 5) +
  92. 6 * Math.round((g / 255) * 5) +
  93. Math.round((b / 255) * 5);
  94. return ansi;
  95. }
  96. export function stripColors(str) {
  97. return ('' + str)
  98. .replace(/\x1b\[[0-9;]+m/g, '')
  99. .replace(/\x1b\]8;;.*?\x07(.*?)\x1b\]8;;\x07/g, (_, group) => group);
  100. }
  101. // modifiers
  102. export const reset = kolorist(0, 0);
  103. export const bold = kolorist(1, 22);
  104. export const dim = kolorist(2, 22);
  105. export const italic = kolorist(3, 23);
  106. export const underline = kolorist(4, 24);
  107. export const inverse = kolorist(7, 27);
  108. export const hidden = kolorist(8, 28);
  109. export const strikethrough = kolorist(9, 29);
  110. // colors
  111. export const black = kolorist(30, 39);
  112. export const red = kolorist(31, 39);
  113. export const green = kolorist(32, 39);
  114. export const yellow = kolorist(33, 39);
  115. export const blue = kolorist(34, 39);
  116. export const magenta = kolorist(35, 39);
  117. export const cyan = kolorist(36, 39);
  118. export const white = kolorist(97, 39);
  119. export const gray = kolorist(90, 39);
  120. export const lightGray = kolorist(37, 39);
  121. export const lightRed = kolorist(91, 39);
  122. export const lightGreen = kolorist(92, 39);
  123. export const lightYellow = kolorist(93, 39);
  124. export const lightBlue = kolorist(94, 39);
  125. export const lightMagenta = kolorist(95, 39);
  126. export const lightCyan = kolorist(96, 39);
  127. // background colors
  128. export const bgBlack = kolorist(40, 49);
  129. export const bgRed = kolorist(41, 49);
  130. export const bgGreen = kolorist(42, 49);
  131. export const bgYellow = kolorist(43, 49);
  132. export const bgBlue = kolorist(44, 49);
  133. export const bgMagenta = kolorist(45, 49);
  134. export const bgCyan = kolorist(46, 49);
  135. export const bgWhite = kolorist(107, 49);
  136. export const bgGray = kolorist(100, 49);
  137. export const bgLightRed = kolorist(101, 49);
  138. export const bgLightGreen = kolorist(102, 49);
  139. export const bgLightYellow = kolorist(103, 49);
  140. export const bgLightBlue = kolorist(104, 49);
  141. export const bgLightMagenta = kolorist(105, 49);
  142. export const bgLightCyan = kolorist(106, 49);
  143. export const bgLightGray = kolorist(47, 49);
  144. // 256 support
  145. export const ansi256 = (n) => kolorist('38;5;' + n, 0, 2 /* ansi256 */);
  146. export const ansi256Bg = (n) => kolorist('48;5;' + n, 0, 2 /* ansi256 */);
  147. // TrueColor 24bit support
  148. export const trueColor = (r, g, b) => {
  149. return options.supportLevel === 2 /* ansi256 */
  150. ? ansi256(rgbToAnsi256(r, g, b))
  151. : kolorist(`38;2;${r};${g};${b}`, 0, 3 /* trueColor */);
  152. };
  153. export const trueColorBg = (r, g, b) => {
  154. return options.supportLevel === 2 /* ansi256 */
  155. ? ansi256Bg(rgbToAnsi256(r, g, b))
  156. : kolorist(`48;2;${r};${g};${b}`, 0, 3 /* trueColor */);
  157. };
  158. // Links
  159. const OSC = '\u001B]';
  160. const BEL = '\u0007';
  161. const SEP = ';';
  162. export function link(text, url) {
  163. return options.enabled
  164. ? OSC + '8' + SEP + SEP + url + BEL + text + OSC + '8' + SEP + SEP + BEL
  165. : `${text} (\u200B${url}\u200B)`;
  166. }
  167. //# sourceMappingURL=index.js.map