Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add in background colors ansi escape sequence list. Get branch up to …

…latest version. Add in a few fixes I've found along the way in the version I'm using outside of node core
  • Loading branch information...
commit 42973187885593cf1e7baa7aa0e2ac4c024cc9c9 1 parent 29aaf64
@Benvie authored
Showing with 139 additions and 127 deletions.
  1. +139 −127 lib/util.js
View
266 lib/util.js
@@ -90,7 +90,6 @@ var error = exports.error = function(x) {
* @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).
- * @param {Boolean} resolveGetters Attempt to resolve getters to their value.
* If an error is thrown the value is set to the error. Default is false.
*/
@@ -126,14 +125,30 @@ var ansi = {
magenta : [ '35', '39'],
cyan : [ '36', '39'],
white : [ '37', '39'],
- boldblack : ['1;30', '22;39'],
- boldred : ['1;31', '22;39'],
- boldgreen : ['1;32', '22;39'],
- boldyellow : ['1;33', '22;39'],
- boldblue : ['1;34', '22;39'],
- boldmagenta : ['1;35', '22;39'],
- boldcyan : ['1;36', '22;39'],
- boldwhite : ['1;37', '22;39']
+ bblack : ['1;30', '22;39'],
+ bred : ['1;31', '22;39'],
+ bgreen : ['1;32', '22;39'],
+ byellow : ['1;33', '22;39'],
+ bblue : ['1;34', '22;39'],
+ bmagenta : ['1;35', '22;39'],
+ bcyan : ['1;36', '22;39'],
+ bwhite : ['1;37', '22;39'],
+ bgblack : [ '40', '49'],
+ bgred : [ '41', '49'],
+ bggreen : [ '42', '49'],
+ bgyellow : [ '43', '49'],
+ bgblue : [ '44', '49'],
+ bgmagenta : [ '45', '49'],
+ bgcyan : [ '46', '49'],
+ bgwhite : [ '47', '49'],
+ bgbblack : [ '100', '25;49'],
+ bgbred : [ '101', '25;49'],
+ bgbgreen : [ '102', '25;49'],
+ bgbyellow : [ '103', '25;49'],
+ bgbblue : [ '104', '25;49'],
+ bgbmagenta : [ '105', '25;49'],
+ bgbcyan : [ '106', '25;49'],
+ bgbwhite : [ '107', '25;49']
};
@@ -143,55 +158,60 @@ var styles = {
Undefined : 'boldblack',
Null : 'boldblack',
// constructor functions
- Constructor : 'boldyellow',
+ Constructor : 'boldcyan',
// normal types
- Function : 'boldmagenta',
- Boolean : 'boldyellow',
- Date : 'boldred',
+ Function : 'cyan',
+ Boolean : 'magenta',
+ Date : 'magenta',
Error : 'boldred',
- Number : 'yellow',
- RegExp : 'boldred',
+ Number : 'boldmagenta',
+ RegExp : 'boldmagenta',
// proprty names and strings
HString : 'green',
String : 'boldgreen',
- HConstant : 'cyan',
- Constant : 'boldcyan',
+ HConstant : 'yellow',
+ Constant : 'boldyellow',
HName : 'boldblack',
Name : 'boldwhite',
// meta-labels
More : 'red',
Accessor : 'magenta',
Circular : 'red',
+ Proto : 'boldred',
+ ObjState : 'boldred',
// brackets
Square : 'white',
Curly : 'white'
};
-// callbind parameterizes `this`
-var callbind = Function.prototype.call.bind.bind(Function.prototype.call);
-var errorToString = callbind(Error.prototype.toString);
-
-// formatter for functions shared with constructor formatter
-function functionLabel(fn, isCtor) {
- var type = isCtor ? 'Constructor' : 'Function',
- label = fn.name ? ': ' + fn.name : '';
- return '[' + type + label + ']';
-}
-
// most formatting determined by internal [[class]]
var formatters = {
- Boolean : String,
- Constructor : function(f){ return functionLabel(f, true); },
- Date : callbind(Date.prototype.toString),
- Error : function(e){ return '[' + errorToString(e) + ']'; },
- Function : function(f){ return functionLabel(f, false); },
- Null : String,
- Number : String,
- RegExp : callbind(RegExp.prototype.toString),
- String : quotes,
- Undefined : String
+ Boolean: String,
+ Date: function(d){
+ return '[' + Date.prototype.toString.call(d) + ']'
+ },
+ Constructor: function(f){
+ var nativeLabel = isNative(f) ? 'Native ' : '';
+ var name = f.name.length ? ': ' + f.name: '';
+ return '[' + nativeLabel + 'Constructor' + name + ']';
+ },
+ Error: function(e){
+ return '[' + Error.prototype.toString.call(e) + ']';
+ },
+ Function: function(f){
+ var nativeLabel = isNative(f) ? 'Native ' : '';
+ var name = f.name.length ? ': ' + f.name: '';
+ return '[' + nativeLabel + 'Function' + name + ']';
+ },
+ Null: String,
+ Number: String,
+ RegExp: function(r){
+ return RegExp.prototype.toString.call(r);
+ },
+ String: quotes,
+ Undefined: String
};
@@ -224,7 +244,7 @@ function quotes(s) {
return q[qWith] + s.replace(qMatch[1-qWith], '\\$1') + q[qWith];
}
-<<<<<<< HEAD
+
/**
* Echos the value of a value. Trys to print the value out
* in the best way possible given the different types.
@@ -239,8 +259,7 @@ function quotes(s) {
function inspect(obj, showHidden, depth, colors, resolveGetters) {
var settings = {
- showHidden: showHidden, // show non-enumerables
- resolveGetters: resolveGetters, // resolve getters to their value
+ showHidden: showHidden, // show non-enumerablesvalue
style: colors ? color : reflect,
seen: []
};
@@ -259,8 +278,6 @@ function inspect(obj, showHidden, depth, colors, resolveGetters) {
}
exports.inspect = inspect;
-=======
->>>>>>> Revert changes to the various isType functions. Make non-configurable properties always display as constants instead of just capslock ones. Reorganize a bit to more closely match original organization of util.js.
function formatValue(value, depth, settings) {
// Provide a hook for user-specified inspect functions.
@@ -270,7 +287,7 @@ function formatValue(value, depth, settings) {
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
- return value.inspect(depth);
+ return value.inspect(depth, settings.showHidden, settings.colors.name === 'color');
}
var base = '';
@@ -289,41 +306,49 @@ function formatValue(value, depth, settings) {
return base;
}
- // depth limit reached
- if (depth < 0) {
- return settings.style('More', 'More', true);
+ if (settings.showHidden) {
+ var properties = Object.getOwnPropertyNames(value);
+ } else {
+ var properties = Object.keys(value);
}
- if (!settings.showHidden) {
- var properties = Object.keys(value);
- } else {
- var properties = Object.getOwnPropertyNames(value);
- if (typeof value === 'function') {
- properties = properties.filter(function(key) {
- // hide useless properties every function has
- return !(key in Function);
- });
- // show prototype last for constructors
- if (type === 'Constructor') {
- properties.push('prototype');
- }
+ if (typeof value === 'function') {
+ properties = properties.filter(function(key) {
+ // hide useless properties every function has
+ return !(key in Function);
+ });
+ }
+
+ // show prototype last for constructors
+ if (type === 'Constructor') {
+ var desc = Object.getOwnPropertyDescriptor(value, 'prototype');
+ if (desc && (settings.showHidden || desc.enumerable)) {
+ properties.push('prototype');
}
}
if (properties.length === 0) {
- // no properties so return '[]' or '{}'
- if (base) {
- return base;
- }
- if (!array || value.length === 0) {
- return braces.join('');
- }
+ if (base) return base;
+ if (!array || value.length === 0) return braces.join('');
+ }
+ if (depth < 0) {
+ return (base ? base+' ' : '') + settings.style('More', 'More', true);
}
- settings.seen.push(value);
+ try {
+ if (Object.isFrozen(value)) {
+ output.push(color('Frozen', 'ObjState', true));
+ } else if (Object.isSealed(value)) {
+ output.push(color('Sealed', 'ObjState', true));
+ } else if (!Object.isExtensible(value)) {
+ output.push(color('Non-Extensible', 'ObjState', true));
+ }
+ } catch (e) {}
+
+ settings.seen.push(value);
var output = [];
// iterate array indexes first
@@ -364,48 +389,63 @@ function formatProperty(value, key, depth, settings, array) {
writable: true
};
}
-<<<<<<< HEAD
- // check for accessors but don't resolve them
-<<<<<<< HEAD
- val.set && str.push('Setter');
- val.get && str.push('Getter');
-=======
-=======
+
+ var nameFormat;
+
+ if (array && numeric.test(key)) {
+ key = '';
+ } else {
+
+ if (/^[a-zA-Z_\$][a-zA-Z0-9_\$]*$/.test(key)) {
+ // valid JavaScript name not requiring quotes
+
+ if (val.value && !val.writable) {
+ // color non-writable differently
+ nameFormat = 'Constant';
+ } else {
+ // regular name
+ nameFormat = 'Name';
+ }
+ } else {
+ // name requires quoting
+ nameFormat = 'String';
+ key = quotes(key);
+ }
+
+ if (!val.enumerable) {
+ if (settings.style.name !== 'color') {
+ // add brackets if colors are disabled
+ key = '[' + key + ']';
+ } else {
+ // use different coloring otherwise
+ nameFormat = 'H' + nameFormat;
+ }
+ }
+
+ key = settings.style(key, nameFormat) + ': ';
+ array = false;
+ }
+
// check for accessors
->>>>>>> Add tests for util.inspect
val.get && str.push('Getter');
val.set && str.push('Setter');
->>>>>>> /bad-path/
// combine Getter/Setter, or evaluate to empty for data descriptors
str = str.join('/');
if (str) {
-<<<<<<< HEAD
- // unresolved accessor
- str = settings.style('\u00AB' + str + '\u00BB', 'Accessor');
-=======
// accessor descriptor
str = settings.style(str, 'Accessor', true);
-<<<<<<< HEAD
->>>>>>> /bad-path/
-
-=======
->>>>>>> Add tests for util.inspect
} else {
// data descriptor
if (~settings.seen.indexOf(val.value)) {
// already seen
-<<<<<<< HEAD
- str = settings.style('\u00ABCircular\u00BB', 'Circular');
-=======
if (key !== 'constructor') {
str = settings.style('Circular', 'Circular', true);
} else {
// hide redundent constructor reference
return '';
}
->>>>>>> /bad-path/
} else {
// recurse to subproperties
@@ -421,40 +461,8 @@ function formatProperty(value, key, depth, settings, array) {
}
}
- // array indexes don't display their name
- if (array && numeric.test(key)) {
- return str;
- }
-
- var nameFormat;
-
- if (/^[a-zA-Z_\$][a-zA-Z0-9_\$]*$/.test(key)) {
- // valid JavaScript name not requiring quotes
-
- if (val.value && !val.writable) {
- // color non-writable differently
- nameFormat = 'Constant';
- } else {
- // regular name
- nameFormat = 'Name';
- }
- } else {
- // name requires quoting
- nameFormat = 'String';
- key = quotes(key);
- }
-
- if (!val.enumerable) {
- if (settings.style.name !== 'color') {
- // add brackets if colors are disabled
- key = '[' + key + ']';
- } else {
- // use different coloring otherwise
- nameFormat = 'H' + nameFormat;
- }
- }
- return settings.style(key, nameFormat) + ': ' + str;
+ return key + str;
}
function indent(str){
@@ -536,8 +544,6 @@ function isPrimitive(o) {
exports.isPrimitive = isPrimitive;
-<<<<<<< HEAD
-=======
// returns true if a function has properties besides `constructor` in its prototype
// and gracefully handles any input including undefined and undefined prototypes
function isConstructor(o){
@@ -549,9 +555,15 @@ function isConstructor(o){
exports.isConstructor = isConstructor;
-var pWarning;
+// Test code code indicating [native code]
+function isNative(o){
+ return typeof o === 'function' &&
+ Function.prototype.toString.call(o).slice(-17) === '{ [native code] }';
+}
+exports.isConstructor = isConstructor;
+
+
->>>>>>> Break out `isConstructor` into its own function and also export it.
exports.p = function() {
for (var i = 0, len = arguments.length; i < len; ++i) {
error(exports.inspect(arguments[i]));
Please sign in to comment.
Something went wrong with that request. Please try again.