7 isNumber: require('lodash/isNumber'),
8 findIndex: require('lodash/findIndex'),
9 isString: require('lodash/isString'),
11 var chalk = require('chalk');
12 var figures = require('figures');
13 var cliCursor = require('cli-cursor');
14 var runAsync = require('run-async');
15 var { flatMap, map, take, takeUntil } = require('rxjs/operators');
16 var Base = require('./base');
17 var observe = require('../utils/events');
18 var Paginator = require('../utils/paginator');
19 var incrementListIndex = require('../utils/incrementListIndex');
21 class ListPrompt extends Base {
22 constructor(questions, rl, answers) {
23 super(questions, rl, answers);
25 if (!this.opt.choices) {
26 this.throwParamError('choices');
29 this.firstRender = true;
32 var def = this.opt.default;
34 // If def is a Number, then use as index. Otherwise, check for value.
35 if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
37 } else if (!_.isNumber(def) && def != null) {
38 let index = _.findIndex(this.opt.choices.realChoices, ({ value }) => value === def);
39 this.selected = Math.max(index, 0);
42 // Make sure no default is set (so it won't be printed)
43 this.opt.default = null;
45 const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
46 this.paginator = new Paginator(this.screen, { isInfinite: shouldLoop });
50 * Start the Inquiry session
51 * @param {Function} cb Callback when prompt is done
60 var events = observe(this.rl);
61 events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
62 events.normalizedDownKey
63 .pipe(takeUntil(events.line))
64 .forEach(this.onDownKey.bind(this));
65 events.numberKey.pipe(takeUntil(events.line)).forEach(this.onNumberKey.bind(this));
69 map(this.getCurrentValue.bind(this)),
70 flatMap((value) => runAsync(self.opt.filter)(value).catch((err) => err))
72 .forEach(this.onSubmit.bind(this));
82 * Render the prompt to screen
83 * @return {ListPrompt} self
88 var message = this.getQuestion();
90 if (this.firstRender) {
91 message += chalk.dim('(Use arrow keys)');
94 // Render choices or answer depending on the state
95 if (this.status === 'answered') {
96 message += chalk.cyan(this.opt.choices.getChoice(this.selected).short);
98 var choicesStr = listRender(this.opt.choices, this.selected);
99 var indexPosition = this.opt.choices.indexOf(
100 this.opt.choices.getChoice(this.selected)
102 var realIndexPosition =
103 this.opt.choices.reduce(function (acc, value, i) {
104 // Dont count lines past the choice we are looking at
105 if (i > indexPosition) {
108 // Add line if it's a separator
109 if (value.type === 'separator') {
114 // Non-strings take up one line
115 if (typeof l !== 'string') {
119 // Calculate lines taken up by string
121 return acc + l.length;
124 '\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
127 this.firstRender = false;
129 this.screen.render(message);
133 * When user press `enter` key
137 this.status = 'answered';
148 return this.opt.choices.getChoice(this.selected).value;
152 * When user press a key
155 this.selected = incrementListIndex(this.selected, 'up', this.opt);
160 this.selected = incrementListIndex(this.selected, 'down', this.opt);
165 if (input <= this.opt.choices.realLength) {
166 this.selected = input - 1;
174 * Function for rendering list choices
175 * @param {Number} pointer Position of the pointer
176 * @return {String} Rendered content
178 function listRender(choices, pointer) {
180 var separatorOffset = 0;
182 choices.forEach((choice, i) => {
183 if (choice.type === 'separator') {
185 output += ' ' + choice + '\n';
189 if (choice.disabled) {
191 output += ' - ' + choice.name;
192 output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
197 var isSelected = i - separatorOffset === pointer;
198 var line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
200 line = chalk.cyan(line);
203 output += line + ' \n';
206 return output.replace(/\n$/, '');
209 module.exports = ListPrompt;