index.cjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. let enabled =
  2. !("NO_COLOR" in process.env) &&
  3. ("FORCE_COLOR" in process.env ||
  4. process.platform === "win32" ||
  5. (process.stdout != null &&
  6. process.stdout.isTTY &&
  7. process.env.TERM &&
  8. process.env.TERM !== "dumb"))
  9. const raw = (open, close, searchRegex, replaceValue) => (s) =>
  10. enabled
  11. ? open +
  12. (~(s += "").indexOf(close, 4) // skip opening \x1b[
  13. ? s.replace(searchRegex, replaceValue)
  14. : s) +
  15. close
  16. : s
  17. const init = (open, close) => {
  18. return raw(
  19. `\x1b[${open}m`,
  20. `\x1b[${close}m`,
  21. new RegExp(`\\x1b\\[${close}m`, "g"),
  22. `\x1b[${open}m`
  23. )
  24. }
  25. exports.options = Object.defineProperty({}, "enabled", {
  26. get: () => enabled,
  27. set: (value) => (enabled = value),
  28. })
  29. exports.reset = init(0, 0)
  30. exports.bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m")
  31. exports.dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m")
  32. exports.italic = init(3, 23)
  33. exports.underline = init(4, 24)
  34. exports.inverse = init(7, 27)
  35. exports.hidden = init(8, 28)
  36. exports.strikethrough = init(9, 29)
  37. exports.black = init(30, 39)
  38. exports.red = init(31, 39)
  39. exports.green = init(32, 39)
  40. exports.yellow = init(33, 39)
  41. exports.blue = init(34, 39)
  42. exports.magenta = init(35, 39)
  43. exports.cyan = init(36, 39)
  44. exports.white = init(37, 39)
  45. exports.gray = init(90, 39)
  46. exports.bgBlack = init(40, 49)
  47. exports.bgRed = init(41, 49)
  48. exports.bgGreen = init(42, 49)
  49. exports.bgYellow = init(43, 49)
  50. exports.bgBlue = init(44, 49)
  51. exports.bgMagenta = init(45, 49)
  52. exports.bgCyan = init(46, 49)
  53. exports.bgWhite = init(47, 49)
  54. exports.blackBright = init(90, 39)
  55. exports.redBright = init(91, 39)
  56. exports.greenBright = init(92, 39)
  57. exports.yellowBright = init(93, 39)
  58. exports.blueBright = init(94, 39)
  59. exports.magentaBright = init(95, 39)
  60. exports.cyanBright = init(96, 39)
  61. exports.whiteBright = init(97, 39)
  62. exports.bgBlackBright = init(100, 49)
  63. exports.bgRedBright = init(101, 49)
  64. exports.bgGreenBright = init(102, 49)
  65. exports.bgYellowBright = init(103, 49)
  66. exports.bgBlueBright = init(104, 49)
  67. exports.bgMagentaBright = init(105, 49)
  68. exports.bgCyanBright = init(106, 49)
  69. exports.bgWhiteBright = init(107, 49)