Package Exports
- prompt-base
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (prompt-base) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
prompt-base

Base prompt module used for creating custom prompt types for Enquirer.
Install
Install with npm:
$ npm install --save prompt-base
Usage
See the examples folder for more detailed usage examples.
var Prompt = require('prompt-base');
var prompt = new Prompt({
name: 'color',
message: 'What is your favorite color?'
});
prompt.run()
.then(function(answer) {
console.log(answer);
})
API
Prompt
Create a new Prompt with the given question
object, answers
and optional instance of readline-ui.
Params
question
{Object}: Plain object or instance of prompt-question.answers
{Object}: Optionally pass an answers object from a prompt manager (like enquirer).ui
{Object}: Optionally pass an instance of readline-ui. If not passed, an instance is created for you.
Example
var prompt = new Prompt({
name: 'color',
message: 'What is your favorite color?'
});
prompt.ask(function(answer) {
console.log(answer);
//=> 'blue'
});
.run
Initialize a prompt and resolve answers. If question.when
returns false,
the prompt will be skipped.
Params
answers
{Object}returns
{Promise}
.ask
Default ask
method. This mayb eb overridden in custom prompts.
.render
Render the current prompt input. This can be replaced by custom prompts.
Example
prompt.ui.on('keypress', prompt.render.bind(prompt));
.move
Move the cursor in the specific direction
when the
given event
is emitted.
Params
direction
{String}event
{Object}
.onKeypress
Default keypress
event handler. This may be overridden in custom prompts.
Params
event
{Object}
.onSubmit
When the answer is submitted (user presses enter
key), re-render
and pass answer to callback. This may be replaced by custom prompts.
Params
input
{Object}
.onTabKey
Default tab
event handler. This may be overridden in custom prompts.
Params
event
{Object}
.onError
Default error
event handler. This may be overridden in custom prompts.
Params
event
{Object}
.format
Returns a formatted prompt message.
returns
{String}
.write
Proxy to readline.write for manually writing output. When called, rl.write() will resume the input stream if it has been paused.
returns
{undefined}
Example
prompt.write('blue\n');
prompt.write(null, {ctrl: true, name: 'l'});
.choices
Getter for getting the choices array from the question.
returns
{Object}: Choices object
.message
Getter that returns question.message
after passing it to format.
returns
{String}: A formatted prompt message.
.prefix
Getter that returns the prefix to use before question.message
. The default value is a green ?
.
returns
{String}: The formatted prefix.
Example
prompt.prefix = '!';
.Separator
Create a new Separator
object. See choices-separator for more details.
Params
separator
{String}: Optionally pass a string to use as the separator.returns
{Object}: Returns a separator object.
Example
new Prompt.Separator('---');
Examples
Instantiate
The main purpose of this library is to be inherited by other libraries to create custom prompt types. However, the main export is a function that can be instantiated to run basic "input" prompts, if you want to see how everything works, run examples, tests, etc.
var Prompt = require('prompt-base');
var prompt = new Prompt({
name: 'first',
message: 'What is your name?'
});
// callback
prompt.ask(function(answer) {
console.log(answer);
//=> 'Jon'
});
// promise
prompt.run()
.then(function(answers) {
console.log(answers);
//=> {first: 'Jon'}
});
Inherit
var Prompt = require('prompt-base');
function CustomPrompt(/*question, answers, rl*/) {
Prompt.apply(this, arguments);
}
util.inherits(CustomPrompt, Prompt);
In the wild
The following custom prompts were created using this library:
- prompt-autocomplete: A prompt in the terminal but with autocomplete functionality | homepage
- prompt-checkbox: Multiple-choice/checkbox prompt. Can be used standalone or with a prompt system like Enquirer. | homepage
- prompt-confirm: Confirm (yes/no) prompt. Can be used standalone or with a prompt system like Enquirer. | homepage
- prompt-editor: Editor prompt. Opens your text editor and waits for you to save your input during… more | homepage
- prompt-expand: Expand prompt. Can be used as a standalone prompt, or with a prompt system like… more | homepage
- prompt-list: List-style prompt. Can be used as a standalone prompt, or with a prompt system like… more | homepage
- prompt-password: Password prompt. Can be used as a standalone prompt, or with a prompt system like… more | homepage
- prompt-radio: Radio prompt. This prompt behaves like other radio-button interfaces, where only one choice is enabled… more | homepage
- prompt-rawlist: Rawlist prompt. Can be used as a standalone prompt, or with a prompt system like… more | homepage
About
Related projects
- enquirer: Intuitive, plugin-based prompt system for node.js. Much faster and lighter alternative to Inquirer, with all… more | homepage
- prompt-choices: Create an array of multiple choice objects for use in prompts. | homepage
- prompt-question: Question object, used by Enquirer and prompt plugins. | homepage
- readline-utils: Readline utils, for moving the cursor, clearing lines, creating a readline interface, and more. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Contributors
Commits | Contributor |
---|---|
57 | jonschlinkert |
6 | doowb |
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on May 05, 2017.