colors.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
  2. if (typeof process !== 'undefined') {
  3. ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env || {});
  4. isTTY = process.stdout && process.stdout.isTTY;
  5. }
  6. export const $ = {
  7. enabled: !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== 'dumb' && (
  8. FORCE_COLOR != null && FORCE_COLOR !== '0' || isTTY
  9. )
  10. }
  11. function init(x, y) {
  12. let rgx = new RegExp(`\\x1b\\[${y}m`, 'g');
  13. let open = `\x1b[${x}m`, close = `\x1b[${y}m`;
  14. return function (txt) {
  15. if (!$.enabled || txt == null) return txt;
  16. return open + (!!~(''+txt).indexOf(close) ? txt.replace(rgx, close + open) : txt) + close;
  17. };
  18. }
  19. // modifiers
  20. export const reset = init(0, 0);
  21. export const bold = init(1, 22);
  22. export const dim = init(2, 22);
  23. export const italic = init(3, 23);
  24. export const underline = init(4, 24);
  25. export const inverse = init(7, 27);
  26. export const hidden = init(8, 28);
  27. export const strikethrough = init(9, 29);
  28. // colors
  29. export const black = init(30, 39);
  30. export const red = init(31, 39);
  31. export const green = init(32, 39);
  32. export const yellow = init(33, 39);
  33. export const blue = init(34, 39);
  34. export const magenta = init(35, 39);
  35. export const cyan = init(36, 39);
  36. export const white = init(37, 39);
  37. export const gray = init(90, 39);
  38. export const grey = init(90, 39);
  39. // background colors
  40. export const bgBlack = init(40, 49);
  41. export const bgRed = init(41, 49);
  42. export const bgGreen = init(42, 49);
  43. export const bgYellow = init(43, 49);
  44. export const bgBlue = init(44, 49);
  45. export const bgMagenta = init(45, 49);
  46. export const bgCyan = init(46, 49);
  47. export const bgWhite = init(47, 49);