-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Changes from 5 commits
48414f0
b34c110
a60da69
d6cee38
f304f8c
2d5feff
a3047dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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. | ||
|
@@ -178,34 +174,6 @@ function myEval(cmd, context, filename, callback) { | |
repl.start({prompt: '> ', eval: myEval}); | ||
``` | ||
|
||
#### Recoverable Errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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>>) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
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. | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.