function Prompt() { Base.apply(this, arguments); if (!this.opt.source) { this.throwParamError('source'); } this.currentChoices = []; this.firstRender = true; this.selected = 0; // Make sure no default is set (so it won't be printed) this.opt.default = null; this.paginator = new Paginator(); }
n/a
super_ = function (question, rl, answers) { // Setup instance defaults property _.assign(this, { answers: answers, status: 'pending' }); // Set defaults prompt options this.opt = _.defaults(_.clone(question), { validate: function () { return true; }, filter: function (val) { return val; }, when: function () { return true; } }); // Check to make sure prompt requirements are there if (!this.opt.message) { this.throwParamError('message'); } if (!this.opt.name) { this.throwParamError('name'); } // Normalize choices if (Array.isArray(this.opt.choices)) { this.opt.choices = new Choices(this.opt.choices, answers); } this.rl = rl; this.screen = new ScreenManager(this.rl); }
n/a
super_ = function (question, rl, answers) { // Setup instance defaults property _.assign(this, { answers: answers, status: 'pending' }); // Set defaults prompt options this.opt = _.defaults(_.clone(question), { validate: function () { return true; }, filter: function (val) { return val; }, when: function () { return true; } }); // Check to make sure prompt requirements are there if (!this.opt.message) { this.throwParamError('message'); } if (!this.opt.name) { this.throwParamError('name'); } // Normalize choices if (Array.isArray(this.opt.choices)) { this.opt.choices = new Choices(this.opt.choices, answers); } this.rl = rl; this.screen = new ScreenManager(this.rl); }
n/a
_run = function (cb) { cb(); }
n/a
close = function () { this.screen.releaseCursor(); }
n/a
getQuestion = function () { var message = chalk.green('?') + ' ' + chalk.bold(this.opt.message) + ' '; // Append the default if available, and if question isn't answered if (this.opt.default != null && this.status !== 'answered') { message += chalk.dim('(' + this.opt.default + ') '); } return message; }
...
/**
* Render the prompt to screen
* @return {Prompt} self
*/
Prompt.prototype.render = function(error) {
// Render question
var content = this.getQuestion();
var bottomContent = '';
if (this.firstRender) {
var suggestText = this.opt.suggestOnly ? ', tab to autocomplete' : '';
content += chalk.dim('(Use arrow keys or type to search' + suggestText + ')');
}
// Render choices or answer depending on the state
...
handleSubmitEvents = function (submit) { var self = this; var validate = runAsync(this.opt.validate); var filter = runAsync(this.opt.filter); var validation = submit.flatMap(function (value) { return filter(value).then(function (filteredValue) { return validate(filteredValue, self.answers).then(function (isValid) { return {isValid: isValid, value: filteredValue}; }, function (err) { return {isValid: err}; }); }, function (err) { return {isValid: err}; }); }).share(); var success = validation .filter(function (state) { return state.isValid === true; }) .take(1); var error = validation .filter(function (state) { return state.isValid !== true; }) .takeUntil(success); return { success: success, error: error }; }
n/a
run = function () { return new Promise(function (resolve) { this._run(function (value) { resolve(value); }); }.bind(this)); }
n/a
throwParamError = function (name) { throw new Error('You must provide a `' + name + '` parameter'); }
...
* Constructor
*/
function Prompt() {
Base.apply(this, arguments);
if (!this.opt.source) {
this.throwParamError('source');
}
this.currentChoices = [];
this.firstRender = true;
this.selected = 0;
...