summaryrefslogtreecommitdiff
path: root/lib/sys.js
diff options
context:
space:
mode:
authorTrent Mick <trentm@gmail.com>2010-09-03 21:25:35 -0700
committerRyan Dahl <ry@tinyclouds.org>2010-09-08 09:47:13 -0700
commit1d961a66309f1ab649fbb54f9badaf53100c6020 (patch)
treed687836892d2b2a962df0c30ac031f87616c921c /lib/sys.js
parent5330fea954010addfd91fe3ac20bd391a11c90a0 (diff)
downloadandroid-node-v8-1d961a66309f1ab649fbb54f9badaf53100c6020.tar.gz
android-node-v8-1d961a66309f1ab649fbb54f9badaf53100c6020.tar.bz2
android-node-v8-1d961a66309f1ab649fbb54f9badaf53100c6020.zip
add ANSI coloring option to sys.inspect and, by default, to the repl
Diffstat (limited to 'lib/sys.js')
-rw-r--r--lib/sys.js81
1 files changed, 63 insertions, 18 deletions
diff --git a/lib/sys.js b/lib/sys.js
index 8480facb7e..1d34713f3a 100644
--- a/lib/sys.js
+++ b/lib/sys.js
@@ -33,10 +33,51 @@ var error = exports.error = function (x) {
*
* @param {Object} value The object to print out
* @param {Boolean} showHidden Flag that shows hidden (not enumerable)
- * properties of objects.
+ * properties of objects.
+ * @param {Number} depth Depth in which to descend in object. Default is 2.
+ * @param {Boolean} colors Flag to turn on ANSI escape codes to color the
+ * output. Default is false (no coloring).
*/
-exports.inspect = function (obj, showHidden, depth) {
+exports.inspect = function (obj, showHidden, depth, colors) {
var seen = [];
+
+ var stylize = function (str, styleType) {
+ // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
+ var styles = { 'bold' : [1, 22]
+ , 'italic' : [3, 23]
+ , 'underline' : [4, 24]
+ , 'inverse' : [7, 27]
+ , 'white' : [37, 39]
+ , 'grey' : [90, 39]
+ , 'black' : [30, 39]
+ , 'blue' : [34, 39]
+ , 'cyan' : [36, 39]
+ , 'green' : [32, 39]
+ , 'magenta' : [35, 39]
+ , 'red' : [31, 39]
+ , 'yellow' : [33, 39]
+ };
+ var style = { "special": "grey"
+ , "number": "blue"
+ , "boolean": "blue"
+ , "undefined": "red"
+ , "null": "red"
+ , "string": "green"
+ , "date": "magenta"
+ //, "name": intentionally not styling
+ , "regexp": "cyan"
+ }[styleType];
+ if (style) {
+ return '\033[' + styles[style][0] + 'm' + str +
+ '\033[' + styles[style][1] + 'm';
+ } else {
+ return str;
+ }
+ };
+ if (! colors) {
+ stylize = function(str, styleType) { return str; };
+ }
+
function format(value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
@@ -50,16 +91,18 @@ exports.inspect = function (obj, showHidden, depth) {
// Primitive types cannot have properties
switch (typeof value) {
- case 'undefined': return 'undefined';
- case 'string': return JSON.stringify(value).replace(/'/g, "\\'")
- .replace(/\\"/g, '"')
- .replace(/(^"|"$)/g, "'");
- case 'number': return '' + value;
- case 'boolean': return '' + value;
+ case 'undefined': return stylize('undefined', 'undefined');
+ case 'string': return stylize(
+ JSON.stringify(value).replace(/'/g, "\\'")
+ .replace(/\\"/g, '"')
+ .replace(/(^"|"$)/g, "'"),
+ 'string');
+ case 'number': return stylize('' + value, 'number');
+ case 'boolean': return stylize('' + value, 'boolean');
}
// For some reason typeof null is "object", so special case here.
if (value === null) {
- return 'null';
+ return stylize('null', 'null');
}
// Look up the keys of the object.
@@ -74,15 +117,15 @@ exports.inspect = function (obj, showHidden, depth) {
// Functions without properties can be shortcutted.
if (typeof value === 'function' && keys.length === 0) {
if (isRegExp(value)) {
- return '' + value;
+ return stylize('' + value, 'regexp');
} else {
- return '[Function]';
+ return stylize('[Function]', 'special');
}
}
// Dates without properties can be shortcutted
if (isDate(value) && keys.length === 0) {
- return value.toUTCString();
+ return stylize(value.toUTCString(), 'date');
}
var base, type, braces;
@@ -115,9 +158,9 @@ exports.inspect = function (obj, showHidden, depth) {
if (recurseTimes < 0) {
if (isRegExp(value)) {
- return '' + value;
+ return stylize('' + value, "regexp");
} else {
- return "[Object]";
+ return stylize("[Object]", "special");
}
}
@@ -126,13 +169,13 @@ exports.inspect = function (obj, showHidden, depth) {
if (value.__lookupGetter__) {
if (value.__lookupGetter__(key)) {
if (value.__lookupSetter__(key)) {
- str = "[Getter/Setter]";
+ str = stylize("[Getter/Setter]", "special");
} else {
- str = "[Getter]";
+ str = stylize("[Getter]", "special");
}
} else {
if (value.__lookupSetter__(key)) {
- str = "[Setter]";
+ str = stylize("[Setter]", "special");
}
}
}
@@ -160,7 +203,7 @@ exports.inspect = function (obj, showHidden, depth) {
}
}
} else {
- str = '[Circular]';
+ str = stylize('[Circular]', 'special');
}
}
if (typeof name === 'undefined') {
@@ -170,11 +213,13 @@ exports.inspect = function (obj, showHidden, depth) {
name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length-2);
+ name = stylize(name, "name");
}
else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'");
+ name = stylize(name, "string");
}
}