2022-01-21 08:28:41 +00:00
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* `rawlist` type prompt
|
|
|
|
*/
|
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
var _ = require('lodash');
|
|
|
|
var chalk = require('chalk');
|
|
|
|
var { map, takeUntil } = require('rxjs/operators');
|
|
|
|
var Base = require('./base');
|
|
|
|
var Separator = require('../objects/separator');
|
|
|
|
var observe = require('../utils/events');
|
|
|
|
var Paginator = require('../utils/paginator');
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
class RawListPrompt extends Base {
|
|
|
|
constructor(questions, rl, answers) {
|
|
|
|
super(questions, rl, answers);
|
|
|
|
|
|
|
|
if (!this.opt.choices) {
|
|
|
|
this.throwParamError('choices');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.opt.validChoices = this.opt.choices.filter(Separator.exclude);
|
|
|
|
|
|
|
|
this.selected = 0;
|
|
|
|
this.rawDefault = 0;
|
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
_.extend(this.opt, {
|
|
|
|
validate: function(val) {
|
2022-01-21 08:28:41 +00:00
|
|
|
return val != null;
|
2022-05-29 09:24:36 +00:00
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
});
|
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
var def = this.opt.default;
|
|
|
|
if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
|
2022-01-21 08:28:41 +00:00
|
|
|
this.selected = def;
|
|
|
|
this.rawDefault = def;
|
2022-05-29 09:24:36 +00:00
|
|
|
} else if (!_.isNumber(def) && def != null) {
|
|
|
|
let index = _.findIndex(this.opt.choices.realChoices, ({ value }) => value === def);
|
|
|
|
let safeIndex = Math.max(index, 0);
|
2022-01-21 08:28:41 +00:00
|
|
|
this.selected = safeIndex;
|
|
|
|
this.rawDefault = safeIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure no default is set (so it won't be printed)
|
|
|
|
this.opt.default = null;
|
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
this.paginator = new Paginator();
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the Inquiry session
|
|
|
|
* @param {Function} cb Callback when prompt is done
|
|
|
|
* @return {this}
|
|
|
|
*/
|
|
|
|
|
|
|
|
_run(cb) {
|
|
|
|
this.done = cb;
|
|
|
|
|
|
|
|
// Once user confirm (enter key)
|
2022-05-29 09:24:36 +00:00
|
|
|
var events = observe(this.rl);
|
|
|
|
var submit = events.line.pipe(map(this.getCurrentValue.bind(this)));
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
var validation = this.handleSubmitEvents(submit);
|
2022-01-21 08:28:41 +00:00
|
|
|
validation.success.forEach(this.onEnd.bind(this));
|
|
|
|
validation.error.forEach(this.onError.bind(this));
|
|
|
|
|
2022-04-07 07:06:43 +00:00
|
|
|
events.keypress
|
|
|
|
.pipe(takeUntil(validation.success))
|
|
|
|
.forEach(this.onKeypress.bind(this));
|
2022-05-29 09:24:36 +00:00
|
|
|
events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
|
|
|
|
events.normalizedDownKey
|
|
|
|
.pipe(takeUntil(events.line))
|
|
|
|
.forEach(this.onDownKey.bind(this));
|
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
// Init the prompt
|
|
|
|
this.render();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the prompt to screen
|
|
|
|
* @return {RawListPrompt} self
|
|
|
|
*/
|
|
|
|
|
|
|
|
render(error) {
|
|
|
|
// Render question
|
2022-05-29 09:24:36 +00:00
|
|
|
var message = this.getQuestion();
|
|
|
|
var bottomContent = '';
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (this.status === 'answered') {
|
2022-05-29 09:24:36 +00:00
|
|
|
message += chalk.cyan(this.answer);
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2022-05-29 09:24:36 +00:00
|
|
|
var choicesStr = renderChoices(this.opt.choices, this.selected);
|
2022-01-21 08:28:41 +00:00
|
|
|
message +=
|
|
|
|
'\n' + this.paginator.paginate(choicesStr, this.selected, this.opt.pageSize);
|
|
|
|
message += '\n Answer: ';
|
|
|
|
}
|
2022-05-29 09:24:36 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
message += this.rl.line;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
bottomContent = '\n' + chalk.red('>> ') + error;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.screen.render(message, bottomContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When user press `enter` key
|
|
|
|
*/
|
|
|
|
|
|
|
|
getCurrentValue(index) {
|
2022-05-29 09:24:36 +00:00
|
|
|
if (index == null || index === '') {
|
2022-01-21 08:28:41 +00:00
|
|
|
index = this.rawDefault;
|
|
|
|
} else {
|
|
|
|
index -= 1;
|
|
|
|
}
|
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
var choice = this.opt.choices.getChoice(index);
|
2022-01-21 08:28:41 +00:00
|
|
|
return choice ? choice.value : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
onEnd(state) {
|
|
|
|
this.status = 'answered';
|
|
|
|
this.answer = state.value;
|
|
|
|
|
|
|
|
// Re-render prompt
|
|
|
|
this.render();
|
|
|
|
|
|
|
|
this.screen.done();
|
|
|
|
this.done(state.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
onError() {
|
|
|
|
this.render('Please enter a valid index');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When user press a key
|
|
|
|
*/
|
|
|
|
|
|
|
|
onKeypress() {
|
2022-05-29 09:24:36 +00:00
|
|
|
var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (this.opt.choices.getChoice(index)) {
|
|
|
|
this.selected = index;
|
|
|
|
} else {
|
|
|
|
this.selected = undefined;
|
|
|
|
}
|
2022-05-29 09:24:36 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When user press up key
|
|
|
|
*/
|
|
|
|
|
|
|
|
onUpKey() {
|
|
|
|
this.onArrowKey('up');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When user press down key
|
|
|
|
*/
|
|
|
|
|
|
|
|
onDownKey() {
|
|
|
|
this.onArrowKey('down');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When user press up or down key
|
|
|
|
* @param {String} type Arrow type: up or down
|
|
|
|
*/
|
|
|
|
|
|
|
|
onArrowKey(type) {
|
2022-05-29 09:24:36 +00:00
|
|
|
var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
|
|
|
|
if (type === 'up') index = index === 0 ? this.opt.choices.length - 1 : index - 1;
|
|
|
|
else index = index === this.opt.choices.length - 1 ? 0 : index + 1;
|
|
|
|
this.rl.line = String(index + 1);
|
|
|
|
this.onKeypress();
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function for rendering list choices
|
|
|
|
* @param {Number} pointer Position of the pointer
|
|
|
|
* @return {String} Rendered content
|
|
|
|
*/
|
|
|
|
|
|
|
|
function renderChoices(choices, pointer) {
|
2022-05-29 09:24:36 +00:00
|
|
|
var output = '';
|
|
|
|
var separatorOffset = 0;
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
choices.forEach(function(choice, i) {
|
|
|
|
output += '\n ';
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (choice.type === 'separator') {
|
|
|
|
separatorOffset++;
|
|
|
|
output += ' ' + choice;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-29 09:24:36 +00:00
|
|
|
var index = i - separatorOffset;
|
|
|
|
var display = index + 1 + ') ' + choice.name;
|
2022-01-21 08:28:41 +00:00
|
|
|
if (index === pointer) {
|
|
|
|
display = chalk.cyan(display);
|
|
|
|
}
|
|
|
|
|
|
|
|
output += display;
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RawListPrompt;
|