From 9db97cd61c162347efada68d5755a13a1d75eba5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 14 Jul 2013 08:37:57 -0500 Subject: [PATCH] easier feature checking. --- lib/program.js | 32 +++++++++++++++++++++----------- lib/tput.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/program.js b/lib/program.js index b7a9d86..aa61aa9 100644 --- a/lib/program.js +++ b/lib/program.js @@ -118,6 +118,12 @@ Program.prototype.setupTput = function() { }); }; +Program.prototype.has = function(name) { + return this.tput + ? this.tput.has(name) + : false; +}; + Program.prototype.term = function(is) { return this.terminal.indexOf(is) === 0; }; @@ -953,13 +959,13 @@ Program.prototype.repeat = function(ch, i) { //Program.prototype.pad = Program.prototype.nul = function() { - //if (this.tput) return this.put.pad(); + //if (this.has('pad')) return this.put.pad(); return this.write('\0'); }; Program.prototype.bel = Program.prototype.bell = function() { - if (this.tput) return this.put.bel(); + if (this.has('bel')) return this.put.bel(); return this.write('\x07'); }; @@ -971,7 +977,7 @@ Program.prototype.vtab = function() { Program.prototype.ff = Program.prototype.form = function() { - if (this.tput) return this.put.ff(); + if (this.has('ff')) return this.put.ff(); return this.write('\x0c'); }; @@ -979,7 +985,7 @@ Program.prototype.kbs = Program.prototype.backspace = function() { this.x--; this._ncoords(); - if (this.tput) return this.put.kbs(); + if (this.has('kbs')) return this.put.kbs(); return this.write('\x08'); }; @@ -987,24 +993,24 @@ Program.prototype.ht = Program.prototype.tab = function() { this.x += 8; this._ncoords(); - if (this.tput) return this.put.ht(); + if (this.has('ht')) return this.put.ht(); return this.write('\t'); }; Program.prototype.shiftOut = function() { - // if (this.tput) return this.put.S2(); + // if (this.has('S2')) return this.put.S2(); return this.write('\x0e'); }; Program.prototype.shiftIn = function() { - // if (this.tput) return this.put.S3(); + // if (this.has('S3')) return this.put.S3(); return this.write('\x0f'); }; Program.prototype.cr = Program.prototype.return = function() { this.x = 0; - if (this.tput) return this.put.cr(); + if (this.has('cr')) return this.put.cr(); return this.write('\r'); }; @@ -1014,7 +1020,7 @@ Program.prototype.feed = function() { this.x = 0; this.y++; this._ncoords(); - if (this.tput) return this.put.nel(); + if (this.has('nel')) return this.put.nel(); return this.write('\n'); }; @@ -1050,14 +1056,18 @@ Program.prototype.nextLine = function() { this.y++; this.x = 0; this._ncoords(); - if (this.tput) return this.put.nel(); + if (this.has('nel')) return this.put.nel(); return this.write('\x1bE'); }; // ESC c Full Reset (RIS). Program.prototype.reset = function() { this.x = this.y = 0; - if (this.tput) return this.put.rs1 ? this.put.rs1() : this.put.ris(); + if (this.has('rs1') || this.has('ris')) { + return this.has('rs1') + ? this.put.rs1() + : this.put.ris(); + } return this.write('\x1bc'); }; diff --git a/lib/tput.js b/lib/tput.js index bfe2640..2c3f16c 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -1581,6 +1581,35 @@ assert.equal(Tput.alias.exit_delete_mode.termcap, 'ed'); // Tput.alias.lines.ignore = 0; // Tput.alias.set_lr_margin.ignore = 1; +/** + * Feature Checking + */ + +Tput.aliasMap = {}; + +Object.keys(Tput.alias).forEach(function(key) { + var aliases = Array.isArray(Tput.alias[key]) + ? Tput.alias[key] + : [Tput.alias[key]]; + Tput.aliasMap[key] = key; + aliases.forEach(function(k) { + Tput.aliasMap[k] = key; + }); +}); + +Tput.prototype.has = function(name) { + var name = Tput.aliasMap[name] + , val = this.all[name]; + + if (!name) return false; + + if (typeof val === 'number') { + return val !== -1; + } + + return !!val; +}; + /** * Fallback Termcap Entry */