From 270a5af7e85ffb7632e5ae5e7c2c7c0edd77817a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 19 Feb 2013 18:35:26 -0600 Subject: [PATCH] refactor. --- example/tput.js | 9 +++++---- lib/tput.js | 34 +++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/example/tput.js b/example/tput.js index 4af72e5..8e13253 100644 --- a/example/tput.js +++ b/example/tput.js @@ -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'); diff --git a/lib/tput.js b/lib/tput.js index ecc8156..d60c307 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -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 */