7 isArray: require('lodash/isArray'),
8 map: require('lodash/map'),
9 isString: require('lodash/isString'),
11 var chalk = require('chalk');
12 var cliCursor = require('cli-cursor');
13 var figures = require('figures');
14 var { map, takeUntil } = require('rxjs/operators');
15 var Base = require('./base');
16 var observe = require('../utils/events');
17 var Paginator = require('../utils/paginator');
19 class CheckboxPrompt extends Base {
20 constructor(questions, rl, answers) {
21 super(questions, rl, answers);
23 if (!this.opt.choices) {
24 this.throwParamError('choices');
27 if (_.isArray(this.opt.default)) {
28 this.opt.choices.forEach(function (choice) {
29 if (this.opt.default.indexOf(choice.value) >= 0) {
30 choice.checked = true;
37 // Make sure no default is set (so it won't be printed)
38 this.opt.default = null;
40 this.paginator = new Paginator(this.screen);
44 * Start the Inquiry session
45 * @param {Function} cb Callback when prompt is done
52 var events = observe(this.rl);
54 var validation = this.handleSubmitEvents(
55 events.line.pipe(map(this.getCurrentValue.bind(this)))
57 validation.success.forEach(this.onEnd.bind(this));
58 validation.error.forEach(this.onError.bind(this));
60 events.normalizedUpKey
61 .pipe(takeUntil(validation.success))
62 .forEach(this.onUpKey.bind(this));
63 events.normalizedDownKey
64 .pipe(takeUntil(validation.success))
65 .forEach(this.onDownKey.bind(this));
67 .pipe(takeUntil(validation.success))
68 .forEach(this.onNumberKey.bind(this));
70 .pipe(takeUntil(validation.success))
71 .forEach(this.onSpaceKey.bind(this));
72 events.aKey.pipe(takeUntil(validation.success)).forEach(this.onAllKey.bind(this));
73 events.iKey.pipe(takeUntil(validation.success)).forEach(this.onInverseKey.bind(this));
78 this.firstRender = false;
84 * Render the prompt to screen
85 * @return {CheckboxPrompt} self
90 var message = this.getQuestion();
91 var bottomContent = '';
93 if (!this.spaceKeyPressed) {
96 chalk.cyan.bold('<space>') +
98 chalk.cyan.bold('<a>') +
100 chalk.cyan.bold('<i>') +
101 ' to invert selection)';
104 // Render choices or answer depending on the state
105 if (this.status === 'answered') {
106 message += chalk.cyan(this.selection.join(', '));
108 var choicesStr = renderChoices(this.opt.choices, this.pointer);
109 var indexPosition = this.opt.choices.indexOf(
110 this.opt.choices.getChoice(this.pointer)
112 var realIndexPosition =
113 this.opt.choices.reduce(function (acc, value, i) {
114 // Dont count lines past the choice we are looking at
115 if (i > indexPosition) {
118 // Add line if it's a separator
119 if (value.type === 'separator') {
124 // Non-strings take up one line
125 if (typeof l !== 'string') {
129 // Calculate lines taken up by string
131 return acc + l.length;
134 '\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
138 bottomContent = chalk.red('>> ') + error;
141 this.screen.render(message, bottomContent);
145 * When user press `enter` key
149 this.status = 'answered';
150 this.spaceKeyPressed = true;
151 // Rerender prompt (and clean subline error)
156 this.done(state.value);
160 this.render(state.isValid);
164 var choices = this.opt.choices.filter(function (choice) {
165 return Boolean(choice.checked) && !choice.disabled;
168 this.selection = _.map(choices, 'short');
169 return _.map(choices, 'value');
173 var len = this.opt.choices.realLength;
174 this.pointer = this.pointer > 0 ? this.pointer - 1 : len - 1;
179 var len = this.opt.choices.realLength;
180 this.pointer = this.pointer < len - 1 ? this.pointer + 1 : 0;
185 if (input <= this.opt.choices.realLength) {
186 this.pointer = input - 1;
187 this.toggleChoice(this.pointer);
194 this.spaceKeyPressed = true;
195 this.toggleChoice(this.pointer);
200 var shouldBeChecked = Boolean(
201 this.opt.choices.find(function (choice) {
202 return choice.type !== 'separator' && !choice.checked;
206 this.opt.choices.forEach(function (choice) {
207 if (choice.type !== 'separator') {
208 choice.checked = shouldBeChecked;
216 this.opt.choices.forEach(function (choice) {
217 if (choice.type !== 'separator') {
218 choice.checked = !choice.checked;
225 toggleChoice(index) {
226 var item = this.opt.choices.getChoice(index);
227 if (item !== undefined) {
228 this.opt.choices.getChoice(index).checked = !item.checked;
234 * Function for rendering checkbox choices
235 * @param {Number} pointer Position of the pointer
236 * @return {String} Rendered content
239 function renderChoices(choices, pointer) {
241 var separatorOffset = 0;
243 choices.forEach(function (choice, i) {
244 if (choice.type === 'separator') {
246 output += ' ' + choice + '\n';
250 if (choice.disabled) {
252 output += ' - ' + choice.name;
253 output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
255 var line = getCheckbox(choice.checked) + ' ' + choice.name;
256 if (i - separatorOffset === pointer) {
257 output += chalk.cyan(figures.pointer + line);
259 output += ' ' + line;
266 return output.replace(/\n$/, '');
271 * @param {Boolean} checked - add a X or not to the checkbox
272 * @return {String} Composited checkbox string
275 function getCheckbox(checked) {
276 return checked ? chalk.green(figures.radioOn) : figures.radioOff;
279 module.exports = CheckboxPrompt;