index.js 2.3 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. export const options = Object.defineProperty({}, "enabled", {
  26. get: () => enabled,
  27. set: (value) => (enabled = value),
  28. })
  29. export const reset = init(0, 0)
  30. export const bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m")
  31. export const dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m")
  32. export const italic = init(3, 23)
  33. export const underline = init(4, 24)
  34. export const inverse = init(7, 27)
  35. export const hidden = init(8, 28)
  36. export const strikethrough = init(9, 29)
  37. export const black = init(30, 39)
  38. export const red = init(31, 39)
  39. export const green = init(32, 39)
  40. export const yellow = init(33, 39)
  41. export const blue = init(34, 39)
  42. export const magenta = init(35, 39)
  43. export const cyan = init(36, 39)
  44. export const white = init(37, 39)
  45. export const gray = init(90, 39)
  46. export const bgBlack = init(40, 49)
  47. export const bgRed = init(41, 49)
  48. export const bgGreen = init(42, 49)
  49. export const bgYellow = init(43, 49)
  50. export const bgBlue = init(44, 49)
  51. export const bgMagenta = init(45, 49)
  52. export const bgCyan = init(46, 49)
  53. export const bgWhite = init(47, 49)
  54. export const blackBright = init(90, 39)
  55. export const redBright = init(91, 39)
  56. export const greenBright = init(92, 39)
  57. export const yellowBright = init(93, 39)
  58. export const blueBright = init(94, 39)
  59. export const magentaBright = init(95, 39)
  60. export const cyanBright = init(96, 39)
  61. export const whiteBright = init(97, 39)
  62. export const bgBlackBright = init(100, 49)
  63. export const bgRedBright = init(101, 49)
  64. export const bgGreenBright = init(102, 49)
  65. export const bgYellowBright = init(103, 49)
  66. export const bgBlueBright = init(104, 49)
  67. export const bgMagentaBright = init(105, 49)
  68. export const bgCyanBright = init(106, 49)
  69. export const bgWhiteBright = init(107, 49)