-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
548 lines (407 loc) · 11.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/**
* Checkbox Plus
*
* @author Mohammad Fares <[email protected]>
*/
'use strict';
var _ = require('lodash');
var chalk = require('chalk');
var { map, takeUntil } = require('rxjs/operators');
var cliCursor = require('cli-cursor');
var figures = require('figures');
var Base = require('inquirer/lib/prompts/base');
var Choices = require('inquirer/lib/objects/choices');
var observe = require('inquirer/lib/utils/events');
var Paginator = require('inquirer/lib/utils/paginator');
/**
* CheckboxPlusPrompt
*/
class CheckboxPlusPrompt extends Base {
/**
* Initialize the prompt
*
* @param {Object} questions
* @param {Object} rl
* @param {Object} answers
*/
constructor(questions, rl, answers) {
super(questions, rl, answers);
// Default value for the highlight option
if (typeof this.opt.highlight == 'undefined') {
this.opt.highlight = false;
}
// Default value for the searchable option
if (typeof this.opt.searchable == 'undefined') {
this.opt.searchable = false;
}
// Default value for the default option
if (typeof this.opt.default == 'undefined') {
this.opt.default = null;
}
// Doesn't have source option
if (!this.opt.source) {
this.throwParamError('source');
}
// Init
this.pointer = 0;
this.firstSourceLoading = true;
this.choices = new Choices([], answers);
this.checkedChoices = [];
this.value = [];
this.lastQuery = null;
this.searching = false;
this.lastSourcePromise = null;
this.default = this.opt.default;
this.opt.default = null;
this.paginator = new Paginator(this.screen);
}
/**
* Start the Inquiry session
*
* @param {Function} callback callback when prompt is done
* @return {this}
*/
_run(callback) {
var self = this;
this.done = callback;
this.executeSource().then(function(result) {
var events = observe(self.rl);
var validation = self.handleSubmitEvents(
events.line.pipe(map(self.getCurrentValue.bind(self)))
);
validation.success.forEach(self.onEnd.bind(self));
validation.error.forEach(self.onError.bind(self));
events.normalizedUpKey
.pipe(takeUntil(validation.success))
.forEach(self.onUpKey.bind(self));
events.normalizedDownKey
.pipe(takeUntil(validation.success))
.forEach(self.onDownKey.bind(self));
events.keypress
.pipe(takeUntil(validation.success))
.forEach(self.onKeypress.bind(self));
events.spaceKey
.pipe(takeUntil(validation.success))
.forEach(self.onSpaceKey.bind(self));
// If the search is enabled
if (!self.opt.searchable) {
events.numberKey
.pipe(takeUntil(validation.success))
.forEach(self.onNumberKey.bind(self));
events.aKey
.pipe(takeUntil(validation.success))
.forEach(self.onAllKey.bind(self));
events.iKey
.pipe(takeUntil(validation.success))
.forEach(self.onInverseKey.bind(self));
} else {
events.keypress
.pipe(takeUntil(validation.success))
.forEach(self.onKeypress.bind(self));
}
if (self.rl.line) {
self.onKeypress();
}
// Init the prompt
cliCursor.hide();
self.render();
});
return this;
}
/**
* Execute the source function to get the choices and render them
*/
executeSource() {
var self = this;
var sourcePromise = null;
// Remove spaces
this.rl.line = _.trim(this.rl.line);
// Same last search query that already loaded
if (this.rl.line === this.lastQuery) {
return;
}
// If the search is enabled
if (this.opt.searchable) {
sourcePromise = this.opt.source(this.answers, this.rl.line);
} else {
sourcePromise = this.opt.source(this.answers, null);
}
this.lastQuery = this.rl.line;
this.lastSourcePromise = sourcePromise;
this.searching = true;
sourcePromise.then(function(choices) {
// Is not the last issued promise
if (self.lastSourcePromise !== sourcePromise) {
return;
}
// Reset the searching status
self.searching = false;
// Save the new choices
self.choices = new Choices(choices, self.answers);
// Foreach choice
self.choices.forEach(function(choice) {
// Is the current choice included in the current checked choices
if (_.findIndex(self.value, _.isEqual.bind(null, choice.value)) != -1) {
self.toggleChoice(choice, true);
} else {
self.toggleChoice(choice, false);
}
// The default is not applied yet
if (self.default) {
// Is the current choice included in the default values
if (_.findIndex(self.default, _.isEqual.bind(null, choice.value)) != -1) {
self.toggleChoice(choice, true);
}
}
});
// Reset the pointer to select the first choice
self.pointer = 0;
self.render();
self.default = null;
self.firstSourceLoading = false;
});
return sourcePromise;
}
/**
* Render the prompt
*
* @param {Object} error
*/
render(error) {
// Render question
var message = this.getQuestion();
var bottomContent = '';
// Answered
if (this.status === 'answered') {
message += chalk.cyan(this.selection.join(', '));
return this.screen.render(message, bottomContent);
}
// No search query is entered before
if (this.firstSourceLoading) {
// If the search is enabled
if (this.opt.searchable) {
message +=
'(Press ' +
chalk.cyan.bold('<space>') +
' to select, ' +
'or type anything to filter the list)';
} else {
message +=
'(Press ' +
chalk.cyan.bold('<space>') +
' to select, ' +
chalk.cyan.bold('<a>') +
' to toggle all, ' +
chalk.cyan.bold('<i>') +
' to invert selection)';
}
}
// If the search is enabled
if (this.opt.searchable) {
// Print the current search query
message += this.rl.line;
}
// Searching mode
if (this.searching) {
message += '\n ' + chalk.cyan('Searching...');
// No choices
} else if (!this.choices.length) {
message += '\n ' + chalk.yellow('No results...');
// Has choices
} else {
var choicesStr = this.renderChoices(this.choices, this.pointer);
var indexPosition = this.choices.indexOf(
this.choices.getChoice(this.pointer)
);
message += '\n' + this.paginator.paginate(choicesStr, indexPosition, this.opt.pageSize);
}
if (error) {
bottomContent = chalk.red('>> ') + error;
}
this.screen.render(message, bottomContent);
}
/**
* A callback function for the event:
* When the user press `Enter` key
*
* @param {Object} state
*/
onEnd(state) {
this.status = 'answered';
// Rerender prompt (and clean subline error)
this.render();
this.screen.done();
cliCursor.show();
this.done(state.value);
}
/**
* A callback function for the event:
* When something wrong happen
*
* @param {Object} state
*/
onError(state) {
this.render(state.isValid);
}
/**
* Get the current values of the selected choices
*
* @return {Array}
*/
getCurrentValue() {
this.selection = _.map(this.checkedChoices, 'short');
return _.map(this.checkedChoices, 'value');
}
/**
* A callback function for the event:
* When the user press `Up` key
*/
onUpKey() {
var len = this.choices.realLength;
this.pointer = this.pointer > 0 ? this.pointer - 1 : len - 1;
this.render();
}
/**
* A callback function for the event:
* When the user press `Down` key
*/
onDownKey() {
var len = this.choices.realLength;
this.pointer = this.pointer < len - 1 ? this.pointer + 1 : 0;
this.render();
}
/**
* A callback function for the event:
* When the user press a number key
*/
onNumberKey(input) {
if (input <= this.choices.realLength) {
this.pointer = input - 1;
this.toggleChoice(this.choices.getChoice(this.pointer));
}
this.render();
}
/**
* A callback function for the event:
* When the user press `Space` key
*/
onSpaceKey() {
// When called no results
if (!this.choices.getChoice(this.pointer)) {
return;
}
this.toggleChoice(this.choices.getChoice(this.pointer));
this.render();
}
/**
* A callback function for the event:
* When the user press 'a' key
*/
onAllKey() {
var shouldBeChecked = Boolean(
this.choices.find(function(choice) {
return choice.type !== 'separator' && !choice.checked;
})
);
this.choices.forEach(function(choice) {
if (choice.type !== 'separator') {
choice.checked = shouldBeChecked;
}
});
this.render();
}
/**
* A callback function for the event:
* When the user press `i` key
*/
onInverseKey() {
this.choices.forEach(function(choice) {
if (choice.type !== 'separator') {
choice.checked = !choice.checked;
}
});
this.render();
}
/**
* A callback function for the event:
* When the user press any key
*/
onKeypress() {
this.executeSource();
this.render();
}
/**
* Toggle (check/uncheck) a specific choice
*
* @param {Boolean} checked if not specified the status will be toggled
* @param {Object} choice
*/
toggleChoice(choice, checked) {
// Default value for checked
if (typeof checked === 'undefined') {
checked = !choice.checked;
}
// Remove the choice's value from the checked values
_.remove(this.value, _.isEqual.bind(null, choice.value));
// Remove the checkedChoices with the value of the current choice
_.remove(this.checkedChoices, function(checkedChoice) {
return _.isEqual(choice.value, checkedChoice.value);
});
choice.checked = false;
// Is the choice checked
if (checked) {
this.value.push(choice.value);
this.checkedChoices.push(choice);
choice.checked = true;
}
}
/**
* Get the checkbox figure (sign)
*
* @param {Boolean} checked
* @return {String}
*/
getCheckboxFigure(checked) {
return checked ? chalk.green(figures.radioOn) : figures.radioOff;
}
/**
* Render the checkbox choices
*
* @param {Array} choices
* @param {Number} pointer the position of the pointer
* @return {String} rendered content
*/
renderChoices(choices, pointer) {
var self = this;
var output = '';
var separatorOffset = 0;
// Foreach choice
choices.forEach(function(choice, index) {
// Is a separator
if (choice.type === 'separator') {
separatorOffset++;
output += ' ' + choice + '\n';
return;
}
// Is the choice disabled
if (choice.disabled) {
separatorOffset++;
output += ' - ' + choice.name;
output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
output += '\n';
return;
}
// Is the current choice is the selected choice
if (index - separatorOffset === pointer) {
output += chalk.cyan(figures.pointer);
output += self.getCheckboxFigure(choice.checked) + ' ';
output += self.opt.highlight ? chalk.gray(choice.name) : choice.name;
} else {
output += ' ' + self.getCheckboxFigure(choice.checked) + ' ' + choice.name;
}
output += '\n';
});
return output.replace(/\n$/, '');
}
}
module.exports = CheckboxPlusPrompt;