add termcap parser.

This commit is contained in:
Christopher Jeffrey 2013-02-22 15:51:13 -06:00
parent a784ca6a2b
commit c477f71dd2
3 changed files with 164 additions and 1 deletions

View File

@ -12,3 +12,6 @@ var tput = new Tput(process.argv[2] || 'xterm', true);
//console.log(tput.methods.set_attributes([34]) + 'foo' + tput.methods.set_attributes([0]));
//console.log(tput.set_attributes(34) + 'foo' + tput.set_attributes(0));
//tput.readTermCap();
//console.log(tput.termcap.terms);

View File

@ -8,7 +8,8 @@
* Modules
*/
var EventEmitter = require('events').EventEmitter;
var EventEmitter = require('events').EventEmitter
, Tput = require('./tput');
/**
* Program
@ -33,6 +34,7 @@ function Program(input, output) {
this.rows = this.output.rows || 1;
this.terminal = process.env.TERM || 'xterm';
this.tput = new Tput(this.terminal);
this.listen();
@ -2442,4 +2444,6 @@ Program.prototype.deleteColumns = function() {
exports = Program;
exports.Program = Program;
exports.Tput = Tput;
module.exports = exports;

View File

@ -740,6 +740,162 @@ Tput.prototype.setupAliases = function(info) {
});
};
/**
* Termcap
*/
Tput.prototype.readTermCap = function() {
if (this.termcap) return;
this.termcap = {};
if (process.env.TERMCAP) {
this.termcap.data = process.env.TERMCAP;
} else {
var file = path.resolve('/etc/termcap');
this.termcap.data = fs.readFileSync(file, 'utf8');
}
this.termcap.terms = this.parseTermCap(this.termcap.data);
this.termcap.info = this.termcap.terms[this.term];
return this.termcap;
};
/**
* Termcap Parser
* http://en.wikipedia.org/wiki/Termcap
* http://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html
* http://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html#SEC17
* http://tldp.org/HOWTO/Text-Terminal-HOWTO.html#toc16
* man termcap
*/
// Example:
// vt102|dec vt102:\
// :do=^J:co#80:li#24:cl=50\E[;H\E[2J:\
// :le=^H:bs:cm=5\E[%i%d;%dH:nd=2\E[C:up=2\E[A:\
// :ce=3\E[K:cd=50\E[J:so=2\E[7m:se=2\E[m:us=2\E[4m:ue=2\E[m:\
// :md=2\E[1m:mr=2\E[7m:mb=2\E[5m:me=2\E[m:is=\E[1;24r\E[24;1H:\
// :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:ks=\E[?1h\E=:ke=\E[?1l\E>:\
// :ku=\EOA:kd=\EOB:kr=\EOC:kl=\EOD:kb=^H:\
// :ho=\E[H:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:pt:sr=5\EM:vt#3:\
// :sc=\E7:rc=\E8:cs=\E[%i%d;%dr:vs=\E[?7l:ve=\E[?7h:\
// :mi:al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:
Tput.prototype.parseTermCap = function(data) {
var terms = {}
, term
, entries
, fields
, field
, names
, i
, j;
// remove escaped newlines
data = data.replace(/\\\n[ \t]*/g, '');
// remove comments
data = data.replace(/^#[^\n]+/gm, '');
// split entries
entries = data.split(/\n+/);
for (i = 0; i < entries.length; i++) {
fields = entries[i].split(/:+/);
for (j = 0; j < fields.length; j++) {
field = fields[j].trim();
if (!field) continue;
if (j === 0) {
names = field.split('|');
term = terms[names[0]] = {};
term.header = {
name: names[0],
names: names,
desc: names.pop()
};
term.bools = {};
term.numbers = {};
term.strings = {};
continue;
}
if (~field.indexOf('=')) {
parts = field.split('=');
term.strings[parts[0]] = parts.slice(1).join('=');
} else if (~field.indexOf('#')) {
parts = field.split('#');
term.numbers[parts[0]] = +parts.slice(1).join('#');
} else {
term.bools[field] = true;
}
}
}
return terms;
};
Tput.prototype.parseTermCap_ = function(data) {
var terms = {}
, term;
// remove escaped newlines
data = data.replace(/\\\n\s*/g, '');
// remove comments
data = data.replace(/^#[^\n]+/gm, '');
// could split by line instead
data = data.replace(/\s+/g, '');
var fields = data.split(/:+/)
, l = fields.length
, i = 0
, field
, names;
for (; i < l; i++) {
field = fields[i].trim();
if (!field) continue;
names = field.split('|');
if (names.length > 1) {
term = terms[names[0]] = {};
term.header = {
desc: names.pop(),
names: names
};
term.bools = {};
term.numbers = {};
term.strings = {};
continue;
}
if (~field.indexOf('=')) {
parts = field.split('=');
term.strings[parts[0]] = parts.slice(1).join('=');
} else if (~field.indexOf('#')) {
parts = field.split('#');
term.numbers[parts[0]] = +parts.slice(1).join('#');
} else {
term.bools[field] = true;
}
}
};
/**
* Termcap Compiler
* man termcap
*/
Tput.prototype.compileTermcap = function() {
};
Tput.prototype._compileTermcap = function() {
};
/**
* Helpers
*/