refactor.

This commit is contained in:
Christopher Jeffrey 2013-02-19 18:35:26 -06:00
parent 40d89449f6
commit 270a5af7e8
2 changed files with 24 additions and 19 deletions

View File

@ -1,11 +1,12 @@
var Tput = require('../lib/tput');
var tput = new Tput(process.argv[2] || 'xterm');
tput.colors();
var tput = new Tput(process.argv[2] || 'xterm', true);
console.log(tput.info);
//tput.colors();
tput.compile();
//console.log(tput.info);
//tput.compile();
//tput._compile('%?%p9%t\u001b(0%e\u001b(B%;\u001b[0%?%p6%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m');

View File

@ -21,7 +21,7 @@ var assert = require('assert')
* Tput
*/
function Tput(term) {
function Tput(term, debug) {
if (!(this instanceof Tput)) {
return new Tput(term);
}
@ -29,6 +29,10 @@ function Tput(term) {
this.term = term;
this.data = null;
this.info = {};
this.debug = debug;
this.readTermInfo();
this.compile();
}
Tput.prototype.readTermInfo = function() {
@ -257,6 +261,10 @@ Tput.prototype.compile = function(key) {
var self = this
, info = this.info;
if (this.debug) {
console.log(this.info);
}
this.methods = {};
this.info.all = {};
@ -273,7 +281,9 @@ Tput.prototype.compile = function(key) {
});
Object.keys(info.all).forEach(function(key) {
console.log('Compiling ' + key + ': ' + JSON.stringify(info.all[key]));
if (self.debug) {
console.log('Compiling ' + key + ': ' + JSON.stringify(info.all[key]));
}
self.methods[key] = self._compile(info.all[key]);
var alias = self.alias(key);
self.methods[alias] = self.methods[key];
@ -282,6 +292,10 @@ Tput.prototype.compile = function(key) {
Tput.bools.concat(Tput.numbers).concat(Tput.strings).forEach(function(key) {
if (!self.methods[key]) self.methods[key] = noop;
});
Object.keys(self.methods).forEach(function(key) {
self[key] = self.methods[key];
});
};
Tput.prototype._compile = function(val) {
@ -720,7 +734,9 @@ Tput.prototype._compile = function(val) {
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += ';return out.join("");';
console.log(code.replace(/\x1b/g, '\\x1b'));
if (this.debug) {
console.log(code.replace(/\x1b/g, '\\x1b'));
}
try {
return new Function('params', code);
@ -753,9 +769,6 @@ Tput.prototype.setupAliases = function(info) {
});
};
Tput.prototype.colors = function() {
};
Tput.bools = [
'auto_left_margin',
'auto_right_margin',
@ -1268,15 +1281,6 @@ Tput.strings = [
'box_chars_1'
];
Object.keys(Tput.prototype).forEach(function(key) {
if (key === 'readTermInfo') return;
var method = Tput.prototype[key];
Tput.prototype[key] = function() {
this.readTermInfo();
return method.apply(this, arguments);
};
});
/**
* Helpers
*/