From 910abec478591b51bb55832c877508124d4a364e Mon Sep 17 00:00:00 2001 From: Stephen Steneker Date: Thu, 16 Oct 2014 12:06:30 +1100 Subject: [PATCH] Fix #13: "edit" command polluted with ASCII color codes - disable tojson() colorize if the caller context is empty (assuming the originating function call comes from C++, eg "edit var") --- hacks/color.js | 12 ++++++++---- hacks/common.js | 29 ++++++++++++++++++----------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/hacks/color.js b/hacks/color.js index 8a9801c..e4cd18a 100644 --- a/hacks/color.js +++ b/hacks/color.js @@ -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]; @@ -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 ); }; diff --git a/hacks/common.js b/hacks/common.js index 54f7c11..a4d16da 100644 --- a/hacks/common.js +++ b/hacks/common.js @@ -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; @@ -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++; @@ -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) @@ -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 ); }