Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Close #955 Change ^C handling in REPL
Browse files Browse the repository at this point in the history
Press with text on the line: Cancels
Press on a bare line: Print a message
Press again on a bare line: Exit
  • Loading branch information
isaacs committed Apr 21, 2011
1 parent 934c82b commit 0b3ecc0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
15 changes: 11 additions & 4 deletions doc/api/repl.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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.

22 changes: 16 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
rli.write('\n');
self.bufferedCommand = '';
self.displayPrompt();
} else {
rli.close();
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();
sawSIGINT = bareInt;
});

rli.addListener('line', function(cmd) {
sawSIGINT = false;
var skipCatchall = false;
cmd = trimWhitespace(cmd);

Expand Down

4 comments on commit 0b3ecc0

@tj
Copy link

@tj tj commented on 0b3ecc0 Apr 21, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should maybe add quit() / exit() as well even if they are not documented, myself and a few others on #node.js always seem to try quit() before .exit

@ry
Copy link

@ry ry commented on 0b3ecc0 Apr 21, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@visionmedia you type quit() by rote? in what system have your finger muscles learned to type that?

@tj
Copy link

@tj tj commented on 0b3ecc0 Apr 21, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ry spidermonkey (even though it's been forever since I've used it)

@tj
Copy link

@tj tj commented on 0b3ecc0 Apr 21, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the dot stuff too, reminds me of sqlite

Please sign in to comment.