Skip to content

Commit 0b3ecc0

Browse files
committed
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
1 parent 934c82b commit 0b3ecc0

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

doc/api/repl.markdown

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ The special variable `_` (underscore) contains the result of the last expression
7777
> _ += 1
7878
4
7979

80-
The REPL provides access to any variables in the global scope. You can expose a variable
81-
to the REPL explicitly by assigning it to the `context` object associated with each
82-
`REPLServer`. For example:
80+
The REPL provides access to any variables in the global scope. You can expose
81+
a variable to the REPL explicitly by assigning it to the `context` object
82+
associated with each `REPLServer`. For example:
8383

8484
// repl_test.js
8585
var repl = require("repl"),
@@ -97,7 +97,14 @@ There are a few special REPL commands:
9797

9898
- `.break` - While inputting a multi-line expression, sometimes you get lost
9999
or just don't care about completing it. `.break` will start over.
100-
- `.clear` - Resets the `context` object to an empty object and clears any multi-line expression.
100+
- `.clear` - Resets the `context` object to an empty object and clears any
101+
multi-line expression.
101102
- `.exit` - Close the I/O stream, which will cause the REPL to exit.
102103
- `.help` - Show this list of special commands.
103104

105+
The following key combinations in the REPL have these special effects:
106+
107+
- `<ctrl>C` - Similar to the `.break` keyword. Terminates the current
108+
command. Press twice on a blank line to forcibly exit.
109+
- `<ctrl>D` - Similar to the `.exit` keyword.
110+

lib/repl.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,27 @@ function REPLServer(prompt, stream) {
101101

102102
rli.setPrompt(self.prompt);
103103

104+
var sawSIGINT = false;
104105
rli.on('SIGINT', function() {
105-
if (self.bufferedCommand && self.bufferedCommand.length > 0) {
106-
rli.write('\n');
107-
self.bufferedCommand = '';
108-
self.displayPrompt();
109-
} else {
110-
rli.close();
106+
if (sawSIGINT) {
107+
rli.close();
108+
process.exit();
111109
}
110+
var bareInt = false;
111+
if (!(self.bufferedCommand && self.bufferedCommand.length > 0) &&
112+
rli.line.length === 0) {
113+
rli.write('\n(^C again to quit)');
114+
bareInt = true;
115+
}
116+
rli.line = '';
117+
rli.write('\n');
118+
self.bufferedCommand = '';
119+
self.displayPrompt();
120+
sawSIGINT = bareInt;
112121
});
113122

114123
rli.addListener('line', function(cmd) {
124+
sawSIGINT = false;
115125
var skipCatchall = false;
116126
cmd = trimWhitespace(cmd);
117127

0 commit comments

Comments
 (0)