From b7e7a8264bb8d986c6d21e0252a9a1347b18081c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 29 Jul 2013 19:00:31 -0500 Subject: [PATCH] better dumps. --- lib/program.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/lib/program.js b/lib/program.js index 82cadf3..0ccb3e0 100644 --- a/lib/program.js +++ b/lib/program.js @@ -76,17 +76,94 @@ function Program(options) { Program.prototype.__proto__ = EventEmitter.prototype; -Program.prototype.dump = -Program.prototype.log = function(file) { +Program.prototype.dump = function(file) { var self = this - , _write = this.output.write; + , _write = this.output.write + , decoder = new (require('string_decoder')).StringDecoder('utf8') + , stringify = stringifyLess; this.logger = require('fs').createWriteStream(file); + function stringifyJson(data) { + return JSON.stringify(caret(data)) + .replace(/\\u00([0-9a-f])/gi, '\\x$1') + .slice(1, -1); + } + + function stringifyLess(data) { + // Could wrap in: \x1b[7m\x1b[m + // for inverse in `less -R`. + return caret(data + .replace(/\r/g, '\\r') + .replace(/\n/g, '\\n') + .replace(/\t/g, '\\t')) + .replace(/[^ -~]/g, function(ch) { + if (ch.charCodeAt(0) > 0xff) return ch; + ch = ch.charCodeAt(0).toString(16); + if (ch.length > 2) { + if (ch.length < 4) ch = '0' + ch; + return '\\u' + ch; + } + if (ch.length < 2) ch = '0' + ch; + return '\\x' + ch; + }); + } + + function stringifyMinimal(data) { + return data + //.replace(/\x1b/g, '^[') + .replace(/\r/g, '\\r') + .replace(/\n/g, '\\n') + .replace(/\t/g, '\\t'); + } + + function caret(data) { + return data.replace(/[\0\x80\x1b-\x1f\x7f\x01-\x1a]/g, function(ch) { + switch (ch) { + case '\0': + case '\200': + ch = '@'; + break; + case '\x1b': + ch = '['; + break; + case '\x1c': + ch = '\\'; + break; + case '\x1d': + ch = ']'; + break; + case '\x1e': + ch = '^'; + break; + case '\x1f': + ch = '_'; + break; + case '\x7f': + ch = '?'; + break; + default: + ch = ch.charCodeAt(0); + if (ch >= 1 && ch <= 26) { + //if (ch >= 'A'.charCodeAt(0) - 64 && ch <= 'Z'.charCodeAt(0) - 64) { + ch = String.fromCharCode(ch + 64); + } else { + return String.fromCharCode(ch); + } + break; + } + return '^' + ch; + }); + } + this.output.write = function(data) { - self.logger.write(data); + self.logger.write('OUT: ' + stringify(data) + '\n-\n'); return _write.apply(this, arguments); }; + + this.input.on('data', function(data) { + self.logger.write('IN: ' + stringify(decoder.write(data)) + '\n-\n'); + }); }; Program.prototype.setupTput = function() {