index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const tty = require('node:tty');
  2. // eslint-disable-next-line no-warning-comments
  3. // TODO: Use a better method when it's added to Node.js (https://github.com/nodejs/node/pull/40240)
  4. // Lots of optionals here to support Deno.
  5. const hasColors = tty?.WriteStream?.prototype?.hasColors?.() ?? false;
  6. const format = (open, close) => {
  7. if (!hasColors) {
  8. return input => input;
  9. }
  10. const openCode = `\u001B[${open}m`;
  11. const closeCode = `\u001B[${close}m`;
  12. return input => {
  13. const string = input + ''; // eslint-disable-line no-implicit-coercion -- This is faster.
  14. let index = string.indexOf(closeCode);
  15. if (index === -1) {
  16. // Note: Intentionally not using string interpolation for performance reasons.
  17. return openCode + string + closeCode;
  18. }
  19. // Handle nested colors.
  20. // We could have done this, but it's too slow (as of Node.js 22).
  21. // return openCode + string.replaceAll(closeCode, openCode) + closeCode;
  22. let result = openCode;
  23. let lastIndex = 0;
  24. while (index !== -1) {
  25. result += string.slice(lastIndex, index) + openCode;
  26. lastIndex = index + closeCode.length;
  27. index = string.indexOf(closeCode, lastIndex);
  28. }
  29. result += string.slice(lastIndex) + closeCode;
  30. return result;
  31. };
  32. };
  33. const colors = {};
  34. colors.reset = format(0, 0);
  35. colors.bold = format(1, 22);
  36. colors.dim = format(2, 22);
  37. colors.italic = format(3, 23);
  38. colors.underline = format(4, 24);
  39. colors.overline = format(53, 55);
  40. colors.inverse = format(7, 27);
  41. colors.hidden = format(8, 28);
  42. colors.strikethrough = format(9, 29);
  43. colors.black = format(30, 39);
  44. colors.red = format(31, 39);
  45. colors.green = format(32, 39);
  46. colors.yellow = format(33, 39);
  47. colors.blue = format(34, 39);
  48. colors.magenta = format(35, 39);
  49. colors.cyan = format(36, 39);
  50. colors.white = format(37, 39);
  51. colors.gray = format(90, 39);
  52. colors.bgBlack = format(40, 49);
  53. colors.bgRed = format(41, 49);
  54. colors.bgGreen = format(42, 49);
  55. colors.bgYellow = format(43, 49);
  56. colors.bgBlue = format(44, 49);
  57. colors.bgMagenta = format(45, 49);
  58. colors.bgCyan = format(46, 49);
  59. colors.bgWhite = format(47, 49);
  60. colors.bgGray = format(100, 49);
  61. colors.redBright = format(91, 39);
  62. colors.greenBright = format(92, 39);
  63. colors.yellowBright = format(93, 39);
  64. colors.blueBright = format(94, 39);
  65. colors.magentaBright = format(95, 39);
  66. colors.cyanBright = format(96, 39);
  67. colors.whiteBright = format(97, 39);
  68. colors.bgRedBright = format(101, 49);
  69. colors.bgGreenBright = format(102, 49);
  70. colors.bgYellowBright = format(103, 49);
  71. colors.bgBlueBright = format(104, 49);
  72. colors.bgMagentaBright = format(105, 49);
  73. colors.bgCyanBright = format(106, 49);
  74. colors.bgWhiteBright = format(107, 49);
  75. module.exports = colors;