-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
doc: repl: add defineComand and displayPrompt #3765
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,10 +138,6 @@ and try to print `obj` in REPL, it will invoke the custom `inspect()` function: | |
> obj | ||
{ bar: 'baz' } | ||
|
||
[Readline Interface]: readline.html#readline_class_interface | ||
[util.inspect()]: util.html#util_util_inspect_object_options | ||
[here]: util.html#util_custom_inspect_function_on_objects | ||
|
||
## repl.start(options) | ||
|
||
Returns and starts a `REPLServer` instance, that inherits from | ||
|
@@ -244,6 +240,10 @@ a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310 | |
For an example of running a REPL instance over `curl(1)`, | ||
see: https://gist.github.com/2053342 | ||
|
||
## Class: REPLServer | ||
|
||
This inherits from [Readline Interface][] with the following events: | ||
|
||
### Event: 'exit' | ||
|
||
`function () {}` | ||
|
@@ -254,7 +254,7 @@ to signal "end" on the `input` stream. | |
|
||
Example of listening for `exit`: | ||
|
||
r.on('exit', function () { | ||
replServer.on('exit', function () { | ||
console.log('Got "exit" event from repl!'); | ||
process.exit(); | ||
}); | ||
|
@@ -271,11 +271,59 @@ be emitted. | |
Example of listening for `reset`: | ||
|
||
// Extend the initial repl context. | ||
var r = repl.start({ options ... }); | ||
var replServer = repl.start({ options ... }); | ||
someExtension.extend(r.context); | ||
|
||
// When a new context is created extend it as well. | ||
r.on('reset', function (context) { | ||
replServer.on('reset', function (context) { | ||
console.log('repl has a new context'); | ||
someExtension.extend(context); | ||
}); | ||
|
||
### replServer.defineCommand(keyword, cmd) | ||
|
||
* `keyword` {String} | ||
* `cmd` {Object|Function} | ||
|
||
Makes a command available in the REPL. The command is invoked by typing a `.` | ||
followed by the keyword. The `cmd` is an object with the following values: | ||
|
||
- `help` - help text to be displayed when `.help` is entered (Optional). | ||
- `action` - a function to execute, potentially taking in a string argument, | ||
when the command is invoked, bound to the REPLServer instance (Required). | ||
|
||
If a function is provided instead of an object for `cmd`, it is treated as the | ||
`action`. | ||
|
||
Example of defining a command: | ||
|
||
// repl_test.js | ||
var repl = require('repl'); | ||
|
||
var replServer = repl.start(); | ||
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. Nit: const |
||
replServer.defineCommand('sayhello', { | ||
help: 'Say hello', | ||
action: function(name) { | ||
this.write('Hello, ' + name + '!\n'); | ||
this.displayPrompt(); | ||
} | ||
}); | ||
|
||
Example of invoking that command from the REPL: | ||
|
||
> .sayhello Node.js User | ||
Hello, Node.js User! | ||
|
||
### replServer.displayPrompt([preserveCursor]) | ||
|
||
* `preserveCursor` {Boolean} | ||
|
||
Like [readline.prompt][] except also adding indents with ellipses when inside | ||
blocks. The `preserveCursor` argument is passed to [readline.prompt][]. This is | ||
used primarily with `defineCommand`. It's also used internally to render each | ||
prompt line. | ||
|
||
[Readline Interface]: readline.html#readline_class_interface | ||
[readline.prompt]: readline.html#readline_rl_prompt_preservecursor | ||
[util.inspect()]: util.html#util_util_inspect_object_options | ||
[here]: util.html#util_custom_inspect_function_on_objects |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: const instead of var?
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.
Other pre-existing examples in the doc use
var
for such things, so I thought it was best to keep with the style already in this doc. Perhaps instead a future PR can convert them all in one fell swoop?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.
Sure. It's a nit. Ignore it.
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.
Cool beans.