1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const _ = {
|
7 | isArray: require('lodash/isArray'),
|
8 | map: require('lodash/map'),
|
9 | isString: require('lodash/isString'),
|
10 | };
|
11 | const chalk = require('chalk');
|
12 | const cliCursor = require('cli-cursor');
|
13 | const figures = require('figures');
|
14 | const { map, takeUntil } = require('rxjs/operators');
|
15 | const Base = require('./base');
|
16 | const observe = require('../utils/events');
|
17 | const Paginator = require('../utils/paginator');
|
18 | const incrementListIndex = require('../utils/incrementListIndex');
|
19 |
|
20 | class CheckboxPrompt extends Base {
|
21 | constructor(questions, rl, answers) {
|
22 | super(questions, rl, answers);
|
23 |
|
24 | if (!this.opt.choices) {
|
25 | this.throwParamError('choices');
|
26 | }
|
27 |
|
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;
|
32 | }
|
33 | }, this);
|
34 | }
|
35 |
|
36 | this.pointer = 0;
|
37 |
|
38 |
|
39 | this.opt.default = null;
|
40 |
|
41 | const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
|
42 | this.paginator = new Paginator(this.screen, { isInfinite: shouldLoop });
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | _run(cb) {
|
52 | this.done = cb;
|
53 |
|
54 | const events = observe(this.rl);
|
55 |
|
56 | const validation = this.handleSubmitEvents(
|
57 | events.line.pipe(map(this.getCurrentValue.bind(this)))
|
58 | );
|
59 | validation.success.forEach(this.onEnd.bind(this));
|
60 | validation.error.forEach(this.onError.bind(this));
|
61 |
|
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));
|
68 | events.numberKey
|
69 | .pipe(takeUntil(validation.success))
|
70 | .forEach(this.onNumberKey.bind(this));
|
71 | events.spaceKey
|
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));
|
76 |
|
77 |
|
78 | cliCursor.hide();
|
79 | this.render();
|
80 | this.firstRender = false;
|
81 |
|
82 | return this;
|
83 | }
|
84 |
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | render(error) {
|
91 |
|
92 | let message = this.getQuestion();
|
93 | let bottomContent = '';
|
94 |
|
95 | if (!this.dontShowHints) {
|
96 | message +=
|
97 | '(Press ' +
|
98 | chalk.cyan.bold('<space>') +
|
99 | ' to select, ' +
|
100 | chalk.cyan.bold('<a>') +
|
101 | ' to toggle all, ' +
|
102 | chalk.cyan.bold('<i>') +
|
103 | ' to invert selection, and ' +
|
104 | chalk.cyan.bold('<enter>') +
|
105 | ' to proceed)';
|
106 | }
|
107 |
|
108 |
|
109 | if (this.status === 'answered') {
|
110 | message += chalk.cyan(this.selection.join(', '));
|
111 | } else {
|
112 | const choicesStr = renderChoices(this.opt.choices, this.pointer);
|
113 | const indexPosition = this.opt.choices.indexOf(
|
114 | this.opt.choices.getChoice(this.pointer)
|
115 | );
|
116 | const realIndexPosition =
|
117 | this.opt.choices.reduce((acc, value, i) => {
|
118 |
|
119 | if (i > indexPosition) {
|
120 | return acc;
|
121 | }
|
122 |
|
123 | if (value.type === 'separator') {
|
124 | return acc + 1;
|
125 | }
|
126 |
|
127 | let l = value.name;
|
128 |
|
129 | if (typeof l !== 'string') {
|
130 | return acc + 1;
|
131 | }
|
132 |
|
133 |
|
134 | l = l.split('\n');
|
135 | return acc + l.length;
|
136 | }, 0) - 1;
|
137 | message +=
|
138 | '\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
|
139 | }
|
140 |
|
141 | if (error) {
|
142 | bottomContent = chalk.red('>> ') + error;
|
143 | }
|
144 |
|
145 | this.screen.render(message, bottomContent);
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 | onEnd(state) {
|
153 | this.status = 'answered';
|
154 | this.dontShowHints = true;
|
155 |
|
156 | this.render();
|
157 |
|
158 | this.screen.done();
|
159 | cliCursor.show();
|
160 | this.done(state.value);
|
161 | }
|
162 |
|
163 | onError(state) {
|
164 | this.render(state.isValid);
|
165 | }
|
166 |
|
167 | getCurrentValue() {
|
168 | const choices = this.opt.choices.filter(
|
169 | (choice) => Boolean(choice.checked) && !choice.disabled
|
170 | );
|
171 |
|
172 | this.selection = _.map(choices, 'short');
|
173 | return _.map(choices, 'value');
|
174 | }
|
175 |
|
176 | onUpKey() {
|
177 | this.pointer = incrementListIndex(this.pointer, 'up', this.opt);
|
178 | this.render();
|
179 | }
|
180 |
|
181 | onDownKey() {
|
182 | this.pointer = incrementListIndex(this.pointer, 'down', this.opt);
|
183 | this.render();
|
184 | }
|
185 |
|
186 | onNumberKey(input) {
|
187 | if (input <= this.opt.choices.realLength) {
|
188 | this.pointer = input - 1;
|
189 | this.toggleChoice(this.pointer);
|
190 | }
|
191 |
|
192 | this.render();
|
193 | }
|
194 |
|
195 | onSpaceKey() {
|
196 | this.toggleChoice(this.pointer);
|
197 | this.render();
|
198 | }
|
199 |
|
200 | onAllKey() {
|
201 | const shouldBeChecked = Boolean(
|
202 | this.opt.choices.find((choice) => choice.type !== 'separator' && !choice.checked)
|
203 | );
|
204 |
|
205 | this.opt.choices.forEach((choice) => {
|
206 | if (choice.type !== 'separator') {
|
207 | choice.checked = shouldBeChecked;
|
208 | }
|
209 | });
|
210 |
|
211 | this.render();
|
212 | }
|
213 |
|
214 | onInverseKey() {
|
215 | this.opt.choices.forEach((choice) => {
|
216 | if (choice.type !== 'separator') {
|
217 | choice.checked = !choice.checked;
|
218 | }
|
219 | });
|
220 |
|
221 | this.render();
|
222 | }
|
223 |
|
224 | toggleChoice(index) {
|
225 | const item = this.opt.choices.getChoice(index);
|
226 | if (item !== undefined) {
|
227 | this.opt.choices.getChoice(index).checked = !item.checked;
|
228 | }
|
229 | }
|
230 | }
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | function renderChoices(choices, pointer) {
|
239 | let output = '';
|
240 | let separatorOffset = 0;
|
241 |
|
242 | choices.forEach((choice, i) => {
|
243 | if (choice.type === 'separator') {
|
244 | separatorOffset++;
|
245 | output += ' ' + choice + '\n';
|
246 | return;
|
247 | }
|
248 |
|
249 | if (choice.disabled) {
|
250 | separatorOffset++;
|
251 | output += ' - ' + choice.name;
|
252 | output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
|
253 | } else {
|
254 | const line = getCheckbox(choice.checked) + ' ' + choice.name;
|
255 | if (i - separatorOffset === pointer) {
|
256 | output += chalk.cyan(figures.pointer + line);
|
257 | } else {
|
258 | output += ' ' + line;
|
259 | }
|
260 | }
|
261 |
|
262 | output += '\n';
|
263 | });
|
264 |
|
265 | return output.replace(/\n$/, '');
|
266 | }
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | function getCheckbox(checked) {
|
275 | return checked ? chalk.green(figures.radioOn) : figures.radioOff;
|
276 | }
|
277 |
|
278 | module.exports = CheckboxPrompt;
|