colour.js 690 B

1234567891011121314151617181920212223242526
  1. /**
  2. * Encodes a string in a colour: red, yellow or green
  3. * @param {String} c colour to highlight in
  4. * @param {String} str the string to encode
  5. * @return {String} coloured string for terminal printing
  6. */
  7. function colour(c, str) {
  8. return (colour[c] || colour.black) + str + colour.black;
  9. }
  10. function strip(str) {
  11. re.lastIndex = 0; // reset position
  12. return str.replace(re, '');
  13. }
  14. colour.red = '\x1B[31m';
  15. colour.yellow = '\x1B[33m';
  16. colour.green = '\x1B[32m';
  17. colour.black = '\x1B[39m';
  18. var reStr = Object.keys(colour).map(key => colour[key]).join('|');
  19. var re = new RegExp(('(' + reStr + ')').replace(/\[/g, '\\['), 'g');
  20. colour.strip = strip;
  21. module.exports = colour;