picocolors.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. let argv = process.argv || [],
  2. env = process.env
  3. let isColorSupported =
  4. !("NO_COLOR" in env || argv.includes("--no-color")) &&
  5. ("FORCE_COLOR" in env ||
  6. argv.includes("--color") ||
  7. process.platform === "win32" ||
  8. (require != null && require("tty").isatty(1) && env.TERM !== "dumb") ||
  9. "CI" in env)
  10. let formatter =
  11. (open, close, replace = open) =>
  12. input => {
  13. let string = "" + input
  14. let index = string.indexOf(close, open.length)
  15. return ~index
  16. ? open + replaceClose(string, close, replace, index) + close
  17. : open + string + close
  18. }
  19. let replaceClose = (string, close, replace, index) => {
  20. let result = ""
  21. let cursor = 0
  22. do {
  23. result += string.substring(cursor, index) + replace
  24. cursor = index + close.length
  25. index = string.indexOf(close, cursor)
  26. } while (~index)
  27. return result + string.substring(cursor)
  28. }
  29. let createColors = (enabled = isColorSupported) => {
  30. let init = enabled ? formatter : () => String
  31. return {
  32. isColorSupported: enabled,
  33. reset: init("\x1b[0m", "\x1b[0m"),
  34. bold: init("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
  35. dim: init("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
  36. italic: init("\x1b[3m", "\x1b[23m"),
  37. underline: init("\x1b[4m", "\x1b[24m"),
  38. inverse: init("\x1b[7m", "\x1b[27m"),
  39. hidden: init("\x1b[8m", "\x1b[28m"),
  40. strikethrough: init("\x1b[9m", "\x1b[29m"),
  41. black: init("\x1b[30m", "\x1b[39m"),
  42. red: init("\x1b[31m", "\x1b[39m"),
  43. green: init("\x1b[32m", "\x1b[39m"),
  44. yellow: init("\x1b[33m", "\x1b[39m"),
  45. blue: init("\x1b[34m", "\x1b[39m"),
  46. magenta: init("\x1b[35m", "\x1b[39m"),
  47. cyan: init("\x1b[36m", "\x1b[39m"),
  48. white: init("\x1b[37m", "\x1b[39m"),
  49. gray: init("\x1b[90m", "\x1b[39m"),
  50. bgBlack: init("\x1b[40m", "\x1b[49m"),
  51. bgRed: init("\x1b[41m", "\x1b[49m"),
  52. bgGreen: init("\x1b[42m", "\x1b[49m"),
  53. bgYellow: init("\x1b[43m", "\x1b[49m"),
  54. bgBlue: init("\x1b[44m", "\x1b[49m"),
  55. bgMagenta: init("\x1b[45m", "\x1b[49m"),
  56. bgCyan: init("\x1b[46m", "\x1b[49m"),
  57. bgWhite: init("\x1b[47m", "\x1b[49m"),
  58. blackBright: init("\x1b[90m", "\x1b[39m"),
  59. redBright: init("\x1b[91m", "\x1b[39m"),
  60. greenBright: init("\x1b[92m", "\x1b[39m"),
  61. yellowBright: init("\x1b[93m", "\x1b[39m"),
  62. blueBright: init("\x1b[94m", "\x1b[39m"),
  63. magentaBright: init("\x1b[95m", "\x1b[39m"),
  64. cyanBright: init("\x1b[96m", "\x1b[39m"),
  65. whiteBright: init("\x1b[97m", "\x1b[39m"),
  66. bgBlackBright: init("\x1b[100m","\x1b[49m"),
  67. bgRedBright: init("\x1b[101m","\x1b[49m"),
  68. bgGreenBright: init("\x1b[102m","\x1b[49m"),
  69. bgYellowBright: init("\x1b[103m","\x1b[49m"),
  70. bgBlueBright: init("\x1b[104m","\x1b[49m"),
  71. bgMagentaBright: init("\x1b[105m","\x1b[49m"),
  72. bgCyanBright: init("\x1b[106m","\x1b[49m"),
  73. bgWhiteBright: init("\x1b[107m","\x1b[49m"),
  74. }
  75. }
  76. module.exports = createColors()
  77. module.exports.createColors = createColors