Browse Source

Revert "readline: add multiline support"

This reverts commit 443071db57.

Patch was overly compilicated and made some incorrect assumptions about the
position of the cursor being at the bottom of the screen. @rlidwka and I are
working on getting a proper implementation written.
v0.9.1-release
Nathan Rajlich 13 years ago
parent
commit
8517089b3e
  1. 86
      lib/readline.js

86
lib/readline.js

@ -95,18 +95,13 @@ function Interface(input, output, completer) {
this.history = []; this.history = [];
this.historyIndex = -1; this.historyIndex = -1;
// a number of lines used by current command
this.usedLines = 1;
var winSize = output.getWindowSize(); var winSize = output.getWindowSize();
exports.columns = winSize[0]; exports.columns = winSize[0];
exports.rows = winSize[1];
if (process.listeners('SIGWINCH').length === 0) { if (process.listeners('SIGWINCH').length === 0) {
process.on('SIGWINCH', function() { process.on('SIGWINCH', function() {
var winSize = output.getWindowSize(); var winSize = output.getWindowSize();
exports.columns = winSize[0]; exports.columns = winSize[0];
exports.rows = winSize[1];
}); });
} }
} }
@ -118,10 +113,6 @@ Interface.prototype.__defineGetter__('columns', function() {
return exports.columns; return exports.columns;
}); });
Interface.prototype.__defineGetter__('rows', function() {
return exports.rows;
});
Interface.prototype.setPrompt = function(prompt, length) { Interface.prototype.setPrompt = function(prompt, length) {
this._prompt = prompt; this._prompt = prompt;
if (length) { if (length) {
@ -187,53 +178,19 @@ Interface.prototype._addHistory = function() {
}; };
Interface.prototype._recalcUsedLines = function() {
var line = this._prompt + this.line;
var newcount = Math.ceil(line.length / this.columns);
if (newcount > this.usedLines) this.usedLines = newcount;
};
Interface.prototype._refreshLine = function() { Interface.prototype._refreshLine = function() {
var columns = this.columns;
if (!columns) columns = Infinity;
// See if a number of used lines has changed
var oldLines = this.usedLines;
this._recalcUsedLines();
if (oldLines != this.usedLines) {
this.output.cursorTo(0, this.rows - 1);
for (var i = oldLines; i < this.usedLines; i++) {
this.output.write('\r\n');
}
}
// Cursor to left edge. // Cursor to left edge.
if (this.usedLines === 1) { this.output.cursorTo(0);
this.output.cursorTo(0);
} else {
this.output.cursorTo(0, this.rows - this.usedLines);
}
// Write the prompt and the current buffer content. // Write the prompt and the current buffer content.
var buffer = this._prompt + this.line; this.output.write(this._prompt);
this.output.write(buffer); this.output.write(this.line);
// Erase to right. // Erase to right.
this.output.clearLine(1); this.output.clearLine(1);
var clearLinesCnt = this.usedLines - Math.floor(buffer.length / columns) - 1;
for (var i = this.rows - clearLinesCnt; i < this.rows; i++) {
this.output.cursorTo(0, i);
this.output.clearLine(0);
}
// Move cursor to original position. // Move cursor to original position.
var curPos = this._getCursorPos(); this.output.cursorTo(this._promptLength + this.cursor);
if (this.usedLines === 1) {
this.output.cursorTo(curPos[0]);
} else {
this.output.cursorTo(curPos[0], this.rows - this.usedLines + curPos[1]);
}
}; };
@ -285,7 +242,6 @@ Interface.prototype._insertString = function(c) {
this.line += c; this.line += c;
this.cursor += c.length; this.cursor += c.length;
this.output.write(c); this.output.write(c);
this._recalcUsedLines();
} }
}; };
@ -459,7 +415,6 @@ Interface.prototype._deleteLineRight = function() {
Interface.prototype._line = function() { Interface.prototype._line = function() {
var line = this._addHistory(); var line = this._addHistory();
this.output.write('\r\n'); this.output.write('\r\n');
this.usedLines = 1;
this._onLine(line); this._onLine(line);
}; };
@ -491,33 +446,6 @@ Interface.prototype._historyPrev = function() {
}; };
// Returns current cursor's position and line
Interface.prototype._getCursorPos = function() {
var columns = this.columns;
var pos = this.cursor + this._promptLength;
if (!columns) return [pos, 0];
var lineNum = Math.floor(pos / columns);
var colNum = pos - lineNum * columns;
return [colNum, lineNum];
};
Interface.prototype._moveCursor = function(dx) {
var oldcursor = this.cursor;
var oldLine = this._getCursorPos()[1];
this.cursor += dx;
var newLine = this._getCursorPos()[1];
// check if cursors are in the same line
if (oldLine == newLine) {
this.output.moveCursor(dx, 0);
} else {
this._refreshLine();
}
};
// handle a write from the tty // handle a write from the tty
Interface.prototype._ttyWrite = function(s, key) { Interface.prototype._ttyWrite = function(s, key) {
var next_word, next_non_word, previous_word, previous_non_word; var next_word, next_non_word, previous_word, previous_non_word;
@ -691,13 +619,15 @@ Interface.prototype._ttyWrite = function(s, key) {
case 'left': case 'left':
if (this.cursor > 0) { if (this.cursor > 0) {
this._moveCursor(-1); this.cursor--;
this.output.moveCursor(-1, 0);
} }
break; break;
case 'right': case 'right':
if (this.cursor != this.line.length) { if (this.cursor != this.line.length) {
this._moveCursor(1); this.cursor++;
this.output.moveCursor(1, 0);
} }
break; break;

Loading…
Cancel
Save