Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Use strings instead of buffers in readline
Browse files Browse the repository at this point in the history
For eventual large character support.
  • Loading branch information
ry committed Jun 8, 2010
1 parent a6942b3 commit 0377b12
Showing 1 changed file with 43 additions and 63 deletions.
106 changes: 43 additions & 63 deletions lib/readline.js
Expand Up @@ -25,8 +25,7 @@ function Interface (output, isTTY) {
this.setPrompt("node> ");

// Current line
this.buf = new Buffer(kBufSize);
this.buf.used = 0;
this.line = ""

if (!isTTY) {
this._tty = false;
Expand Down Expand Up @@ -57,7 +56,6 @@ Interface.prototype.setPrompt = function (prompt, length) {

Interface.prototype.prompt = function () {
if (this._tty) {
this.buf.used = 0;
this.cursor = 0;
this._refreshLine();
} else {
Expand All @@ -67,21 +65,18 @@ Interface.prototype.prompt = function () {


Interface.prototype._addHistory = function () {
if (this.buf.used === 0) return "";
if (this.line.length === 0) return "";

var b = new Buffer(this.buf.used);
this.buf.copy(b, 0, 0, this.buf.used);
this.buf.used = 0;

this.history.unshift(b);
this.history.unshift(this.line);
this.line = "";
this.historyIndex = -1;

this.cursor = 0;

// Only store so many
if (this.history.length > kHistorySize) this.history.pop();

return b.toString('utf8');
return this.history[0];
};


Expand All @@ -95,9 +90,7 @@ Interface.prototype._refreshLine = function () {

// Write the prompt and the current buffer content.
this.output.write(this._prompt);
if (this.buf.used > 0) {
this.output.write(this.buf.slice(0, this.buf.used));
}
this.output.write(this.line);

// Erase to right.
this.output.write('\x1b[0K');
Expand All @@ -123,42 +116,31 @@ Interface.prototype.write = function (d) {


Interface.prototype._normalWrite = function (b) {
for (var i = 0; i < b.length; i++) {
var code = b instanceof Buffer ? b[i] : b.charCodeAt(i);
if (code === '\n'.charCodeAt(0) || code === '\r'.charCodeAt(0)) {
var s = this.buf.toString('utf8', 0, this.buf.used);
this.emit('line', s);
this.buf.used = 0;
} else {
this.buf[this.buf.used++] = code;
}
}
// Very simple implementation right now. Should try to break on
// new lines.
this.emit('line', b.toString());
};

Interface.prototype._historyNext = function () {
if (this.historyIndex > 0) {
this.historyIndex--;
this.history[this.historyIndex].copy(this.buf, 0);
this.buf.used = this.history[this.historyIndex].length;
// set cursor to end of line.
this.cursor = this.buf.used;
this.line = this.history[this.historyIndex];
this.cursor = this.line.length; // set cursor to end of line.
this._refreshLine();

} else if (this.historyIndex === 0) {
this.historyIndex = -1;
this.cursor = 0;
this.buf.used = 0;
this.line = '';
this._refreshLine();
}
};

Interface.prototype._historyPrev = function () {
if (this.historyIndex + 1 < this.history.length) {
this.historyIndex++;
this.history[this.historyIndex].copy(this.buf, 0);
this.buf.used = this.history[this.historyIndex].length;
// set cursor to end of line.
this.cursor = this.buf.used;
this.line = this.history[this.historyIndex];
this.cursor = this.line.length; // set cursor to end of line.

this._refreshLine();
}
Expand All @@ -174,13 +156,12 @@ Interface.prototype._ttyWrite = function (b) {
break;

case 4: // control-d, delete right or EOF
if (this.cursor === 0 && this.buf.used === 0) {
if (this.cursor === 0 && this.line.length === 0) {
this.close();
} else if (this.cursor < this.buf.used) {
for (var i = this.cursor; i < this.buf.used; i++) {
this.buf[i] = this.buf[i+1];
}
this.buf.used--;
} else if (this.cursor < this.line.length) {
this.line = this.line.slice(0, this.cursor)
+ this.line.slice(this.cursor+1, this.line.length)
;
this._refreshLine();
}
break;
Expand All @@ -194,23 +175,23 @@ Interface.prototype._ttyWrite = function (b) {

case 127: /* backspace */
case 8: /* ctrl+h */
if (this.cursor > 0 && this.buf.used > 0) {
for (var i = this.cursor; i < this.buf.used; i++) {
this.buf[i-1] = this.buf[i];
}
if (this.cursor > 0 && this.line.length > 0) {
this.line = this.line.slice(0, this.cursor-1)
+ this.line.slice(this.cursor, this.line.length)
;
this.cursor--;
this.buf.used--;
this._refreshLine();
}
break;

case 21: /* Ctrl+u, delete the whole line. */
this.cursor = this.buf.used = 0;
this.cursor = 0;
this.line = '';
this._refreshLine();
break;

case 11: /* Ctrl+k, delete from current to end of line. */
this.buf.used = this.cursor;
this.line = this.line.slice(0, this.cursor);
this._refreshLine();
break;

Expand All @@ -220,7 +201,7 @@ Interface.prototype._ttyWrite = function (b) {
break;

case 5: /* ctrl+e, go to the end of the line */
this.cursor = this.buf.used;
this.cursor = this.line.length;
this._refreshLine();
break;

Expand All @@ -232,7 +213,7 @@ Interface.prototype._ttyWrite = function (b) {
break;

case 6: // control-f, forward one character
if (this.cursor != this.buf.used) {
if (this.cursor != this.line.length) {
this.cursor++;
this._refreshLine();
}
Expand All @@ -249,17 +230,17 @@ Interface.prototype._ttyWrite = function (b) {
case 27: /* escape sequence */
if (b[1] === 98 && this.cursor > 0) { // meta-b - backward word

} else if (b[1] === 102 && this.cursor < this.buf.used) { // meta-f - forward word
} else if (b[1] === 102 && this.cursor < this.line.length) { // meta-f - forward word

} else if (b[1] === 91 && b[2] === 68) { // left arrow
if (this.cursor > 0) {
this.cursor--;
this._refreshLine();
this.output.write('\x1b[0D');
}
} else if (b[1] === 91 && b[2] === 67) { // right arrow
if (this.cursor != this.buf.used) {
if (this.cursor != this.line.length) {
this.cursor++;
this._refreshLine();
this.output.write('\x1b[0C');
}
} else if (b[1] === 91 && b[2] === 65) { // up arrow
this._historyPrev();
Expand All @@ -269,18 +250,17 @@ Interface.prototype._ttyWrite = function (b) {
break;

default:
if (this.buf.used < kBufSize) {
for (var i = this.buf.used + 1; this.cursor < i; i--) {
this.buf[i] = this.buf[i-1];
}
this.buf[this.cursor++] = b[0];
this.buf.used++;

if (this.buf.used == this.cursor) {
this.output.write(b);
} else {
this._refreshLine();
}
var c = b.toString('utf8');
if (this.cursor < this.line.length) {
var beg = this.line.slice(0, this.cursor);
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);
}
break;
}
Expand Down

0 comments on commit 0377b12

Please sign in to comment.