Skip to content

Commit

Permalink
Fix #13: "edit" command polluted with ASCII color codes
Browse files Browse the repository at this point in the history
 - disable tojson() colorize if the caller context is empty (assuming
   the originating function call comes from C++, eg "edit var")
  • Loading branch information
stennie committed Oct 16, 2014
1 parent 87c9f93 commit 910abec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
12 changes: 8 additions & 4 deletions hacks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ function controlCode( parameters ) {
return __ansi.csi + String(parameters) + String(__ansi.text_prop);
};

function applyColorCode( string, properties ) {
return __colorize ? controlCode(properties) + String(string) + controlCode() : String(string);
function applyColorCode( string, properties, nocolor ) {
// Allow global __colorize default to be overriden
var applyColor = (null == nocolor) ? __colorize : !nocolor;

return applyColor ? controlCode(properties) + String(string) + controlCode() : String(string);
};

function colorize( string, color ) {
function colorize( string, color, nocolor ) {

var params = [];
var code = __ansi.foreground + __ansi.colors[color.color];

Expand All @@ -43,5 +47,5 @@ function colorize( string, color ) {
if ( color.bright === true ) params.push(__ansi.bright);
if ( color.underline === true ) params.push(__ansi.underline);

return applyColorCode( string, params );
return applyColorCode( string, params, nocolor );
};
29 changes: 18 additions & 11 deletions hacks/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ DBQuery.prototype.shellPrint = function(){
}
};

tojsonObject = function( x, indent, nolint ) {
tojsonObject = function( x, indent, nolint, nocolor ) {

var lineEnding = nolint ? " " : "\n";
var tabSpace = nolint ? "" : __indent;

Expand Down Expand Up @@ -196,7 +197,7 @@ tojsonObject = function( x, indent, nolint ) {
continue;

var color = mongo_hacker_config.colors.key;
s += indent + colorize("\"" + key + "\"", color) + ": " + tojson( val, indent , nolint );
s += indent + colorize("\"" + key + "\"", color, nocolor) + ": " + tojson( val, indent , nolint, nocolor );
if (num != total) {
s += ",";
num++;
Expand All @@ -210,16 +211,22 @@ tojsonObject = function( x, indent, nolint ) {
};


tojson = function( x, indent , nolint ) {
tojson = function( x, indent , nolint, nocolor ) {

if (null == tojson.caller) {
// Unknonwn caller context, so assume this is from C++ code
nocolor = true;
}

if ( x === null )
return colorize("null", mongo_hacker_config.colors['null']);
return colorize("null", mongo_hacker_config.colors['null'], nocolor);

if ( x === undefined )
return colorize("undefined", mongo_hacker_config.colors['undefined']);
return colorize("undefined", mongo_hacker_config.colors['undefined'], nocolor);

if ( x.isObjectId ) {
var color = mongo_hacker_config.colors['objectid'];
return surround('ObjectId', colorize('"' + x.str + '"', color));
return surround('ObjectId', colorize('"' + x.str + '"', color, nocolor));
}

if (!indent)
Expand Down Expand Up @@ -250,21 +257,21 @@ tojson = function( x, indent , nolint ) {
}
}
s += "\"";
return colorize(s, mongo_hacker_config.colors.string);
return colorize(s, mongo_hacker_config.colors.string, nocolor);
}
case "number":
return colorize(x, mongo_hacker_config.colors.number);
return colorize(x, mongo_hacker_config.colors.number, nocolor);
case "boolean":
return colorize("" + x, mongo_hacker_config.colors['boolean']);
return colorize("" + x, mongo_hacker_config.colors['boolean'], nocolor);
case "object": {
s = tojsonObject( x, indent , nolint );
s = tojsonObject( x, indent , nolint, nocolor );
if ( ( nolint === null || nolint === true ) && s.length < 80 && ( indent === null || indent.length === 0 ) ){
s = s.replace( /[\s\r\n ]+/gm , " " );
}
return s;
}
case "function":
return colorize(x.toString(), mongo_hacker_config.colors['function']);
return colorize(x.toString(), mongo_hacker_config.colors['function'], nocolor);
default:
throw "tojson can't handle type " + ( typeof x );
}
Expand Down

0 comments on commit 910abec

Please sign in to comment.