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
This commit is contained in:
parent
934c82b8b8
commit
0b3ecc05a6
@ -77,9 +77,9 @@ The special variable `_` (underscore) contains the result of the last expression
|
||||
> _ += 1
|
||||
4
|
||||
|
||||
The REPL provides access to any variables in the global scope. You can expose a variable
|
||||
to the REPL explicitly by assigning it to the `context` object associated with each
|
||||
`REPLServer`. For example:
|
||||
The REPL provides access to any variables in the global scope. You can expose
|
||||
a variable to the REPL explicitly by assigning it to the `context` object
|
||||
associated with each `REPLServer`. For example:
|
||||
|
||||
// repl_test.js
|
||||
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
|
||||
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.
|
||||
- `.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.
|
||||
|
||||
|
18
lib/repl.js
18
lib/repl.js
@ -101,17 +101,27 @@ function REPLServer(prompt, stream) {
|
||||
|
||||
rli.setPrompt(self.prompt);
|
||||
|
||||
var sawSIGINT = false;
|
||||
rli.on('SIGINT', function() {
|
||||
if (self.bufferedCommand && self.bufferedCommand.length > 0) {
|
||||
if (sawSIGINT) {
|
||||
rli.close();
|
||||
process.exit();
|
||||
}
|
||||
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();
|
||||
} else {
|
||||
rli.close();
|
||||
}
|
||||
sawSIGINT = bareInt;
|
||||
});
|
||||
|
||||
rli.addListener('line', function(cmd) {
|
||||
sawSIGINT = false;
|
||||
var skipCatchall = false;
|
||||
cmd = trimWhitespace(cmd);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user