diff --git a/lib/tput.js b/lib/tput.js index 88ed91b..e04f317 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -35,6 +35,7 @@ function Tput(options) { this.term = options.term || process.env.TERM; this.data = null; this.info = {}; + this.methods = null; this.termcap = null; this.debug = options.debug; @@ -57,17 +58,18 @@ function Tput(options) { Tput.prototype.fallback = function() { delete this.term; - delete this.info; delete this.data; + delete this.info; + delete this.methods; delete this.termcap; this.term = 'vt102'; - this.readTermcap(Tput._vt102); + this.readTermcap(Tput.vt102); this.compileTermcap(); }; Tput.prototype.readTerminfo = function() { - if (this.data) return; + // if (this.data) return; var file = path.resolve( '/usr/share/terminfo', @@ -508,6 +510,16 @@ Tput.prototype._compile = function(val) { return cap; } + function read_(regex, buf) { + cap = regex.exec(val); + if (!cap) return; + val = val.substring(cap[0].length); + ch = op = i = v = cap[1]; + if (!buf) clear(); + else if (typeof buf === 'string') buff += buf; + return cap; + } + function stmt(c) { if (code[code.length-1] === ',') { code = code.slice(0, -1); @@ -539,8 +551,17 @@ Tput.prototype._compile = function(val) { while (val) { // '\e' -> ^[ - if (read(/^\\e/i)) { - print('\\x1b'); + // if (read(/^\\e/i)) { + // print('\\x1b'); + // continue; + // } + // optimized: + // if (read_(/^\\e/i, '\x1b')) { + // continue; + // } + if (/^\\e/i.test(val)) { + val = val.substring(2); + buff += '\x1b'; continue; } @@ -959,13 +980,14 @@ Tput.prototype._parsePadding = function(code, print, done) { */ Tput.prototype.readTermcap = function(data) { - if (this.termcap) return; + // if (this.termcap) return; this.termcap = {}; this.termcap.data = data || process.env.TERMCAP - || fs.readFileSync('/etc/termcap', 'ascii'); + || tryRead('/etc/termcap') + || Tput.vt102; this.termcap.terms = this.parseTermcap(this.termcap.data); this.termcap.info = this.termcap.terms[this.term]; @@ -1068,6 +1090,10 @@ Tput.prototype.translateTermcap = function() { throw new Error('Terminal not found.'); } + ['name', 'names', 'desc'].forEach(function(key) { + out[key] = info[key]; + }); + // Separate aliases for termcap var talias = (function() { var alias = Tput.alias @@ -1099,48 +1125,6 @@ Tput.prototype.compileTermcap = function() { this.compile(); }; -/*Tput.prototype.compileTermcap = function() { - var self = this - , info = this.termcap.info; - - if (!info) { - throw new Error('Terminal not found.'); - } - - info.all = {}; - - ['bools', 'numbers', 'strings'].forEach(function(key) { - Object.keys(info[key]).forEach(function(cap) { - info.all[cap] = self._compile(info[key][cap]); - }); - }); - - Object.keys(Tput.alias).forEach(function(ti) { - var a = Tput.alias[ti] - , tia = a[0] - , tc = a.termcap || a[1]; - - if (info.all[tc]) { - if (!a.termcap) { - a = a.slice(); - a.splice(1, 1); - } - info.all[ti] = info.all[tc]; - a.forEach(function(k) { - if (!info.all[k]) { - info.all[k] = info.all[tc]; - } - }); - } - }); - - Object.keys(info.all).forEach(function(key) { - self[key] = info.all[key]; - }); - - return info.all; -};*/ - /** * Helpers */ @@ -1162,6 +1146,15 @@ function write(data) { return process.stdout.write(data); } +function tryRead() { + var file = path.resolve.apply(path, arguments); + try { + return fs.readFileSync(file, 'utf8'); + } catch (e) { + return ''; + } +} + /** * sprintf * http://www.cplusplus.com/reference/cstdio/printf/ diff --git a/test/tput.js b/test/tput.js index 119bb46..a2873fe 100644 --- a/test/tput.js +++ b/test/tput.js @@ -1,5 +1,16 @@ var Tput = require('../').Tput; +if (~process.argv.indexOf('--termcap')) { + var tput = Tput({ + term: 'vt102', + debug: true, + termcap: true + }); + console.log(tput.info); + console.log(tput.termcap); + process.exit(0); +} + var tput = Tput({ term: process.argv[2] || 'xterm', extended: true,