Browse files

readline: row-agnostic multiline readline implementation

Attempting to rewrite 9fc84ad to not
depend on the terminal's "rows", since that should not be necessary at all.

Attempting to fix #2959.
  • Loading branch information...
1 parent 9fc84ad commit 54fba0af859bd640cc12f86600ec4ad8b2048209 @TooTallNate committed Mar 18, 2012
Showing with 42 additions and 9 deletions.
  1. +42 −9 lib/readline.js
View
51 lib/readline.js
@@ -179,18 +179,51 @@ Interface.prototype._addHistory = function() {
Interface.prototype._refreshLine = function() {
+
+ var columns = this.columns;
+
+ // line length
+ var line = this._prompt + this.line;
+ var lineLength = line.length;
+ var lineCols = lineLength % columns;
+ var lineRows = (lineLength - lineCols) / columns;
+
+ // cursor position
+ var cursorPos = this._promptLength + this.cursor;
+ var cols = cursorPos % columns;
+ var rows = (cursorPos - cols) / columns;
+
+ // first move to the bottom of the current line, based on cursor pos
+ var prevRows = this.prevRows || 0;
+ if (prevRows > 0) {
+ this.output.moveCursor(0, -prevRows);
+ }
+
// Cursor to left edge.
this.output.cursorTo(0);
+ // erase data
+ this.output.write('\x1b[0J');
// Write the prompt and the current buffer content.
- this.output.write(this._prompt);
- this.output.write(this.line);
-
- // Erase to right.
- this.output.clearLine(1);
+ var l = 0;
+ do {
+ var li = line.substring(l, l + columns);
+ if (li.length === columns) {
+ li += '\r\n';
+ }
+ this.output.write(li);
+ l += columns;
+ } while (l < lineLength);
// Move cursor to original position.
- this.output.cursorTo(this._promptLength + this.cursor);
+ this.output.cursorTo(cols);
+
+ var diff = lineRows - rows;
+ if (diff > 0) {
+ this.output.moveCursor(0, -diff);
+ }
+
+ this.prevRows = rows;
};
@@ -237,12 +270,12 @@ Interface.prototype._insertString = function(c) {
var end = this.line.slice(this.cursor, this.line.length);
this.line = beg + c + end;
this.cursor += c.length;
- this._refreshLine();
} else {
this.line += c;
this.cursor += c.length;
this.output.write(c);
}
+ this._refreshLine();
};
Interface.prototype._tabComplete = function() {
@@ -620,14 +653,14 @@ Interface.prototype._ttyWrite = function(s, key) {
case 'left':
if (this.cursor > 0) {
this.cursor--;
- this.output.moveCursor(-1, 0);
+ this._refreshLine();
}
break;
case 'right':
if (this.cursor != this.line.length) {
this.cursor++;
- this.output.moveCursor(1, 0);
+ this._refreshLine();
}
break;

0 comments on commit 54fba0a

Please sign in to comment.