You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import * as tty from "tty"
  2. const {
  3. env = {},
  4. argv = [],
  5. platform = "",
  6. } = typeof process === "undefined" ? {} : process
  7. const isDisabled = "NO_COLOR" in env || argv.includes("--no-color")
  8. const isForced = "FORCE_COLOR" in env || argv.includes("--color")
  9. const isWindows = platform === "win32"
  10. const isDumbTerminal = env.TERM === "dumb"
  11. const isCompatibleTerminal =
  12. tty && tty.isatty && tty.isatty(1) && env.TERM && !isDumbTerminal
  13. const isCI =
  14. "CI" in env &&
  15. ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env)
  16. export const isColorSupported =
  17. !isDisabled &&
  18. (isForced || (isWindows && !isDumbTerminal) || isCompatibleTerminal || isCI)
  19. const replaceClose = (
  20. index,
  21. string,
  22. close,
  23. replace,
  24. head = string.substring(0, index) + replace,
  25. tail = string.substring(index + close.length),
  26. next = tail.indexOf(close)
  27. ) => head + (next < 0 ? tail : replaceClose(next, tail, close, replace))
  28. const clearBleed = (index, string, open, close, replace) =>
  29. index < 0
  30. ? open + string + close
  31. : open + replaceClose(index, string, close, replace) + close
  32. const filterEmpty =
  33. (open, close, replace = open, at = open.length + 1) =>
  34. (string) =>
  35. string || !(string === "" || string === undefined)
  36. ? clearBleed(
  37. ("" + string).indexOf(close, at),
  38. string,
  39. open,
  40. close,
  41. replace
  42. )
  43. : ""
  44. const init = (open, close, replace) =>
  45. filterEmpty(`\x1b[${open}m`, `\x1b[${close}m`, replace)
  46. const colors = {
  47. reset: init(0, 0),
  48. bold: init(1, 22, "\x1b[22m\x1b[1m"),
  49. dim: init(2, 22, "\x1b[22m\x1b[2m"),
  50. italic: init(3, 23),
  51. underline: init(4, 24),
  52. inverse: init(7, 27),
  53. hidden: init(8, 28),
  54. strikethrough: init(9, 29),
  55. black: init(30, 39),
  56. red: init(31, 39),
  57. green: init(32, 39),
  58. yellow: init(33, 39),
  59. blue: init(34, 39),
  60. magenta: init(35, 39),
  61. cyan: init(36, 39),
  62. white: init(37, 39),
  63. gray: init(90, 39),
  64. bgBlack: init(40, 49),
  65. bgRed: init(41, 49),
  66. bgGreen: init(42, 49),
  67. bgYellow: init(43, 49),
  68. bgBlue: init(44, 49),
  69. bgMagenta: init(45, 49),
  70. bgCyan: init(46, 49),
  71. bgWhite: init(47, 49),
  72. blackBright: init(90, 39),
  73. redBright: init(91, 39),
  74. greenBright: init(92, 39),
  75. yellowBright: init(93, 39),
  76. blueBright: init(94, 39),
  77. magentaBright: init(95, 39),
  78. cyanBright: init(96, 39),
  79. whiteBright: init(97, 39),
  80. bgBlackBright: init(100, 49),
  81. bgRedBright: init(101, 49),
  82. bgGreenBright: init(102, 49),
  83. bgYellowBright: init(103, 49),
  84. bgBlueBright: init(104, 49),
  85. bgMagentaBright: init(105, 49),
  86. bgCyanBright: init(106, 49),
  87. bgWhiteBright: init(107, 49),
  88. }
  89. export const createColors = ({ useColor = isColorSupported } = {}) =>
  90. useColor
  91. ? colors
  92. : Object.keys(colors).reduce(
  93. (colors, key) => ({ ...colors, [key]: String }),
  94. {}
  95. )
  96. export const {
  97. reset,
  98. bold,
  99. dim,
  100. italic,
  101. underline,
  102. inverse,
  103. hidden,
  104. strikethrough,
  105. black,
  106. red,
  107. green,
  108. yellow,
  109. blue,
  110. magenta,
  111. cyan,
  112. white,
  113. gray,
  114. bgBlack,
  115. bgRed,
  116. bgGreen,
  117. bgYellow,
  118. bgBlue,
  119. bgMagenta,
  120. bgCyan,
  121. bgWhite,
  122. blackBright,
  123. redBright,
  124. greenBright,
  125. yellowBright,
  126. blueBright,
  127. magentaBright,
  128. cyanBright,
  129. whiteBright,
  130. bgBlackBright,
  131. bgRedBright,
  132. bgGreenBright,
  133. bgYellowBright,
  134. bgBlueBright,
  135. bgMagentaBright,
  136. bgCyanBright,
  137. bgWhiteBright,
  138. } = createColors()