^c to get out of '...' in REPL

This commit is contained in:
Ryan Dahl 2010-09-16 21:07:22 -07:00
Родитель 42eb5a6898
Коммит d2de8ba400
2 изменённых файлов: 25 добавлений и 5 удалений

Просмотреть файл

@ -20,10 +20,13 @@ exports.createInterface = function (output, completer) {
}; };
function Interface (output, completer) { function Interface (output, completer) {
if (!(this instanceof Interface)) return new Interface(output, completer);
EventEmitter.call(this);
this.output = output; this.output = output;
this.completer = completer; this.completer = completer;
this.setPrompt("node> "); this.setPrompt("> ");
this.enabled = output.fd < 3; // Looks like a TTY. this.enabled = output.fd < 3; // Looks like a TTY.
@ -266,7 +269,12 @@ Interface.prototype._ttyWrite = function (b) {
/* ctrl+c */ /* ctrl+c */
case 3: case 3:
//process.kill(process.pid, "SIGINT"); //process.kill(process.pid, "SIGINT");
this.close(); if (this.listeners('SIGINT').length) {
this.emit('SIGINT');
} else {
// default behavior, end the readline
this.close();
}
break; break;
case 4: // control-d, delete right or EOF case 4: // control-d, delete right or EOF

Просмотреть файл

@ -53,14 +53,26 @@ function REPLServer(prompt, stream) {
var rli = self.rli = rl.createInterface(self.stream, function (text) { var rli = self.rli = rl.createInterface(self.stream, function (text) {
return self.complete(text); return self.complete(text);
}); });
if (rli.enabled) { if (rli.enabled) {
// Turn on ANSI coloring. // Turn on ANSI coloring.
exports.writer = function(obj, showHidden, depth) { exports.writer = function(obj, showHidden, depth) {
return sys.inspect(obj, showHidden, depth, true); return sys.inspect(obj, showHidden, depth, true);
} }
} }
rli.setPrompt(self.prompt); 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) { self.stream.addListener("data", function (chunk) {
rli.write(chunk); rli.write(chunk);
}); });
@ -136,7 +148,7 @@ exports.start = function (prompt, source) {
}; };
REPLServer.prototype.displayPrompt = function () { 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(); this.rli.prompt();
}; };
@ -387,6 +399,7 @@ REPLServer.prototype.parseREPLKeyword = function (cmd) {
switch (cmd) { switch (cmd) {
case ".break": case ".break":
// TODO remove me after 0.3.x
self.buffered_cmd = ''; self.buffered_cmd = '';
self.displayPrompt(); self.displayPrompt();
return true; return true;
@ -400,7 +413,6 @@ REPLServer.prototype.parseREPLKeyword = function (cmd) {
self.stream.destroy(); self.stream.destroy();
return true; return true;
case ".help": 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(".clear\tBreak, and also clear the local context.\n");
self.stream.write(".exit\tExit the prompt\n"); self.stream.write(".exit\tExit the prompt\n");
self.stream.write(".help\tShow repl options\n"); self.stream.write(".help\tShow repl options\n");