zero indexed program to mirror tput.

This commit is contained in:
Christopher Jeffrey 2013-07-12 00:00:11 -05:00
parent d452b72dd8
commit cc863af2cf
1 changed files with 106 additions and 43 deletions

View File

@ -35,12 +35,17 @@ function Program(options) {
this.input = options.input || process.stdin; this.input = options.input || process.stdin;
this.output = options.output || process.stdout; this.output = options.output || process.stdout;
this.x = 1; this.zero = options.zero;
this.y = 1;
this.x = 0;
this.y = 0;
this.cols = this.output.columns || 1; this.cols = this.output.columns || 1;
this.rows = this.output.rows || 1; this.rows = this.output.rows || 1;
this.scrollTop = 0;
this.scrollBottom = this.rows - 1;
this.terminal = options.terminal || process.env.TERM || 'xterm'; this.terminal = options.terminal || process.env.TERM || 'xterm';
if (options.tput) { if (options.tput) {
@ -49,14 +54,6 @@ function Program(options) {
this.listen(); this.listen();
if (options.zero) {
this.ti = 0;
this.ni = 1;
} else {
this.ti = -1;
this.ni = 0;
}
//if (!Program.global) { //if (!Program.global) {
// Program._write = process.stdout.write; // Program._write = process.stdout.write;
// process.stdout.write = function() {}; // process.stdout.write = function() {};
@ -711,6 +708,13 @@ Program.prototype.echo = function(text, attr) {
: this.output.write(text); : this.output.write(text);
}; };
Program.prototype._ncoords = function() {
if (this.x < 0) this.x = 0;
else if (this.x >= this.cols) this.x = this.cols - 1;
if (this.y < 0) this.y = 0;
else if (this.y >= this.rows) this.y = this.rows - 1;
};
Program.prototype.setx = function(x) { Program.prototype.setx = function(x) {
return this.cursorCharAbsolute(x); return this.cursorCharAbsolute(x);
// return this.charPosAbsolute(x); // return this.charPosAbsolute(x);
@ -772,6 +776,7 @@ Program.prototype.bell = function() {
Program.prototype.vtab = function() { Program.prototype.vtab = function() {
this.y++; this.y++;
this._ncoords();
return this.write('\x0b'); return this.write('\x0b');
}; };
@ -782,6 +787,7 @@ Program.prototype.form = function() {
Program.prototype.kbs = Program.prototype.kbs =
Program.prototype.backspace = function() { Program.prototype.backspace = function() {
this.x--; this.x--;
this._ncoords();
if (this.tput) return this.put.kbs(); if (this.tput) return this.put.kbs();
return this.write('\x08'); return this.write('\x08');
}; };
@ -789,6 +795,7 @@ Program.prototype.backspace = function() {
Program.prototype.ht = Program.prototype.ht =
Program.prototype.tab = function() { Program.prototype.tab = function() {
this.x += 8; this.x += 8;
this._ncoords();
if (this.tput) return this.put.ht(); // or `tab` if (this.tput) return this.put.ht(); // or `tab`
return this.write('\t'); return this.write('\t');
}; };
@ -803,7 +810,7 @@ Program.prototype.shiftIn = function() {
Program.prototype.cr = Program.prototype.cr =
Program.prototype.return = function() { Program.prototype.return = function() {
this.x = 1; this.x = 0;
if (this.tput) return this.put.cr(); if (this.tput) return this.put.cr();
return this.write('\r'); return this.write('\r');
}; };
@ -811,8 +818,9 @@ Program.prototype.return = function() {
Program.prototype.nel = Program.prototype.nel =
Program.prototype.newline = Program.prototype.newline =
Program.prototype.feed = function() { Program.prototype.feed = function() {
this.x = 1; this.x = 0;
this.y++; this.y++;
this._ncoords();
if (this.tput) return this.put.nel(); if (this.tput) return this.put.nel();
return this.write('\n'); return this.write('\n');
}; };
@ -829,6 +837,7 @@ Program.prototype.esc = function(code) {
Program.prototype.ind = Program.prototype.ind =
Program.prototype.index = function() { Program.prototype.index = function() {
this.y++; this.y++;
this._ncoords();
if (this.tput) return this.put.ind(); if (this.tput) return this.put.ind();
return this.write('\x1bD'); return this.write('\x1bD');
}; };
@ -838,6 +847,7 @@ Program.prototype.ri =
Program.prototype.reverse = Program.prototype.reverse =
Program.prototype.reverseIndex = function() { Program.prototype.reverseIndex = function() {
this.y--; this.y--;
this._ncoords();
if (this.tput) return this.put.ri(); if (this.tput) return this.put.ri();
return this.write('\x1bM'); return this.write('\x1bM');
}; };
@ -845,14 +855,15 @@ Program.prototype.reverseIndex = function() {
// ESC E Next Line (NEL is 0x85). // ESC E Next Line (NEL is 0x85).
Program.prototype.nextLine = function() { Program.prototype.nextLine = function() {
this.y++; this.y++;
this.x = 1; this.x = 0;
this._ncoords();
if (this.tput) return this.put.nel(); if (this.tput) return this.put.nel();
return this.write('\x1bE'); return this.write('\x1bE');
}; };
// ESC c Full Reset (RIS). // ESC c Full Reset (RIS).
Program.prototype.reset = function() { Program.prototype.reset = function() {
//this.x = this.y = 1; //this.x = this.y = 0;
if (this.tput) return this.put.rs1 ? this.put.rs1() : this.put.ris(); if (this.tput) return this.put.rs1 ? this.put.rs1() : this.put.ris();
return this.write('\x1bc'); return this.write('\x1bc');
}; };
@ -866,8 +877,8 @@ Program.prototype.tabSet = function() {
// ESC 7 Save Cursor (DECSC). // ESC 7 Save Cursor (DECSC).
Program.prototype.sc = Program.prototype.sc =
Program.prototype.saveCursor = function() { Program.prototype.saveCursor = function() {
this.savedX = this.x || 1; this.savedX = this.x || 0;
this.savedY = this.y || 1; this.savedY = this.y || 0;
if (this.tput) return this.put.sc(); if (this.tput) return this.put.sc();
return this.esc('7'); return this.esc('7');
}; };
@ -875,8 +886,8 @@ Program.prototype.saveCursor = function() {
// ESC 8 Restore Cursor (DECRC). // ESC 8 Restore Cursor (DECRC).
Program.prototype.rc = Program.prototype.rc =
Program.prototype.restoreCursor = function() { Program.prototype.restoreCursor = function() {
this.x = this.savedX || 1; this.x = this.savedX || 0;
this.y = this.savedY || 1; this.y = this.savedY || 0;
if (this.tput) return this.put.rc(); if (this.tput) return this.put.rc();
return this.esc('8'); return this.esc('8');
}; };
@ -1089,6 +1100,7 @@ Program.prototype.cuu =
Program.prototype.up = Program.prototype.up =
Program.prototype.cursorUp = function(param) { Program.prototype.cursorUp = function(param) {
this.y -= param || 1; this.y -= param || 1;
this._ncoords();
if (this.tput) return this.put.cuu(param); if (this.tput) return this.put.cuu(param);
return this.write('\x1b[' + (param || '') + 'A'); return this.write('\x1b[' + (param || '') + 'A');
}; };
@ -1099,6 +1111,7 @@ Program.prototype.cud =
Program.prototype.down = Program.prototype.down =
Program.prototype.cursorDown = function(param) { Program.prototype.cursorDown = function(param) {
this.y += param || 1; this.y += param || 1;
this._ncoords();
if (this.tput) return this.put.cud(param); if (this.tput) return this.put.cud(param);
return this.write('\x1b[' + (param || '') + 'B'); return this.write('\x1b[' + (param || '') + 'B');
}; };
@ -1110,6 +1123,7 @@ Program.prototype.right =
Program.prototype.forward = Program.prototype.forward =
Program.prototype.cursorForward = function(param) { Program.prototype.cursorForward = function(param) {
this.x += param || 1; this.x += param || 1;
this._ncoords();
if (this.tput) return this.put.cuf(param); if (this.tput) return this.put.cuf(param);
return this.write('\x1b[' + (param || '') + 'C'); return this.write('\x1b[' + (param || '') + 'C');
}; };
@ -1121,6 +1135,7 @@ Program.prototype.left =
Program.prototype.back = Program.prototype.back =
Program.prototype.cursorBackward = function(param) { Program.prototype.cursorBackward = function(param) {
this.x -= param || 1; this.x -= param || 1;
this._ncoords();
if (this.tput) return this.put.cub(param); if (this.tput) return this.put.cub(param);
return this.write('\x1b[' + (param || '') + 'D'); return this.write('\x1b[' + (param || '') + 'D');
}; };
@ -1130,10 +1145,18 @@ Program.prototype.cursorBackward = function(param) {
Program.prototype.cup = Program.prototype.cup =
Program.prototype.pos = Program.prototype.pos =
Program.prototype.cursorPos = function(row, col) { Program.prototype.cursorPos = function(row, col) {
this.x = col || 1; if (!this.zero) {
this.y = row || 1; row = (row || 1) - 1;
if (this.tput) return this.put.cup((row || 1) - 1, (col || 1) - 1); col = (col || 1) - 1;
return this.write('\x1b[' + (row || '1') + ';' + (col || '1') + 'H'); } else {
row = row || 0;
col = col || 0;
}
this.x = col;
this.y = row;
this._ncoords();
if (this.tput) return this.put.cup(row, col);
return this.write('\x1b[' + (row + 1) + ';' + (col + 1) + 'H');
}; };
// CSI Ps J Erase in Display (ED). // CSI Ps J Erase in Display (ED).
@ -1604,6 +1627,7 @@ Program.prototype.getCursor = function(callback) {
Program.prototype.ich = Program.prototype.ich =
Program.prototype.insertChars = function(param) { Program.prototype.insertChars = function(param) {
this.x += param || 1; this.x += param || 1;
this._ncoords();
if (this.tput) return this.put.ich(param); if (this.tput) return this.put.ich(param);
return this.write('\x1b[' + (param || 1) + '@'); return this.write('\x1b[' + (param || 1) + '@');
}; };
@ -1614,6 +1638,7 @@ Program.prototype.insertChars = function(param) {
Program.prototype.cnl = Program.prototype.cnl =
Program.prototype.cursorNextLine = function(param) { Program.prototype.cursorNextLine = function(param) {
this.y += param || 1; this.y += param || 1;
this._ncoords();
return this.write('\x1b[' + (param || '') + 'E'); return this.write('\x1b[' + (param || '') + 'E');
}; };
@ -1623,6 +1648,7 @@ Program.prototype.cursorNextLine = function(param) {
Program.prototype.cpl = Program.prototype.cpl =
Program.prototype.cursorPrecedingLine = function(param) { Program.prototype.cursorPrecedingLine = function(param) {
this.y -= param || 1; this.y -= param || 1;
this._ncoords();
return this.write('\x1b[' + (param || '') + 'F'); return this.write('\x1b[' + (param || '') + 'F');
}; };
@ -1630,10 +1656,16 @@ Program.prototype.cursorPrecedingLine = function(param) {
// Cursor Character Absolute [column] (default = [row,1]) (CHA). // Cursor Character Absolute [column] (default = [row,1]) (CHA).
Program.prototype.cha = Program.prototype.cha =
Program.prototype.cursorCharAbsolute = function(param) { Program.prototype.cursorCharAbsolute = function(param) {
this.x = param || 1; if (!this.zero) {
this.y = 1; param = (param || 1) - 1;
} else {
param = param || 0;
}
this.x = param;
this.y = 0;
this._ncoords();
if (this.tput) return this.put.cha(param); if (this.tput) return this.put.cha(param);
return this.write('\x1b[' + (param || '') + 'G'); return this.write('\x1b[' + (param + 1) + 'G');
}; };
// CSI Ps L // CSI Ps L
@ -1670,10 +1702,14 @@ Program.prototype.eraseChars = function(param) {
// CSI Pm ` Character Position Absolute // CSI Pm ` Character Position Absolute
// [column] (default = [row,1]) (HPA). // [column] (default = [row,1]) (HPA).
// NOTE: Can't find in terminfo, no idea why it has multiple params.
Program.prototype.hpa = Program.prototype.hpa =
Program.prototype.charPosAbsolute = function() { Program.prototype.charPosAbsolute = function(param) {
this.x = arguments[0] || 1; this.x = param || 0;
if (this.tput) return this.put.hpa.apply(this.put, arguments); this._ncoords();
if (this.tput) {
return this.put.hpa.apply(this.put, arguments);
}
var param = Array.prototype.slice.call(arguments).join(';'); var param = Array.prototype.slice.call(arguments).join(';');
return this.write('\x1b[' + (param || '') + '`'); return this.write('\x1b[' + (param || '') + '`');
}; };
@ -1684,6 +1720,7 @@ Program.prototype.charPosAbsolute = function() {
Program.prototype.hpr = Program.prototype.hpr =
Program.prototype.HPositionRelative = function(param) { Program.prototype.HPositionRelative = function(param) {
this.x += param || 1; this.x += param || 1;
this._ncoords();
// Does not exist: // Does not exist:
// if (this.tput) return this.put.hpr(param); // if (this.tput) return this.put.hpr(param);
if (this.tput) return this.put.cuf(param); if (this.tput) return this.put.cuf(param);
@ -1738,10 +1775,14 @@ Program.prototype.sendDeviceAttributes = function(param, callback) {
// CSI Pm d // CSI Pm d
// Line Position Absolute [row] (default = [1,column]) (VPA). // Line Position Absolute [row] (default = [1,column]) (VPA).
// NOTE: Can't find in terminfo, no idea why it has multiple params.
Program.prototype.vpa = Program.prototype.vpa =
Program.prototype.linePosAbsolute = function() { Program.prototype.linePosAbsolute = function(param) {
this.y = arguments[0] || 1; this.y = param || 1;
if (this.tput) return this.put.vpa.apply(this.put, arguments); this._ncoords();
if (this.tput) {
return this.put.vpa.apply(this.put, arguments);
}
var param = Array.prototype.slice.call(arguments).join(';'); var param = Array.prototype.slice.call(arguments).join(';');
return this.write('\x1b[' + (param || '') + 'd'); return this.write('\x1b[' + (param || '') + 'd');
}; };
@ -1751,6 +1792,7 @@ Program.prototype.linePosAbsolute = function() {
Program.prototype.vpr = Program.prototype.vpr =
Program.prototype.VPositionRelative = function(param) { Program.prototype.VPositionRelative = function(param) {
this.y += param || 1; this.y += param || 1;
this._ncoords();
// Does not exist: // Does not exist:
// if (this.tput) return this.put.vpr(param); // if (this.tput) return this.put.vpr(param);
if (this.tput) return this.put.cud(param); if (this.tput) return this.put.cud(param);
@ -1762,12 +1804,20 @@ Program.prototype.VPositionRelative = function(param) {
// [1,1]) (HVP). // [1,1]) (HVP).
Program.prototype.hvp = Program.prototype.hvp =
Program.prototype.HVPosition = function(row, col) { Program.prototype.HVPosition = function(row, col) {
this.y += row || 1; if (!this.zero) {
this.x += col || 1; row = (row || 1) - 1;
col = (col || 1) - 1;
} else {
row = row || 0;
col = col || 0;
}
this.y = row;
this.x = col;
this._ncoords();
// Does not exist (?): // Does not exist (?):
// if (this.tput) return this.put.hvp(row, col); // if (this.tput) return this.put.hvp(row, col);
if (this.tput) return this.put.cup((row || 1) - 1, (col || 1) - 1); if (this.tput) return this.put.cup(row, col);
return this.write('\x1b[' + (row || '1') + ';' + (col || '1') + 'f'); return this.write('\x1b[' + (row + 1) + ';' + (col + 1) + 'f');
}; };
// CSI Pm h Set Mode (SM). // CSI Pm h Set Mode (SM).
@ -2145,12 +2195,20 @@ Program.prototype.setMouse = function(opt, enable) {
Program.prototype.decstbm = Program.prototype.decstbm =
Program.prototype.csr = Program.prototype.csr =
Program.prototype.setScrollRegion = function(top, bottom) { Program.prototype.setScrollRegion = function(top, bottom) {
this.scrollTop = (top || 1) - 1; if (!this.zero) {
this.scrollBottom = (bottom || this.rows) - 1; top = (top || 1) - 1;
this.x = 1; bottom = (bottom || this.rows) - 1;
this.y = 1; } else {
if (this.tput) return this.put.csr((top || 1) - 1, (bottom || 1) - 1); top = top || 0;
return this.write('\x1b[' + (top || 1) + ';' + (bottom || this.rows) + 'r'); bottom = bottom || (this.rows - 1);
}
this.scrollTop = top;
this.scrollBottom = bottom;
this.x = 0;
this.y = 0;
this._ncoords();
if (this.tput) return this.put.csr(top, bottom);
return this.write('\x1b[' + (top + 1) + ';' + (bottom + 1) + 'r');
}; };
// CSI s // CSI s
@ -2167,8 +2225,8 @@ Program.prototype.saveCursorA = function() {
// Restore cursor (ANSI.SYS). // Restore cursor (ANSI.SYS).
Program.prototype.rcA = Program.prototype.rcA =
Program.prototype.restoreCursorA = function() { Program.prototype.restoreCursorA = function() {
this.x = this.savedX || 1; this.x = this.savedX || 0;
this.y = this.savedY || 1; this.y = this.savedY || 0;
if (this.tput) return this.put.rc(); if (this.tput) return this.put.rc();
return this.write('\x1b[u'); return this.write('\x1b[u');
}; };
@ -2182,6 +2240,7 @@ Program.prototype.restoreCursorA = function() {
Program.prototype.cht = Program.prototype.cht =
Program.prototype.cursorForwardTab = function(param) { Program.prototype.cursorForwardTab = function(param) {
this.x += 8; this.x += 8;
this._ncoords();
// Does not exit (?): // Does not exit (?):
// if (this.tput) return this.put.cht(param); // if (this.tput) return this.put.cht(param);
if (this.tput) return this.put.tab(param); // or this.put.ht if (this.tput) return this.put.tab(param); // or this.put.ht
@ -2192,6 +2251,7 @@ Program.prototype.cursorForwardTab = function(param) {
Program.prototype.su = Program.prototype.su =
Program.prototype.scrollUp = function(param) { Program.prototype.scrollUp = function(param) {
this.y -= param || 1; this.y -= param || 1;
this._ncoords();
// Does not exit: // Does not exit:
// if (this.tput) return this.put.su(param); // if (this.tput) return this.put.su(param);
if (this.tput) return this.put.rin(param); if (this.tput) return this.put.rin(param);
@ -2202,6 +2262,7 @@ Program.prototype.scrollUp = function(param) {
Program.prototype.sd = Program.prototype.sd =
Program.prototype.scrollDown = function(param) { Program.prototype.scrollDown = function(param) {
this.y += param || 1; this.y += param || 1;
this._ncoords();
// Does not exit: // Does not exit:
// if (this.tput) return this.put.sd(param); // if (this.tput) return this.put.sd(param);
if (this.tput) return this.put.indn(param); if (this.tput) return this.put.indn(param);
@ -2235,6 +2296,7 @@ Program.prototype.resetTitleModes = function() {
Program.prototype.cbt = Program.prototype.cbt =
Program.prototype.cursorBackwardTab = function(param) { Program.prototype.cursorBackwardTab = function(param) {
this.x -= 8; this.x -= 8;
this._ncoords();
if (this.tput) return this.put.cbt(param); if (this.tput) return this.put.cbt(param);
return this.write('\x1b[' + (param || 1) + 'Z'); return this.write('\x1b[' + (param || 1) + 'Z');
}; };
@ -2243,6 +2305,7 @@ Program.prototype.cursorBackwardTab = function(param) {
Program.prototype.rep = Program.prototype.rep =
Program.prototype.repeatPrecedingCharacter = function(param) { Program.prototype.repeatPrecedingCharacter = function(param) {
//this.x += param || 1; //this.x += param || 1;
//this._ncoords();
if (this.tput) return this.put.rep(param); if (this.tput) return this.put.rep(param);
return this.write('\x1b[' + (param || 1) + 'b'); return this.write('\x1b[' + (param || 1) + 'b');
}; };