picocolors.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. let tty = require("tty")
  2. let isColorSupported =
  3. !("NO_COLOR" in process.env || process.argv.includes("--no-color")) &&
  4. ("FORCE_COLOR" in process.env ||
  5. process.argv.includes("--color") ||
  6. process.platform === "win32" ||
  7. (tty.isatty(1) && process.env.TERM !== "dumb") ||
  8. "CI" in process.env)
  9. function formatter(open, close, replace = open) {
  10. return (input) => {
  11. let string = "" + input
  12. let index = string.indexOf(close, open.length)
  13. return !~index
  14. ? open + string + close
  15. : open + replaceClose(string, close, replace, index) + close
  16. }
  17. }
  18. function replaceClose(string, close, replace, index) {
  19. let start = string.substring(0, index) + replace
  20. let end = string.substring(index + close.length)
  21. let nextIndex = end.indexOf(close)
  22. return !~nextIndex ? start + end : start + replaceClose(end, close, replace, nextIndex)
  23. }
  24. function createColors(enabled = isColorSupported) {
  25. return {
  26. isColorSupported: enabled,
  27. reset: enabled ? (s) => `\x1b[0m${s}\x1b[0m` : String,
  28. bold: enabled ? formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m") : String,
  29. dim: enabled ? formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m") : String,
  30. italic: enabled ? formatter("\x1b[3m", "\x1b[23m") : String,
  31. underline: enabled ? formatter("\x1b[4m", "\x1b[24m") : String,
  32. inverse: enabled ? formatter("\x1b[7m", "\x1b[27m") : String,
  33. hidden: enabled ? formatter("\x1b[8m", "\x1b[28m") : String,
  34. strikethrough: enabled ? formatter("\x1b[9m", "\x1b[29m") : String,
  35. black: enabled ? formatter("\x1b[30m", "\x1b[39m") : String,
  36. red: enabled ? formatter("\x1b[31m", "\x1b[39m") : String,
  37. green: enabled ? formatter("\x1b[32m", "\x1b[39m") : String,
  38. yellow: enabled ? formatter("\x1b[33m", "\x1b[39m") : String,
  39. blue: enabled ? formatter("\x1b[34m", "\x1b[39m") : String,
  40. magenta: enabled ? formatter("\x1b[35m", "\x1b[39m") : String,
  41. cyan: enabled ? formatter("\x1b[36m", "\x1b[39m") : String,
  42. white: enabled ? formatter("\x1b[37m", "\x1b[39m") : String,
  43. gray: enabled ? formatter("\x1b[90m", "\x1b[39m") : String,
  44. bgBlack: enabled ? formatter("\x1b[40m", "\x1b[49m") : String,
  45. bgRed: enabled ? formatter("\x1b[41m", "\x1b[49m") : String,
  46. bgGreen: enabled ? formatter("\x1b[42m", "\x1b[49m") : String,
  47. bgYellow: enabled ? formatter("\x1b[43m", "\x1b[49m") : String,
  48. bgBlue: enabled ? formatter("\x1b[44m", "\x1b[49m") : String,
  49. bgMagenta: enabled ? formatter("\x1b[45m", "\x1b[49m") : String,
  50. bgCyan: enabled ? formatter("\x1b[46m", "\x1b[49m") : String,
  51. bgWhite: enabled ? formatter("\x1b[47m", "\x1b[49m") : String,
  52. }
  53. }
  54. module.exports = createColors()
  55. module.exports.createColors = createColors