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 this.paginator = new Paginator(this.screen);
49 * Start the Inquiry session
50 * @param {Function} cb Callback when prompt is done
59 var events = observe(this.rl);
60 events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
61 events.normalizedDownKey
62 .pipe(takeUntil(events.line))
63 .forEach(this.onDownKey.bind(this));
64 events.numberKey.pipe(takeUntil(events.line)).forEach(this.onNumberKey.bind(this));
68 map(this.getCurrentValue.bind(this)),
69 flatMap((value) => runAsync(self.opt.filter)(value).catch((err) => err))
71 .forEach(this.onSubmit.bind(this));
81 * Render the prompt to screen
82 * @return {ListPrompt} self
87 var message = this.getQuestion();
89 if (this.firstRender) {
90 message += chalk.dim('(Use arrow keys)');
93 // Render choices or answer depending on the state
94 if (this.status === 'answered') {
95 message += chalk.cyan(this.opt.choices.getChoice(this.selected).short);
97 var choicesStr = listRender(this.opt.choices, this.selected);
98 var indexPosition = this.opt.choices.indexOf(
99 this.opt.choices.getChoice(this.selected)
101 var realIndexPosition =
102 this.opt.choices.reduce(function (acc, value, i) {
103 // Dont count lines past the choice we are looking at
104 if (i > indexPosition) {
107 // Add line if it's a separator
108 if (value.type === 'separator') {
113 // Non-strings take up one line
114 if (typeof l !== 'string') {
118 // Calculate lines taken up by string
120 return acc + l.length;
123 '\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
126 this.firstRender = false;
128 this.screen.render(message);
132 * When user press `enter` key
136 this.status = 'answered';
147 return this.opt.choices.getChoice(this.selected).value;
151 * When user press a key
154 this.selected = incrementListIndex(this.selected, 'up', this.opt);
159 this.selected = incrementListIndex(this.selected, 'down', this.opt);
164 if (input <= this.opt.choices.realLength) {
165 this.selected = input - 1;
173 * Function for rendering list choices
174 * @param {Number} pointer Position of the pointer
175 * @return {String} Rendered content
177 function listRender(choices, pointer) {
179 var separatorOffset = 0;
181 choices.forEach((choice, i) => {
182 if (choice.type === 'separator') {
184 output += ' ' + choice + '\n';
188 if (choice.disabled) {
190 output += ' - ' + choice.name;
191 output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
196 var isSelected = i - separatorOffset === pointer;
197 var line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
199 line = chalk.cyan(line);
202 output += line + ' \n';
205 return output.replace(/\n$/, '');
208 module.exports = ListPrompt;