picocolors.js 2.6 KB

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