mirror of
				https://github.com/flynx/colors.js.git
				synced 2025-11-04 05:50:10 +00:00 
			
		
		
		
	
						commit
						4a63717497
					
				
							
								
								
									
										85
									
								
								colors.js
									
									
									
									
									
								
							
							
						
						
									
										85
									
								
								colors.js
									
									
									
									
									
								
							@ -23,14 +23,24 @@ THE SOFTWARE.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.mode = "console";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// prototypes the string object to have additional method calls that add terminal colors
 | 
					// prototypes the string object to have additional method calls that add terminal colors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var addProperty = function (color, func) {
 | 
				
			||||||
 | 
					  exports[color] = function(str) {
 | 
				
			||||||
 | 
					    return func.apply(str);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  String.prototype.__defineGetter__(color, func);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var isHeadless = (typeof module !== 'undefined');
 | 
					var isHeadless = (typeof module !== 'undefined');
 | 
				
			||||||
['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
 | 
					['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // __defineGetter__ at the least works in more browsers
 | 
					  // __defineGetter__ at the least works in more browsers
 | 
				
			||||||
  // http://robertnyman.com/javascript/javascript-getters-setters.html
 | 
					  // http://robertnyman.com/javascript/javascript-getters-setters.html
 | 
				
			||||||
  // Object.defineProperty only works in Chrome
 | 
					  // Object.defineProperty only works in Chrome
 | 
				
			||||||
  String.prototype.__defineGetter__(style, function () {
 | 
					  addProperty(style, function () {
 | 
				
			||||||
    return isHeadless ?
 | 
					    return isHeadless ?
 | 
				
			||||||
             stylize(this, style) : // for those running in node (headless environments)
 | 
					             stylize(this, style) : // for those running in node (headless environments)
 | 
				
			||||||
             this.replace(/( )/, '$1'); // and for those running in browsers:
 | 
					             this.replace(/( )/, '$1'); // and for those running in browsers:
 | 
				
			||||||
@ -40,7 +50,7 @@ var isHeadless = (typeof module !== 'undefined');
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// prototypes string with method "rainbow"
 | 
					// prototypes string with method "rainbow"
 | 
				
			||||||
// rainbow will apply a the color spectrum to a string, changing colors every letter
 | 
					// rainbow will apply a the color spectrum to a string, changing colors every letter
 | 
				
			||||||
String.prototype.__defineGetter__('rainbow', function () {
 | 
					addProperty('rainbow', function () {
 | 
				
			||||||
  if (!isHeadless) {
 | 
					  if (!isHeadless) {
 | 
				
			||||||
    return this.replace(/( )/, '$1');
 | 
					    return this.replace(/( )/, '$1');
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -59,30 +69,53 @@ String.prototype.__defineGetter__('rainbow', function () {
 | 
				
			|||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function stylize(str, style) {
 | 
					function stylize(str, style) {
 | 
				
			||||||
  var styles = {
 | 
					  if (exports.mode == 'console') {
 | 
				
			||||||
  //styles
 | 
					    var styles = {
 | 
				
			||||||
  'bold'      : [1,  22],
 | 
					      //styles
 | 
				
			||||||
  'italic'    : [3,  23],
 | 
					      'bold'      : ['\033[1m',  '\033[22m'],
 | 
				
			||||||
  'underline' : [4,  24],
 | 
					      'italic'    : ['\033[3m',  '\033[23m'],
 | 
				
			||||||
  'inverse'   : [7,  27],
 | 
					      'underline' : ['\033[4m',  '\033[24m'],
 | 
				
			||||||
  //grayscale
 | 
					      'inverse'   : ['\033[7m',  '\033[27m'],
 | 
				
			||||||
  'white'     : [37, 39],
 | 
					      //grayscale
 | 
				
			||||||
  'grey'      : [90, 39],
 | 
					      'white'     : ['\033[37m', '\033[39m'],
 | 
				
			||||||
  'black'     : [90, 39],
 | 
					      'grey'      : ['\033[90m', '\033[39m'],
 | 
				
			||||||
  //colors
 | 
					      'black'     : ['\033[30m', '\033[39m'],
 | 
				
			||||||
  'blue'      : [34, 39],
 | 
					      //colors
 | 
				
			||||||
  'cyan'      : [36, 39],
 | 
					      'blue'      : ['\033[34m', '\033[39m'],
 | 
				
			||||||
  'green'     : [32, 39],
 | 
					      'cyan'      : ['\033[36m', '\033[39m'],
 | 
				
			||||||
  'magenta'   : [35, 39],
 | 
					      'green'     : ['\033[32m', '\033[39m'],
 | 
				
			||||||
  'red'       : [31, 39],
 | 
					      'magenta'   : ['\033[35m', '\033[39m'],
 | 
				
			||||||
  'yellow'    : [33, 39]
 | 
					      'red'       : ['\033[31m', '\033[39m'],
 | 
				
			||||||
  };
 | 
					      'yellow'    : ['\033[33m', '\033[39m']
 | 
				
			||||||
  return '\033[' + styles[style][0] + 'm' + str +
 | 
					    };
 | 
				
			||||||
         '\033[' + styles[style][1] + 'm';
 | 
					  } else if (exports.mode == 'browser') {
 | 
				
			||||||
 | 
					    var styles = {
 | 
				
			||||||
 | 
					      //styles
 | 
				
			||||||
 | 
					      'bold'      : ['<b>',  '</b>'],
 | 
				
			||||||
 | 
					      'italic'    : ['<i>',  '</i>'],
 | 
				
			||||||
 | 
					      'underline' : ['<u>',  '</u>'],
 | 
				
			||||||
 | 
					      'inverse'   : ['<span style="background-color:black;color:white;">',  '</span>'],
 | 
				
			||||||
 | 
					      //grayscale
 | 
				
			||||||
 | 
					      'white'     : ['<span style="color:white;">',   '</span>'],
 | 
				
			||||||
 | 
					      'grey'      : ['<span style="color:grey;">',    '</span>'],
 | 
				
			||||||
 | 
					      'black'     : ['<span style="color:black;">',   '</span>'],
 | 
				
			||||||
 | 
					      //colors
 | 
				
			||||||
 | 
					      'blue'      : ['<span style="color:blue;">',    '</span>'],
 | 
				
			||||||
 | 
					      'cyan'      : ['<span style="color:cyan;">',    '</span>'],
 | 
				
			||||||
 | 
					      'green'     : ['<span style="color:green;">',   '</span>'],
 | 
				
			||||||
 | 
					      'magenta'   : ['<span style="color:magenta;">', '</span>'],
 | 
				
			||||||
 | 
					      'red'       : ['<span style="color:red;">',     '</span>'],
 | 
				
			||||||
 | 
					      'yellow'    : ['<span style="color:yellow;">',  '</span>']
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    console.log('unsupported mode, try "browser" or "console"');
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return styles[style][0] + str + styles[style][1];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// don't summon zalgo
 | 
					// don't summon zalgo
 | 
				
			||||||
String.prototype.__defineGetter__('zalgo', function () {
 | 
					addProperty('zalgo', function () {
 | 
				
			||||||
  return zalgo(this);
 | 
					  return zalgo(this);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -189,3 +222,7 @@ function zalgo(text, options) {
 | 
				
			|||||||
  };
 | 
					  };
 | 
				
			||||||
  return heComes(text);
 | 
					  return heComes(text);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					addProperty('stripColors', function() {
 | 
				
			||||||
 | 
					  return ("" + this).replace(/\u001b\[\d+m/g,'');
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										15
									
								
								example.js
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								example.js
									
									
									
									
									
								
							@ -1,7 +1,20 @@
 | 
				
			|||||||
var sys = require('sys');
 | 
					var sys = require('sys');
 | 
				
			||||||
var colors = require('./colors');
 | 
					var colors = require('./colors');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//colors.mode = "browser";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var test = colors.red("hopefully colorless output");
 | 
				
			||||||
sys.puts('Rainbows are fun!'.rainbow);
 | 
					sys.puts('Rainbows are fun!'.rainbow);
 | 
				
			||||||
sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
 | 
					sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
 | 
				
			||||||
sys.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
 | 
					sys.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
 | 
				
			||||||
// sys.puts('zalgo time!'.zalgo);
 | 
					//sys.puts('zalgo time!'.zalgo);
 | 
				
			||||||
 | 
					sys.puts(test.stripColors);
 | 
				
			||||||
 | 
					sys.puts("a".grey + " b".black);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					sys.puts(colors.rainbow('Rainbows are fun!'));
 | 
				
			||||||
 | 
					sys.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
 | 
				
			||||||
 | 
					sys.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
 | 
				
			||||||
 | 
					//sys.puts(colors.zalgo('zalgo time!'));
 | 
				
			||||||
 | 
					sys.puts(colors.stripColors(test));
 | 
				
			||||||
 | 
					sys.puts(colors.grey("a") + colors.black(" b"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user