From 831b72631bcaf89f9f8c9cca8a6861d689402b22 Mon Sep 17 00:00:00 2001 From: dodo Date: Fri, 3 Jun 2011 18:34:59 +0200 Subject: [PATCH 1/4] pretty print buffers with hexy when installed --- lib/eyes.js | 31 ++++++++++++++++++++++++++++++- test/eyes-test.js | 2 ++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/eyes.js b/lib/eyes.js index 23caafe..fa01b1c 100644 --- a/lib/eyes.js +++ b/lib/eyes.js @@ -26,6 +26,14 @@ eyes.defaults = { bool: 'blue', // true false regexp: 'green', // /\d+/ }, + hexy: { + width: 16, + numbering: 'hex_bytes', + format: 'twos', + caps: 'lower', + annotate: 'ascii', + prefix: '', + }, pretty: true, // Indent object literals hideFunctions: false, stream: process.stdout, @@ -126,6 +134,7 @@ function stringify(obj, options) { case "date" : return stylize(obj.toUTCString()); case "array" : return stringifyArray(obj, options, stack.length); case "object" : return stringifyObject(obj, options, stack.length); + case "buffer" : return stringifyBuffer(obj, options, stack.length); } })(obj); @@ -193,10 +202,30 @@ function stringifyObject(obj, options, level) { } }; +// Convert a buffer to string such as 00000000: 00 when hexy is installed +// otherwise it shows the Buffer as Object +var hexy = null; +try {hexy = require('hexy').hexy} catch(e) {}; +function stringifyBuffer(obj, options, level) { + if (hexy === null || !options.pretty) + return stringifyObject(obj, options, level); + + var opts = {}; + Object.keys(options.hexy).forEach(function (k) { + if (options.hexy.hasOwnProperty(k)) + opts[k] = options.hexy[k]; + }); + if (opts.indent === undefined) + opts.indent = level * 4 + 1; + + var out = hexy(obj, opts); + return (out.split('\n').length > 2 ? '\n':'') + out; +}; + // A better `typeof` function typeOf(value) { var s = typeof(value), - types = [Object, Array, String, RegExp, Number, Function, Boolean, Date]; + types = [Object, Array, Buffer, String, RegExp, Number, Function, Boolean, Date]; if (s === 'object' || s === 'function') { if (value) { diff --git a/test/eyes-test.js b/test/eyes-test.js index d3a1d26..6669f0e 100644 --- a/test/eyes-test.js +++ b/test/eyes-test.js @@ -47,6 +47,8 @@ eyes.inspect({ }, 'complex'); +eyes.inspect(new Buffer(100), "buffer"); + var inspect = eyes.inspector({ stream: null }); sys.puts(inspect('something', "something")); From dec2ce1500cfad9d3720aa9d02c944520d79d6e9 Mon Sep 17 00:00:00 2001 From: dodo Date: Fri, 3 Jun 2011 18:49:13 +0200 Subject: [PATCH 2/4] merge hexy options with defaults --- lib/eyes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/eyes.js b/lib/eyes.js index fa01b1c..20cb81a 100644 --- a/lib/eyes.js +++ b/lib/eyes.js @@ -53,6 +53,7 @@ eyes.inspector = function (options) { // if not, we just return the stringified object. eyes.inspect = function (obj, label, options) { options = merge(this.defaults, options || {}); + options.hexy = merge(this.defaults.hexy, options.hexy || {}); if (options.stream) { return this.print(stringify(obj, options), label, options); From 8daf4d2f0ca33a9bcc00e5c9abdeadd7d5abf00e Mon Sep 17 00:00:00 2001 From: dodo Date: Fri, 3 Jun 2011 19:00:06 +0200 Subject: [PATCH 3/4] force disable hexy in options --- lib/eyes.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/eyes.js b/lib/eyes.js index 20cb81a..429297c 100644 --- a/lib/eyes.js +++ b/lib/eyes.js @@ -53,7 +53,8 @@ eyes.inspector = function (options) { // if not, we just return the stringified object. eyes.inspect = function (obj, label, options) { options = merge(this.defaults, options || {}); - options.hexy = merge(this.defaults.hexy, options.hexy || {}); + if (options.hexy !== false) + options.hexy = merge(this.defaults.hexy, options.hexy || {}); if (options.stream) { return this.print(stringify(obj, options), label, options); @@ -208,7 +209,7 @@ function stringifyObject(obj, options, level) { var hexy = null; try {hexy = require('hexy').hexy} catch(e) {}; function stringifyBuffer(obj, options, level) { - if (hexy === null || !options.pretty) + if (hexy === null || !options.hexy || !options.pretty) return stringifyObject(obj, options, level); var opts = {}; From f054f11607d425bdfbde5e4ce75362fd35d6f9ff Mon Sep 17 00:00:00 2001 From: dodo Date: Sat, 4 Jun 2011 15:17:25 +0200 Subject: [PATCH 4/4] bugfix: null in array --- lib/eyes.js | 2 +- test/eyes-test.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/eyes.js b/lib/eyes.js index 429297c..8d94500 100644 --- a/lib/eyes.js +++ b/lib/eyes.js @@ -159,7 +159,7 @@ function stringifyString (str, options) { function stringifyArray(ary, options, level) { var out = []; var pretty = options.pretty && (ary.length > 4 || ary.some(function (o) { - return (typeof(o) === 'object' && Object.keys(o).length > 0) || + return (typeof(o) === 'object' && o && Object.keys(o).length > 0) || (Array.isArray(o) && o.length > 0); })); var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' '; diff --git a/test/eyes-test.js b/test/eyes-test.js index 6669f0e..93c19b7 100644 --- a/test/eyes-test.js +++ b/test/eyes-test.js @@ -9,6 +9,7 @@ eyes.inspect({ func: function () {}, bool: false, nil: null, + nilarr: [null], undef: undefined, object: {attr: []} }, "native types");