more tput usage.

This commit is contained in:
Christopher Jeffrey 2013-02-25 01:47:32 -06:00
parent 55ba68195e
commit 68911aa357
2 changed files with 75 additions and 40 deletions

View File

@ -45,6 +45,21 @@ function Program(options) {
if (options.tput) {
this.tput = new Tput(this.terminal);
this.put = function() {
var args = Array.prototype.slice.call(arguments)
, cap = args.shift();
if (!this.tput[cap]) return;
return this.write(this.tput[cap].apply(this.tput, args));
};
Object.keys(this.tput).forEach(function(key) {
if (typeof self.tput[key] !== 'function') {
self.put[key] = self.tput[key];
return;
}
self.put[key] = function() {
return self.write(self.tput[key].apply(self.tput, arguments));
};
});
}
this.listen();
@ -691,6 +706,7 @@ Program.prototype.nul = function() {
};
Program.prototype.bell = function() {
if (this.tput) return this.put.bel();
return this.write('\x07');
};
@ -710,6 +726,7 @@ Program.prototype.backspace = function() {
Program.prototype.tab = function() {
this.x += 8;
if (this.tput) return this.put.ht(); // or `tab`
return this.write('\t');
};
@ -721,8 +738,10 @@ Program.prototype.shiftIn = function() {
return this.write('\x0f');
};
Program.prototype.cr =
Program.prototype.return = function() {
this.x = 1;
if (this.tput) return this.put.cr();
return this.write('\r');
};
@ -743,7 +762,7 @@ Program.prototype.esc = function(code) {
// ESC D Index (IND is 0x84).
Program.prototype.index = function() {
this.y++;
if (this.tput) return this.tput.ind(); // indn
if (this.tput) return this.put.ind();
return this.write('\x1bD');
};
@ -751,7 +770,7 @@ Program.prototype.index = function() {
Program.prototype.reverse =
Program.prototype.reverseIndex = function() {
this.y--;
if (this.tput) return this.tput.ri(); // rin
if (this.tput) return this.put.ri();
return this.write('\x1bM');
};
@ -759,20 +778,20 @@ Program.prototype.reverseIndex = function() {
Program.prototype.nextLine = function() {
this.y++;
this.x = 1;
if (this.tput) return this.tput.nel();
if (this.tput) return this.put.nel();
return this.write('\x1bE');
};
// ESC c Full Reset (RIS).
Program.prototype.reset = function() {
//this.x = this.y = 1;
if (this.tput) return this.tput.ris();
if (this.tput) return this.put.ris();
return this.write('\x1bc');
};
// ESC H Tab Set (HTS is 0x88).
Program.prototype.tabSet = function() {
if (this.tput) return this.tput.hts();
if (this.tput) return this.put.hts();
return this.write('\x1bH');
};
@ -780,7 +799,7 @@ Program.prototype.tabSet = function() {
Program.prototype.saveCursor = function() {
this.savedX = this.x || 1;
this.savedY = this.y || 1;
if (this.tput) return this.tput.decsc();
if (this.tput) return this.put.sc(); // not correct
return this.esc('7');
};
@ -788,7 +807,7 @@ Program.prototype.saveCursor = function() {
Program.prototype.restoreCursor = function() {
this.x = this.savedX || 1;
this.y = this.savedY || 1;
if (this.tput) return this.tput.decrc();
if (this.tput) return this.put.rc(); // not correct
return this.esc('8');
};
@ -801,7 +820,8 @@ Program.prototype.lineHeight = function() {
Program.prototype.charset = function(val, level) {
level = level || 0;
if (this.tput) return this.tput['s' + level](val);
// tput: TODO
// if (this.tput) return this.put('s' + level, val);
switch (level) {
case 0:
@ -890,6 +910,7 @@ Program.prototype.charset = function(val, level) {
// Invoke the G1 Character Set as GR (LS1R).
Program.prototype.setG = function(val) {
// tput: TODO
// if (this.tput) return this.put('s' + val);
switch (val) {
case 1:
val = '~'; // GR
@ -937,7 +958,7 @@ Program.prototype.cuu =
Program.prototype.up =
Program.prototype.cursorUp = function(param) {
this.y -= param || 1;
if (this.tput) return this.tput.cuu(param);
if (this.tput) return this.put.cuu(param);
return this.write('\x1b[' + (param || '') + 'A');
};
@ -947,7 +968,7 @@ Program.prototype.cud =
Program.prototype.down =
Program.prototype.cursorDown = function(param) {
this.y += param || 1;
if (this.tput) return this.tput.cud(param);
if (this.tput) return this.put.cud(param);
return this.write('\x1b[' + (param || '') + 'B');
};
@ -958,7 +979,7 @@ Program.prototype.right =
Program.prototype.forward =
Program.prototype.cursorForward = function(param) {
this.x += param || 1;
if (this.tput) return this.tput.cuf(param);
if (this.tput) return this.put.cuf(param);
return this.write('\x1b[' + (param || '') + 'C');
};
@ -969,7 +990,7 @@ Program.prototype.left =
Program.prototype.back =
Program.prototype.cursorBackward = function(param) {
this.x -= param || 1;
if (this.tput) return this.tput.cub(param);
if (this.tput) return this.put.cub(param);
return this.write('\x1b[' + (param || '') + 'D');
};
@ -980,7 +1001,7 @@ Program.prototype.pos =
Program.prototype.cursorPos = function(row, col) {
this.x = col || 1;
this.y = row || 1;
if (this.tput) return this.tput.cup(row, col);
if (this.tput) return this.put.cup((row || 1) - 1, (col || 1) - 1);
return this.write('\x1b[' + (row || '1') + ';' + (col || '1') + 'H');
};
@ -1009,10 +1030,10 @@ Program.prototype.eraseInDisplay = function(param) {
break;
case 'below':
default:
param = null;
param = 0;
break;
}
return this.tput.ed(param);
return this.put.ed(param);
}
switch (param) {
case 'above':
@ -1056,10 +1077,10 @@ Program.prototype.eraseInLine = function(param) {
break;
case 'right':
default:
param = null;
param = 0;
break;
}
return this.tput.el(param);
return this.put.el(param);
}
switch (param) {
case 'left':
@ -1166,6 +1187,8 @@ Program.prototype._attr = function(param, val) {
val = false;
}
// tput: TODO
switch (param) {
// attributes
case 'normal':
@ -1442,7 +1465,7 @@ Program.prototype.getCursor = function(callback) {
Program.prototype.ich =
Program.prototype.insertChars = function(param) {
this.x += param || 1;
if (this.tput) return this.tput.ich(param);
if (this.tput) return this.put.ich(param);
return this.write('\x1b[' + (param || 1) + '@');
};
@ -1470,7 +1493,7 @@ Program.prototype.cha =
Program.prototype.cursorCharAbsolute = function(param) {
this.x = param || 1;
this.y = 1;
if (this.tput) return this.tput.cha(param);
if (this.tput) return this.put.cha(param);
return this.write('\x1b[' + (param || '') + 'G');
};
@ -1478,7 +1501,7 @@ Program.prototype.cursorCharAbsolute = function(param) {
// Insert Ps Line(s) (default = 1) (IL).
Program.prototype.il =
Program.prototype.insertLines = function(param) {
if (this.tput) return this.tput.il(param);
if (this.tput) return this.put.il(param);
return this.write('\x1b[' + (param || '') + 'L');
};
@ -1486,7 +1509,7 @@ Program.prototype.insertLines = function(param) {
// Delete Ps Line(s) (default = 1) (DL).
Program.prototype.dl =
Program.prototype.deleteLines = function(param) {
if (this.tput) return this.tput.dl(param);
if (this.tput) return this.put.dl(param);
return this.write('\x1b[' + (param || '') + 'M');
};
@ -1494,7 +1517,7 @@ Program.prototype.deleteLines = function(param) {
// Delete Ps Character(s) (default = 1) (DCH).
Program.prototype.dch =
Program.prototype.deleteChars = function(param) {
if (this.tput) return this.tput.dch(param);
if (this.tput) return this.put.dch(param);
return this.write('\x1b[' + (param || '') + 'P');
};
@ -1502,7 +1525,7 @@ Program.prototype.deleteChars = function(param) {
// Erase Ps Character(s) (default = 1) (ECH).
Program.prototype.ech =
Program.prototype.eraseChars = function(param) {
if (this.tput) return this.tput.ech(param);
if (this.tput) return this.put.ech(param);
return this.write('\x1b[' + (param || '') + 'X');
};
@ -1511,7 +1534,7 @@ Program.prototype.eraseChars = function(param) {
Program.prototype.hpa =
Program.prototype.charPosAbsolute = function() {
this.x = arguments[0] || 1;
if (this.tput) return this.tput.hpa.apply(this.tput, arguments);
if (this.tput) return this.put.hpa.apply(this.put, arguments);
var param = Array.prototype.slice.call(arguments).join(';');
return this.write('\x1b[' + (param || '') + '`');
};
@ -1522,7 +1545,9 @@ Program.prototype.charPosAbsolute = function() {
Program.prototype.hpr =
Program.prototype.HPositionRelative = function(param) {
this.x += param || 1;
if (this.tput) return this.tput.hpr(param);
// Does not exist:
// if (this.tput) return this.put.hpr(param);
if (this.tput) return this.put.cuf(param);
return this.write('\x1b[' + (param || '') + 'a');
};
@ -1577,7 +1602,7 @@ Program.prototype.sendDeviceAttributes = function(param, callback) {
Program.prototype.vpa =
Program.prototype.linePosAbsolute = function() {
this.y = arguments[0] || 1;
if (this.tput) return this.tput.vpa.apply(this.tput, arguments);
if (this.tput) return this.put.vpa.apply(this.put, arguments);
var param = Array.prototype.slice.call(arguments).join(';');
return this.write('\x1b[' + (param || '') + 'd');
};
@ -1587,7 +1612,9 @@ Program.prototype.linePosAbsolute = function() {
Program.prototype.vpr =
Program.prototype.VPositionRelative = function(param) {
this.y += param || 1;
if (this.tput) return this.tput.vpr(param);
// Does not exist:
// if (this.tput) return this.put.vpr(param);
if (this.tput) return this.put.cud(param);
return this.write('\x1b[' + (param || '') + 'e');
};
@ -1598,7 +1625,9 @@ Program.prototype.hvp =
Program.prototype.HVPosition = function(row, col) {
this.y += row || 1;
this.x += col || 1;
if (this.tput) return this.tput.hvp(row, col);
// Does not exist (?):
// if (this.tput) return this.put.hvp(row, col);
if (this.tput) return this.put.cup((row || 1) - 1, (col || 1) - 1);
return this.write('\x1b[' + (row || '1') + ';' + (col || '1') + 'f');
};
@ -1937,7 +1966,7 @@ Program.prototype.setScrollRegion = function(top, bottom) {
this.scrollBottom = (bottom || this.rows) - 1;
this.x = 1;
this.y = 1;
if (this.tput) return this.tput.csr(top - 1, bottom - 1);
if (this.tput) return this.put.csr((top || 1) - 1, (bottom || 1) - 1);
return this.write('\x1b[' + (top || 1) + ';' + (bottom || this.rows) + 'r');
};
@ -1946,7 +1975,7 @@ Program.prototype.setScrollRegion = function(top, bottom) {
Program.prototype.saveCursor = function() {
this.savedX = this.x;
this.savedY = this.y;
if (this.tput) return this.tput.sc();
if (this.tput) return this.put.sc();
return this.write('\x1b[s');
};
@ -1955,7 +1984,7 @@ Program.prototype.saveCursor = function() {
Program.prototype.restoreCursor = function() {
this.x = this.savedX || 1;
this.y = this.savedY || 1;
if (this.tput) return this.tput.rc();
if (this.tput) return this.put.rc();
return this.write('\x1b[u');
};
@ -1968,7 +1997,9 @@ Program.prototype.restoreCursor = function() {
Program.prototype.cht =
Program.prototype.cursorForwardTab = function(param) {
this.x += 8;
if (this.tput) return this.tput.cht(param);
// Does not exit (?):
// if (this.tput) return this.put.cht(param);
if (this.tput) return this.put.tab(param); // or this.put.ht
return this.write('\x1b[' + (param || 1) + 'I');
};
@ -1976,7 +2007,9 @@ Program.prototype.cursorForwardTab = function(param) {
Program.prototype.su =
Program.prototype.scrollUp = function(param) {
this.y -= param || 1;
if (this.tput) return this.tput.su(param);
// Does not exit:
// if (this.tput) return this.put.su(param);
if (this.tput) return this.put.rin(param);
return this.write('\x1b[' + (param || 1) + 'I');
};
@ -1984,7 +2017,9 @@ Program.prototype.scrollUp = function(param) {
Program.prototype.sd =
Program.prototype.scrollDown = function(param) {
this.y += param || 1;
if (this.tput) return this.tput.sd(param);
// Does not exit:
// if (this.tput) return this.put.sd(param);
if (this.tput) return this.put.indn(param);
return this.write('\x1b[' + (param || 1) + 'T');
};
@ -2015,7 +2050,7 @@ Program.prototype.resetTitleModes = function() {
Program.prototype.cbt =
Program.prototype.cursorBackwardTab = function(param) {
this.x -= 8;
if (this.tput) return this.tput.cbt(param);
if (this.tput) return this.put.cbt(param);
return this.write('\x1b[' + (param || 1) + 'Z');
};
@ -2023,7 +2058,7 @@ Program.prototype.cursorBackwardTab = function(param) {
Program.prototype.rep =
Program.prototype.repeatPrecedingCharacter = function(param) {
//this.x += param || 1;
if (this.tput) return this.tput.rep(param);
if (this.tput) return this.put.rep(param);
return this.write('\x1b[' + (param || 1) + 'b');
};
@ -2035,7 +2070,7 @@ Program.prototype.repeatPrecedingCharacter = function(param) {
// http://vt100.net/annarbor/aaa-ug/section6.html
Program.prototype.tbc =
Program.prototype.tabClear = function(param) {
if (this.tput) return this.tput.tbc(param);
if (this.tput) return this.put.tbc(param);
return this.write('\x1b[' + (param || 0) + 'g');
};
@ -2052,7 +2087,7 @@ Program.prototype.tabClear = function(param) {
// Ps = 1 1 -> Print all pages.
Program.prototype.mc =
Program.prototype.mediaCopy = function() {
if (this.tput) return this.tput.mc.apply(this.tput, arguments);
// tput: TODO. See: mc0, mc5p, mc4, mc5
//if (dec) {
// this.write('\x1b[?' + Array.prototype.slice.call(arguments).join(';') + 'i');
// return;
@ -2502,7 +2537,7 @@ Program.prototype.selectiveEraseRectangle = function(params) {
Program.prototype.decrqlp =
Program.prototype.requestLocatorPosition = function(params, callback) {
// Correct for tput?
// if (this.tput) return this.tput.req_mouse_pos.apply(this.tput, arguments);
if (this.tput) return this.put.req_mouse_pos.apply(this.put, arguments);
return this.receive('\x1b[' + (param || '') + '\'|', callback);
};

View File

@ -32,7 +32,7 @@ function Tput(options) {
}
this.options = options;
this.term = options.term;
this.term = options.term || process.env.TERM;
this.data = null;
this.info = {};
this.debug = options.debug;