colorConsole.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var supportsColor = require('color-support');
  2. var hasColors = supportsColor();
  3. var styles = {
  4. black: hasColors ? '\x1b[30m' : '',
  5. red: hasColors ? '\x1b[31m' : '',
  6. green: hasColors ? '\x1b[32m' : '',
  7. yellow: hasColors ? '\x1b[33m' : '',
  8. blue: hasColors ? '\x1b[34m' : '',
  9. magenta: hasColors ? '\x1b[35m' : '',
  10. cyan: hasColors ? '\x1b[36m' : '',
  11. gray: hasColors ? '\x1b[90m' : '',
  12. white: hasColors ? '\x1b[97m' : '',
  13. bgBlack: hasColors ? '\x1b[40m' : '',
  14. bgRed: hasColors ? '\x1b[41m' : '',
  15. bgGreen: hasColors ? '\x1b[42m' : '',
  16. bgYellow: hasColors ? '\x1b[43m' : '',
  17. bgBlue: hasColors ? '\x1b[44m' : '',
  18. bgMagenta: hasColors ? '\x1b[45m' : '',
  19. bgCyan: hasColors ? '\x1b[46m' : '',
  20. bgWhite: hasColors ? '\x1b[47m' : '',
  21. bold: hasColors ? '\x1b[1m' : '',
  22. italic: hasColors ? '\x1b[3m' : '',
  23. underline: hasColors ? '\x1b[4m' : '',
  24. strikethrough: hasColors ? '\x1b[9m' : '',
  25. }
  26. var clear = hasColors ? '\x1b[0m' : '';
  27. var currentColor = undefined;
  28. function getTimestamp() {
  29. var time = new Date();
  30. var timeInString = ("0" + time.getHours()).slice(-2) + ":" +
  31. ("0" + time.getMinutes()).slice(-2) + ":" +
  32. ("0" + time.getSeconds()).slice(-2);
  33. if (currentColor) {
  34. return styles.white + '[' + currentColor + timeInString + clear + styles.white + ']';
  35. }
  36. else {
  37. return styles.white + '[' + styles.gray + timeInString + styles.white + ']';
  38. }
  39. }
  40. function log() {
  41. currentColor = styles.gray;
  42. var time = getTimestamp();
  43. process.stdout.write(time + ' ');
  44. currentColor = undefined;
  45. console.log.apply(console, arguments);
  46. return this;
  47. }
  48. function warn() {
  49. currentColor = styles.yellow;
  50. var time = getTimestamp();
  51. process.stdout.write(time + ' ');
  52. currentColor = undefined;
  53. console.warn.apply(console, arguments);
  54. return this;
  55. }
  56. function err() {
  57. currentColor = styles.red;
  58. var time = getTimestamp();
  59. process.stderr.write(time + ' ');
  60. currentColor = undefined;
  61. console.error.apply(console, arguments);
  62. return this;
  63. }
  64. function success() {
  65. currentColor = styles.green;
  66. var time = getTimestamp();
  67. process.stdout.write(time + ' ');
  68. currentColor = undefined;
  69. console.log.apply(console, arguments);
  70. return this;
  71. }
  72. function emptyLine() {
  73. console.log();
  74. }
  75. for (let style in styles) {
  76. Object.defineProperty(String.prototype, style, {
  77. get: function() {
  78. return styles[style] + this + clear;
  79. },
  80. enumerable: true,
  81. configurable: true
  82. });
  83. }
  84. module.exports = {
  85. log,
  86. warn,
  87. error: err,
  88. success,
  89. emptyLine
  90. };