Browse Source

Support more readline navigation keys.

v0.7.4-release
Matt Ranney 15 years ago
committed by Ryan Dahl
parent
commit
09af242c89
  1. 105
      lib/readline.js
  2. 3
      lib/repl.js

105
lib/readline.js

@ -135,7 +135,36 @@ Interface.prototype._normalWrite = function (b) {
} }
}; };
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._refreshLine();
} else if (this.historyIndex === 0) {
this.historyIndex = -1;
this.cursor = 0;
this.buf.used = 0;
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._refreshLine();
}
};
// handle a write from the tty
Interface.prototype._ttyWrite = function (b) { Interface.prototype._ttyWrite = function (b) {
switch (b[0]) { switch (b[0]) {
/* ctrl+c */ /* ctrl+c */
@ -144,8 +173,16 @@ Interface.prototype._ttyWrite = function (b) {
this.close(); this.close();
break; break;
case 4: /* ctrl+d */ case 4: // control-d, delete right or EOF
this.close(); if (this.cursor === 0 && this.buf.used === 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--;
this._refreshLine();
}
break; break;
case 13: /* enter */ case 13: /* enter */
@ -187,47 +224,47 @@ Interface.prototype._ttyWrite = function (b) {
this._refreshLine(); this._refreshLine();
break; break;
case 2: // control-b, back one character
if (this.cursor > 0) {
this.cursor--;
this._refreshLine();
}
break;
case 6: // control-f, forward one character
if (this.cursor != this.buf.used) {
this.cursor++;
this._refreshLine();
}
break;
case 14: // control-n, next history item
this._historyNext();
break;
case 16: // control-p, previous history item
this._historyPrev();
break;
case 27: /* escape sequence */ case 27: /* escape sequence */
if (b[1] === 91 && b[2] === 68) { if (b[1] === 98 && this.cursor > 0) { // meta-b - backward word
// left arrow
} else if (b[1] === 102 && this.cursor < this.buf.used) { // meta-f - forward word
} else if (b[1] === 91 && b[2] === 68) { // left arrow
if (this.cursor > 0) { if (this.cursor > 0) {
this.cursor--; this.cursor--;
this._refreshLine(); this._refreshLine();
} }
} else if (b[1] === 91 && b[2] === 67) { } else if (b[1] === 91 && b[2] === 67) { // right arrow
// right arrow
if (this.cursor != this.buf.used) { if (this.cursor != this.buf.used) {
this.cursor++; this.cursor++;
this._refreshLine(); this._refreshLine();
} }
} else if (b[1] === 91 && b[2] === 65) { } else if (b[1] === 91 && b[2] === 65) { // up arrow
// up arrow this._historyPrev();
if (this.historyIndex + 1 < this.history.length) { } else if (b[1] === 91 && b[2] === 66) { // down arrow
this.historyIndex++; this._historyNext();
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._refreshLine();
}
} else if (b[1] === 91 && b[2] === 66) {
// down arrow
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._refreshLine();
} else if (this.historyIndex === 0) {
this.historyIndex = -1;
this.cursor = 0;
this.buf.used = 0;
this._refreshLine();
}
} }
break; break;

3
lib/repl.js

@ -68,8 +68,7 @@ function REPLServer(prompt, stream) {
// This try is for determining if the command is complete, or should // This try is for determining if the command is complete, or should
// continue onto the next line. // continue onto the next line.
try { try {
// Scope the readline with self.scope // Use evalcx to supply the global scope
// with(){} and eval() are considered bad.
var ret = evalcx(self.buffered_cmd, scope, "repl"); var ret = evalcx(self.buffered_cmd, scope, "repl");
if (ret !== undefined) { if (ret !== undefined) {
scope._ = ret; scope._ = ret;

Loading…
Cancel
Save