better dumps.

This commit is contained in:
Christopher Jeffrey 2013-07-29 19:00:31 -05:00
parent 81267dbd2a
commit b7e7a8264b

View File

@ -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() {