From 79b75ad626efe5f5c05084e1087884ac14fcff45 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 30 May 2014 22:48:44 -0700 Subject: [PATCH 1/2] browser: add colors for WebKit browsers Based off of the implemention from #67, except this one matches the Node.js colored output exactly, including the trailing `diff` value. --- browser.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/browser.js b/browser.js index be1d476..4ce55cb 100644 --- a/browser.js +++ b/browser.js @@ -10,6 +10,43 @@ 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 are known + * to support "%c" CSS customizations. + */ + +var useColors = 'WebkitAppearance' in document.documentElement.style; + +/** + * 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 +55,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); } /** From 8bed009c849aec970b92566936e9dccdd8c93181 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 31 May 2014 11:16:12 -0700 Subject: [PATCH 2/2] browser: add Firebug color support --- browser.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/browser.js b/browser.js index 4ce55cb..96a2985 100644 --- a/browser.js +++ b/browser.js @@ -30,11 +30,19 @@ var colors = [ var prevColor = 0; /** - * Currently only WebKit-based Web Inspectors are known - * to support "%c" CSS customizations. + * 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 = 'WebkitAppearance' in document.documentElement.style; +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.