Merge pull request #98 from TooTallNate/add/browser-colors

browser: add colors for WebKit browsers and Firebug
This commit is contained in:
Nathan Rajlich 2014-05-31 12:24:06 -07:00
commit b259bc0a10

View File

@ -10,6 +10,51 @@ exports.log = log;
exports.save = save;
exports.load = load;
/**
* Colors.
*/
var colors = [
'cyan',
'green',
'goldenrod', // "yellow" is just too bright on a white background...
'blue',
'purple',
'red'
];
/**
* Previously assigned color.
*/
var prevColor = 0;
/**
* Currently only WebKit-based Web Inspectors and the Firebug
* extension (*not* the built-in Firefox web inpector) are
* known to support "%c" CSS customizations.
*
* TODO: add a `localStorage` variable to explicitly enable/disable colors
*/
var useColors =
// is webkit? http://stackoverflow.com/a/16459606/376773
('WebkitAppearance' in document.documentElement.style) ||
// is firebug? http://stackoverflow.com/a/398120/376773
(window.console && (console.firebug || (console.exception && console.table)));
/**
* Select a color.
*
* @return {Number}
* @api private
*/
function selectColor() {
return colors[prevColor++ % colors.length];
}
/**
* Invokes `console.log()` when available.
* No-op when `console.log` is not a "function".
@ -18,20 +63,30 @@ exports.load = load;
*/
function log(fmt) {
var args = arguments;
var curr = new Date();
var ms = curr - (this.prev || curr);
this.prev = curr;
fmt = this.namespace
+ ' '
fmt = (useColors ? '%c' : '')
+ this.namespace
+ (useColors ? '%c ' : ' ')
+ fmt
+ ' +' + exports.humanize(ms);
+ (useColors ? '%c ' : ' ')
+ '+' + exports.humanize(ms);
if (useColors) {
if (null == this.c) this.c = selectColor();
var c = 'color: ' + this.c;
args = [args[0], c, ''].concat(Array.prototype.slice.call(arguments, 1));
args.push(c);
}
// This hackery is required for IE8,
// where the `console.log` function doesn't have 'apply'
return 'object' == typeof console
&& 'function' == typeof console.log
&& Function.prototype.apply.call(console.log, console, arguments);
&& Function.prototype.apply.call(console.log, console, args);
}
/**