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');
18 var incrementListIndex = require('../utils/incrementListIndex');
20 class CheckboxPrompt extends Base {
21 constructor(questions, rl, answers) {
22 super(questions, rl, answers);
24 if (!this.opt.choices) {
25 this.throwParamError('choices');
28 if (_.isArray(this.opt.default)) {
29 this.opt.choices.forEach(function (choice) {
30 if (this.opt.default.indexOf(choice.value) >= 0) {
31 choice.checked = true;
38 // Make sure no default is set (so it won't be printed)
39 this.opt.default = null;
41 const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
42 this.paginator = new Paginator(this.screen, { isInfinite: shouldLoop });
46 * Start the Inquiry session
47 * @param {Function} cb Callback when prompt is done
54 var events = observe(this.rl);
56 var validation = this.handleSubmitEvents(
57 events.line.pipe(map(this.getCurrentValue.bind(this)))
59 validation.success.forEach(this.onEnd.bind(this));
60 validation.error.forEach(this.onError.bind(this));
62 events.normalizedUpKey
63 .pipe(takeUntil(validation.success))
64 .forEach(this.onUpKey.bind(this));
65 events.normalizedDownKey
66 .pipe(takeUntil(validation.success))
67 .forEach(this.onDownKey.bind(this));
69 .pipe(takeUntil(validation.success))
70 .forEach(this.onNumberKey.bind(this));
72 .pipe(takeUntil(validation.success))
73 .forEach(this.onSpaceKey.bind(this));
74 events.aKey.pipe(takeUntil(validation.success)).forEach(this.onAllKey.bind(this));
75 events.iKey.pipe(takeUntil(validation.success)).forEach(this.onInverseKey.bind(this));
80 this.firstRender = false;
86 * Render the prompt to screen
87 * @return {CheckboxPrompt} self
92 var message = this.getQuestion();
93 var bottomContent = '';
95 if (!this.spaceKeyPressed) {
98 chalk.cyan.bold('<space>') +
100 chalk.cyan.bold('<a>') +
102 chalk.cyan.bold('<i>') +
103 ' to invert selection)';
106 // Render choices or answer depending on the state
107 if (this.status === 'answered') {
108 message += chalk.cyan(this.selection.join(', '));
110 var choicesStr = renderChoices(this.opt.choices, this.pointer);
111 var indexPosition = this.opt.choices.indexOf(
112 this.opt.choices.getChoice(this.pointer)
114 var realIndexPosition =
115 this.opt.choices.reduce(function (acc, value, i) {
116 // Dont count lines past the choice we are looking at
117 if (i > indexPosition) {
120 // Add line if it's a separator
121 if (value.type === 'separator') {
126 // Non-strings take up one line
127 if (typeof l !== 'string') {
131 // Calculate lines taken up by string
133 return acc + l.length;
136 '\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
140 bottomContent = chalk.red('>> ') + error;
143 this.screen.render(message, bottomContent);
147 * When user press `enter` key
151 this.status = 'answered';
152 this.spaceKeyPressed = true;
153 // Rerender prompt (and clean subline error)
158 this.done(state.value);
162 this.render(state.isValid);
166 var choices = this.opt.choices.filter(function (choice) {
167 return Boolean(choice.checked) && !choice.disabled;
170 this.selection = _.map(choices, 'short');
171 return _.map(choices, 'value');
175 this.pointer = incrementListIndex(this.pointer, 'up', this.opt);
180 this.pointer = incrementListIndex(this.pointer, 'down', this.opt);
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;