Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: Proposal for better repl (implementation) #9601

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 22 additions & 38 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ customizable evaluation functions.

The following special commands are supported by all REPL instances:

* `.break` - When in the process of inputting a multi-line expression, entering
the `.break` command (or pressing the `<ctrl>-C` key combination) will abort
further input or processing of that expression.
* `.clear` - Resets the REPL `context` to an empty object and clears any
multi-line expression currently being input.
* `.clear` - Resets the local REPL `context` to an empty object.
* `.exit` - Close the I/O stream, causing the REPL to exit.
* `.help` - Show this list of special commands.
* `.save` - Save the current REPL session to a file:
Expand All @@ -56,7 +52,7 @@ welcome('Node.js User');

The following key combinations in the REPL have these special effects:

* `<ctrl>-C` - When pressed once, has the same effect as the `.break` command.
* `<ctrl>-C` - When pressed once, it will break the current command.
When pressed twice on a blank line, has the same effect as the `.exit`
command.
* `<ctrl>-D` - Has the same effect as the `.exit` command.
Copy link
Member

Choose a reason for hiding this comment

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

^M and ^J should be documented.

Expand Down Expand Up @@ -178,34 +174,6 @@ function myEval(cmd, context, filename, callback) {
repl.start({prompt: '> ', eval: myEval});
```

#### Recoverable Errors
Copy link
Member

Choose a reason for hiding this comment

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

I for one am not very happy this feature got removed. In recent versions of Chrome, a similar function just got added (instead of having to type Shift+Enter, the prompt automatically recognizes line continuation), and to have to type an awkward Ctrl+J for the same feature does not sound very appealing to me.

If this change is here to stay, may I suggest we at least add support to Shift+Enter also/instead? It is the syntax in most IM apps, and also in most developer consoles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TimothyGu we discussed this option here. Shift+Enter is not possible


As a user is typing input into the REPL prompt, pressing the `<enter>` key will
send the current line of input to the `eval` function. In order to support
multi-line input, the eval function can return an instance of `repl.Recoverable`
to the provided callback function:

```js
function myEval(cmd, context, filename, callback) {
let result;
try {
result = vm.runInThisContext(cmd);
} catch (e) {
if (isRecoverableError(e)) {
return callback(new repl.Recoverable(e));
}
}
callback(null, result);
}

function isRecoverableError(error) {
if (error.name === 'SyntaxError') {
return /^(Unexpected end of input|Unexpected token)/.test(error.message);
}
return false;
}
```

### Customizing REPL Output

By default, `repl.REPLServer` instances format output using the
Expand Down Expand Up @@ -420,6 +388,18 @@ changes:
* `breakEvalOnSigint` - Stop evaluating the current piece of code when
`SIGINT` is received, i.e. `Ctrl+C` is pressed. This cannot be used together
with a custom `eval` function. Defaults to `false`.
* `executeOnTimeout` {number} If `terminal` is false,`executeOnTimeout`
delay is used to determine the end of expression. Defaults to 50ms.
`executeOnTimeout` will be coerced to `[100, 2000]` range.
* `displayWelcomeMessage` {boolean} If `true`, welcome message will be
displayed. Defaults to `false`.
```js
> node
Welcome to Node.js <<version>> (<<vm name>> VM, <<vm version>>)
Copy link
Member

Choose a reason for hiding this comment

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

The VM should be gone now.

Type ^M or enter to execute, ^J to continue, ^C to exit
Or try .help for help, more at https://nodejs.org/dist/<<version>>/docs/api/repl.html
>
```

The `repl.start()` method creates and starts a `repl.REPLServer` instance.

Expand All @@ -440,11 +420,15 @@ without passing any arguments (or by passing the `-i` argument):

```js
$ node
> const a = [1, 2, 3];
Welcome to Node.js v6.5.0 (v8 VM, 5.1.281.81)
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

Type ^M or enter to execute, ^J to continue, ^C to exit
Or try .help for help, more at https://nodejs.org/dist/v6.5.0/docs/api/repl.html

> a = [1, 2, 3]; // ^M or ⏎
[ 1, 2, 3 ]
> a.forEach((v) => {
... console.log(v);
... });
> a.forEach((v) => { // ^J to continue
console.log(v);
});
1
2
3
Expand Down
3 changes: 2 additions & 1 deletion lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,8 @@ function Interface(stdin, stdout, args) {
output: this.stdout,
eval: (code, ctx, file, cb) => this.controlEval(code, ctx, file, cb),
useGlobal: false,
ignoreUndefined: true
ignoreUndefined: true,
displayWelcomeMessage: false,
};
if (parseInt(process.env['NODE_NO_READLINE'], 10)) {
opts.terminal = false;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function createRepl(env, opts, cb) {
ignoreUndefined: false,
terminal: process.stdout.isTTY,
useGlobal: true,
breakEvalOnSigint: true
breakEvalOnSigint: true,
displayWelcomeMessage: process.execArgv.indexOf('-i') === -1,
}, opts);

if (parseInt(env.NODE_NO_READLINE)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ function emitKeypressEvents(stream, iface) {
clearTimeout(timeoutId);

if (iface) {
iface._sawKeyPress = r.length === 1;
iface._sawKeyPress = r.length === 1 || (r[0] === '\x1b');
}

for (var i = 0; i < r.length; i++) {
Expand Down
Loading