Skip to content

Commit

Permalink
^c to get out of '...' in REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 17, 2010
1 parent 42eb5a6 commit d2de8ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ exports.createInterface = function (output, completer) {
};

function Interface (output, completer) {
if (!(this instanceof Interface)) return new Interface(output, completer);
EventEmitter.call(this);

this.output = output;
this.completer = completer;

this.setPrompt("node> ");
this.setPrompt("> ");

this.enabled = output.fd < 3; // Looks like a TTY.

Expand Down Expand Up @@ -266,7 +269,12 @@ Interface.prototype._ttyWrite = function (b) {
/* ctrl+c */
case 3:
//process.kill(process.pid, "SIGINT");
this.close();
if (this.listeners('SIGINT').length) {
this.emit('SIGINT');
} else {
// default behavior, end the readline
this.close();
}
break;

case 4: // control-d, delete right or EOF
Expand Down
18 changes: 15 additions & 3 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,26 @@ function REPLServer(prompt, stream) {
var rli = self.rli = rl.createInterface(self.stream, function (text) {
return self.complete(text);
});

if (rli.enabled) {
// Turn on ANSI coloring.
exports.writer = function(obj, showHidden, depth) {
return sys.inspect(obj, showHidden, depth, true);
}
}

rli.setPrompt(self.prompt);

rli.on("SIGINT", function () {
if (self.buffered_cmd && self.buffered_cmd.length > 0) {
rli.write("\n");
self.buffered_cmd = '';
self.displayPrompt();
} else {
rli.close();
}
});

self.stream.addListener("data", function (chunk) {
rli.write(chunk);
});
Expand Down Expand Up @@ -136,7 +148,7 @@ exports.start = function (prompt, source) {
};

REPLServer.prototype.displayPrompt = function () {
this.rli.setPrompt(this.buffered_cmd.length ? '... ' : this.prompt);
this.rli.setPrompt(this.buffered_cmd.length ? '... ' : this.prompt);
this.rli.prompt();
};

Expand Down Expand Up @@ -386,7 +398,8 @@ REPLServer.prototype.parseREPLKeyword = function (cmd) {
var self = this;

switch (cmd) {
case ".break":
case ".break":
// TODO remove me after 0.3.x
self.buffered_cmd = '';
self.displayPrompt();
return true;
Expand All @@ -400,7 +413,6 @@ REPLServer.prototype.parseREPLKeyword = function (cmd) {
self.stream.destroy();
return true;
case ".help":
self.stream.write(".break\tSometimes you get stuck in a place you can't get out... This will get you out.\n");
self.stream.write(".clear\tBreak, and also clear the local context.\n");
self.stream.write(".exit\tExit the prompt\n");
self.stream.write(".help\tShow repl options\n");
Expand Down

0 comments on commit d2de8ba

Please sign in to comment.