Browse Source

Close #955 Change ^C handling in REPL

Press with text on the line: Cancels
Press on a bare line: Print a message
Press again on a bare line: Exit
v0.7.4-release
isaacs 14 years ago
parent
commit
0b3ecc05a6
  1. 15
      doc/api/repl.markdown
  2. 22
      lib/repl.js

15
doc/api/repl.markdown

@ -77,9 +77,9 @@ The special variable `_` (underscore) contains the result of the last expression
> _ += 1 > _ += 1
4 4
The REPL provides access to any variables in the global scope. You can expose a variable The REPL provides access to any variables in the global scope. You can expose
to the REPL explicitly by assigning it to the `context` object associated with each a variable to the REPL explicitly by assigning it to the `context` object
`REPLServer`. For example: associated with each `REPLServer`. For example:
// repl_test.js // repl_test.js
var repl = require("repl"), var repl = require("repl"),
@ -97,7 +97,14 @@ There are a few special REPL commands:
- `.break` - While inputting a multi-line expression, sometimes you get lost - `.break` - While inputting a multi-line expression, sometimes you get lost
or just don't care about completing it. `.break` will start over. or just don't care about completing it. `.break` will start over.
- `.clear` - Resets the `context` object to an empty object and clears any multi-line expression. - `.clear` - Resets the `context` object to an empty object and clears any
multi-line expression.
- `.exit` - Close the I/O stream, which will cause the REPL to exit. - `.exit` - Close the I/O stream, which will cause the REPL to exit.
- `.help` - Show this list of special commands. - `.help` - Show this list of special commands.
The following key combinations in the REPL have these special effects:
- `<ctrl>C` - Similar to the `.break` keyword. Terminates the current
command. Press twice on a blank line to forcibly exit.
- `<ctrl>D` - Similar to the `.exit` keyword.

22
lib/repl.js

@ -101,17 +101,27 @@ function REPLServer(prompt, stream) {
rli.setPrompt(self.prompt); rli.setPrompt(self.prompt);
var sawSIGINT = false;
rli.on('SIGINT', function() { rli.on('SIGINT', function() {
if (self.bufferedCommand && self.bufferedCommand.length > 0) { if (sawSIGINT) {
rli.write('\n'); rli.close();
self.bufferedCommand = ''; process.exit();
self.displayPrompt();
} else {
rli.close();
} }
var bareInt = false;
if (!(self.bufferedCommand && self.bufferedCommand.length > 0) &&
rli.line.length === 0) {
rli.write('\n(^C again to quit)');
bareInt = true;
}
rli.line = '';
rli.write('\n');
self.bufferedCommand = '';
self.displayPrompt();
sawSIGINT = bareInt;
}); });
rli.addListener('line', function(cmd) { rli.addListener('line', function(cmd) {
sawSIGINT = false;
var skipCatchall = false; var skipCatchall = false;
cmd = trimWhitespace(cmd); cmd = trimWhitespace(cmd);

Loading…
Cancel
Save