inquirerer 1.9.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/keypress.js CHANGED
@@ -59,6 +59,9 @@ export class TerminalKeypress {
59
59
  }
60
60
  }
61
61
  }
62
+ clearHandlers() {
63
+ this.listeners = {};
64
+ }
62
65
  pause() {
63
66
  this.active = false;
64
67
  }
package/esm/prompt.js CHANGED
@@ -68,7 +68,13 @@ function generatePromptMessage(question, ctx) {
68
68
  promptMessage += ` [${question.default}]`;
69
69
  }
70
70
  break;
71
+ case 'number':
72
+ if (question.default) {
73
+ promptMessage += ` [${question.default}]`;
74
+ }
75
+ break;
71
76
  case 'autocomplete':
77
+ case 'list':
72
78
  case 'checkbox':
73
79
  // For these types, you might want to show the default selected options if any
74
80
  if (question.options && question.default) {
@@ -275,6 +281,7 @@ export class Inquirerer {
275
281
  }
276
282
  // Apply default value if applicable
277
283
  // this is if useDefault is set, rare! not typical defaults which happen AFTER
284
+ // this is mostly to avoid a prompt for "hidden" options
278
285
  if ('default' in question && (this.useDefaults || question.useDefault)) {
279
286
  obj[question.name] = question.default;
280
287
  continue; // Skip to the next question since the default is applied
@@ -327,11 +334,13 @@ export class Inquirerer {
327
334
  handleOverrides(argv, obj, question) {
328
335
  switch (question.type) {
329
336
  case 'text':
337
+ case 'number':
330
338
  case 'confirm':
331
339
  // do nothing, already set!
332
340
  break;
333
341
  case 'checkbox':
334
342
  case 'autocomplete':
343
+ case 'list':
335
344
  // get the value from options :)
336
345
  this.handleOverridesWithOptions(argv, obj, question);
337
346
  break;
@@ -350,16 +359,20 @@ export class Inquirerer {
350
359
  }
351
360
  }
352
361
  async handleQuestionType(question, ctx) {
362
+ this.keypress.clearHandlers();
353
363
  switch (question.type) {
354
364
  case 'confirm':
355
365
  return this.confirm(question, ctx);
356
366
  case 'checkbox':
357
367
  return this.checkbox(question, ctx);
368
+ case 'list':
369
+ return this.list(question, ctx);
358
370
  case 'autocomplete':
359
371
  return this.autocomplete(question, ctx);
360
372
  case 'number':
361
373
  return this.number(question, ctx);
362
374
  case 'text':
375
+ return this.text(question, ctx);
363
376
  default:
364
377
  return this.text(question, ctx);
365
378
  }
@@ -398,6 +411,9 @@ export class Inquirerer {
398
411
  if (input.trim() !== '') {
399
412
  resolve(input); // Return input if not empty
400
413
  }
414
+ else if ('default' in question) {
415
+ resolve(question.default); // Use default if input is empty
416
+ }
401
417
  else {
402
418
  resolve(null); // Return null if empty and not required
403
419
  }
@@ -422,18 +438,41 @@ export class Inquirerer {
422
438
  resolve(num);
423
439
  }
424
440
  else {
425
- resolve(null);
441
+ resolve(null); // Let validation handle bad input
426
442
  }
427
443
  }
444
+ else if ('default' in question) {
445
+ resolve(question.default); // Use default if input is empty
446
+ }
428
447
  else {
429
- resolve(null);
448
+ resolve(null); // Empty and no default
430
449
  }
431
450
  });
432
451
  });
433
452
  }
434
453
  async checkbox(question, ctx) {
435
- if (this.noTty || !this.rl)
436
- return question.default ?? []; // Return default if non-interactive
454
+ if (this.noTty || !this.rl) {
455
+ const options = this.sanitizeOptions(question);
456
+ const defaults = Array.isArray(question.default)
457
+ ? question.default
458
+ : [question.default];
459
+ // If returnFullResults is true, return all options with boolean selection
460
+ if (question.returnFullResults) {
461
+ return options.map(opt => ({
462
+ name: opt.name,
463
+ value: opt.value,
464
+ selected: defaults.includes(opt.name)
465
+ }));
466
+ }
467
+ // Otherwise, return only selected options
468
+ return options
469
+ .filter(opt => defaults.includes(opt.name) || defaults.includes(opt.value))
470
+ .map(opt => ({
471
+ name: opt.name,
472
+ value: opt.value,
473
+ selected: true
474
+ }));
475
+ }
437
476
  if (!question.options.length) {
438
477
  // no arguments don't make sense
439
478
  throw new Error('checkbox requires options');
@@ -445,7 +484,15 @@ export class Inquirerer {
445
484
  let selectedIndex = 0;
446
485
  let startIndex = 0; // Start index for visible options
447
486
  const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
448
- const selections = new Array(options.length).fill(false);
487
+ // const selections: boolean[] = new Array(options.length).fill(false);
488
+ const selections = options.map(opt => {
489
+ if (!question.default)
490
+ return false;
491
+ const defaults = Array.isArray(question.default)
492
+ ? question.default
493
+ : [question.default];
494
+ return defaults.includes(opt.name);
495
+ });
449
496
  const updateFilteredOptions = () => {
450
497
  filteredOptions = this.filterOptions(options, input);
451
498
  };
@@ -517,7 +564,8 @@ export class Inquirerer {
517
564
  options.forEach((option, index) => {
518
565
  result.push({
519
566
  name: option.name,
520
- value: selections[index]
567
+ value: option.value,
568
+ selected: selections[index]
521
569
  });
522
570
  });
523
571
  }
@@ -527,7 +575,8 @@ export class Inquirerer {
527
575
  if (selections[index]) {
528
576
  result.push({
529
577
  name: option.name,
530
- value: selections[index]
578
+ value: option.value,
579
+ selected: selections[index]
531
580
  });
532
581
  }
533
582
  });
@@ -627,6 +676,83 @@ export class Inquirerer {
627
676
  });
628
677
  });
629
678
  }
679
+ async list(question, ctx) {
680
+ if (this.noTty || !this.rl) {
681
+ if ('default' in question) {
682
+ return question.default;
683
+ }
684
+ return;
685
+ }
686
+ if (!question.options.length) {
687
+ throw new Error('list requires options');
688
+ }
689
+ this.keypress.resume();
690
+ const options = this.sanitizeOptions(question);
691
+ let input = '';
692
+ let selectedIndex = 0;
693
+ let startIndex = 0; // Start index for visible options
694
+ const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
695
+ const display = () => {
696
+ this.clearScreen();
697
+ this.displayPrompt(question, ctx, input);
698
+ // Determine the range of options to display
699
+ const endIndex = Math.min(startIndex + maxLines, options.length);
700
+ for (let i = startIndex; i < endIndex; i++) {
701
+ const option = options[i];
702
+ if (!option) {
703
+ this.log('No options'); // sometimes user searches and there are no options...
704
+ }
705
+ else if (i === selectedIndex) {
706
+ this.log(chalk.blue('> ' + option.name)); // Highlight the selected option with chalk
707
+ }
708
+ else {
709
+ this.log(' ' + option.name);
710
+ }
711
+ }
712
+ };
713
+ display();
714
+ // // Handling these because otherwise they're bound by OTHER methods! memory leak.
715
+ // // e.g. you have checkbox before, then you have a list.... if you don't also do keys and backspace
716
+ // // it will start to display the closure from the
717
+ // // @ts-ignore
718
+ // // this.keypress.input.removeAllListeners('data');
719
+ // // Handling BACKSPACE key
720
+ // this.keypress.off(KEY_CODES.BACKSPACE);
721
+ // // Register alphanumeric and space keypresses to accumulate input
722
+ // 'abcdefghijklmnopqrstuvwxyz0123456789 '.split('').forEach(char => {
723
+ // this.keypress.off(char);
724
+ // });
725
+ // this.keypress.on(KEY_CODES.SPACE, () => {
726
+ // display();
727
+ // });
728
+ // Navigation
729
+ this.keypress.on(KEY_CODES.UP_ARROW, () => {
730
+ selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
731
+ if (selectedIndex < startIndex) {
732
+ startIndex = selectedIndex; // Scroll up
733
+ }
734
+ else if (selectedIndex === options.length - 1) {
735
+ startIndex = Math.max(0, options.length - maxLines); // Jump to the bottom of the list
736
+ }
737
+ display();
738
+ });
739
+ this.keypress.on(KEY_CODES.DOWN_ARROW, () => {
740
+ selectedIndex = (selectedIndex + 1) % options.length;
741
+ if (selectedIndex >= startIndex + maxLines) {
742
+ startIndex = selectedIndex - maxLines + 1; // Scroll down
743
+ }
744
+ else if (selectedIndex === 0) {
745
+ startIndex = 0; // Jump to the top of the list
746
+ }
747
+ display();
748
+ });
749
+ return new Promise(resolve => {
750
+ this.keypress.on(KEY_CODES.ENTER, () => {
751
+ this.keypress.pause();
752
+ resolve(options[selectedIndex]?.value || input);
753
+ });
754
+ });
755
+ }
630
756
  getOptionValue(option) {
631
757
  if (typeof option === 'string') {
632
758
  return { name: option, value: option };
package/keypress.d.ts CHANGED
@@ -26,6 +26,7 @@ export declare class TerminalKeypress {
26
26
  private setupListeners;
27
27
  on(key: string, callback: KeyHandler): void;
28
28
  off(key: string, callback: KeyHandler): void;
29
+ clearHandlers(): void;
29
30
  pause(): void;
30
31
  resume(): void;
31
32
  destroy(): void;
package/keypress.js CHANGED
@@ -62,6 +62,9 @@ class TerminalKeypress {
62
62
  }
63
63
  }
64
64
  }
65
+ clearHandlers() {
66
+ this.listeners = {};
67
+ }
65
68
  pause() {
66
69
  this.active = false;
67
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "1.9.1",
3
+ "version": "2.0.1",
4
4
  "author": "Dan Lynch <pyramation@gmail.com>",
5
5
  "description": "inquirerer",
6
6
  "main": "index.js",
@@ -35,5 +35,5 @@
35
35
  "minimist": "^1.2.8"
36
36
  },
37
37
  "keywords": [],
38
- "gitHead": "8cd37ae36d4a3ae76cc31f47bcbc15fd8a6c8c84"
38
+ "gitHead": "b20090e1514228a9010745404b3d17e7716f054b"
39
39
  }
package/prompt.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { Readable, Writable } from 'stream';
3
- import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, NumberQuestion, OptionValue, Question, TextQuestion, Validation } from './question';
3
+ import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, ListQuestion, NumberQuestion, OptionValue, Question, TextQuestion, Validation } from './question';
4
4
  export interface ManPageInfo {
5
5
  commandName: string;
6
6
  questions: Question[];
@@ -66,6 +66,7 @@ export declare class Inquirerer {
66
66
  number(question: NumberQuestion, ctx: PromptContext): Promise<number | null>;
67
67
  checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<OptionValue[]>;
68
68
  autocomplete(question: AutocompleteQuestion, ctx: PromptContext): Promise<any>;
69
+ list(question: ListQuestion, ctx: PromptContext): Promise<any>;
69
70
  private getOptionValue;
70
71
  private sanitizeOptions;
71
72
  private filterOptions;
package/prompt.js CHANGED
@@ -74,7 +74,13 @@ function generatePromptMessage(question, ctx) {
74
74
  promptMessage += ` [${question.default}]`;
75
75
  }
76
76
  break;
77
+ case 'number':
78
+ if (question.default) {
79
+ promptMessage += ` [${question.default}]`;
80
+ }
81
+ break;
77
82
  case 'autocomplete':
83
+ case 'list':
78
84
  case 'checkbox':
79
85
  // For these types, you might want to show the default selected options if any
80
86
  if (question.options && question.default) {
@@ -281,6 +287,7 @@ class Inquirerer {
281
287
  }
282
288
  // Apply default value if applicable
283
289
  // this is if useDefault is set, rare! not typical defaults which happen AFTER
290
+ // this is mostly to avoid a prompt for "hidden" options
284
291
  if ('default' in question && (this.useDefaults || question.useDefault)) {
285
292
  obj[question.name] = question.default;
286
293
  continue; // Skip to the next question since the default is applied
@@ -333,11 +340,13 @@ class Inquirerer {
333
340
  handleOverrides(argv, obj, question) {
334
341
  switch (question.type) {
335
342
  case 'text':
343
+ case 'number':
336
344
  case 'confirm':
337
345
  // do nothing, already set!
338
346
  break;
339
347
  case 'checkbox':
340
348
  case 'autocomplete':
349
+ case 'list':
341
350
  // get the value from options :)
342
351
  this.handleOverridesWithOptions(argv, obj, question);
343
352
  break;
@@ -356,16 +365,20 @@ class Inquirerer {
356
365
  }
357
366
  }
358
367
  async handleQuestionType(question, ctx) {
368
+ this.keypress.clearHandlers();
359
369
  switch (question.type) {
360
370
  case 'confirm':
361
371
  return this.confirm(question, ctx);
362
372
  case 'checkbox':
363
373
  return this.checkbox(question, ctx);
374
+ case 'list':
375
+ return this.list(question, ctx);
364
376
  case 'autocomplete':
365
377
  return this.autocomplete(question, ctx);
366
378
  case 'number':
367
379
  return this.number(question, ctx);
368
380
  case 'text':
381
+ return this.text(question, ctx);
369
382
  default:
370
383
  return this.text(question, ctx);
371
384
  }
@@ -404,6 +417,9 @@ class Inquirerer {
404
417
  if (input.trim() !== '') {
405
418
  resolve(input); // Return input if not empty
406
419
  }
420
+ else if ('default' in question) {
421
+ resolve(question.default); // Use default if input is empty
422
+ }
407
423
  else {
408
424
  resolve(null); // Return null if empty and not required
409
425
  }
@@ -428,18 +444,41 @@ class Inquirerer {
428
444
  resolve(num);
429
445
  }
430
446
  else {
431
- resolve(null);
447
+ resolve(null); // Let validation handle bad input
432
448
  }
433
449
  }
450
+ else if ('default' in question) {
451
+ resolve(question.default); // Use default if input is empty
452
+ }
434
453
  else {
435
- resolve(null);
454
+ resolve(null); // Empty and no default
436
455
  }
437
456
  });
438
457
  });
439
458
  }
440
459
  async checkbox(question, ctx) {
441
- if (this.noTty || !this.rl)
442
- return question.default ?? []; // Return default if non-interactive
460
+ if (this.noTty || !this.rl) {
461
+ const options = this.sanitizeOptions(question);
462
+ const defaults = Array.isArray(question.default)
463
+ ? question.default
464
+ : [question.default];
465
+ // If returnFullResults is true, return all options with boolean selection
466
+ if (question.returnFullResults) {
467
+ return options.map(opt => ({
468
+ name: opt.name,
469
+ value: opt.value,
470
+ selected: defaults.includes(opt.name)
471
+ }));
472
+ }
473
+ // Otherwise, return only selected options
474
+ return options
475
+ .filter(opt => defaults.includes(opt.name) || defaults.includes(opt.value))
476
+ .map(opt => ({
477
+ name: opt.name,
478
+ value: opt.value,
479
+ selected: true
480
+ }));
481
+ }
443
482
  if (!question.options.length) {
444
483
  // no arguments don't make sense
445
484
  throw new Error('checkbox requires options');
@@ -451,7 +490,15 @@ class Inquirerer {
451
490
  let selectedIndex = 0;
452
491
  let startIndex = 0; // Start index for visible options
453
492
  const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
454
- const selections = new Array(options.length).fill(false);
493
+ // const selections: boolean[] = new Array(options.length).fill(false);
494
+ const selections = options.map(opt => {
495
+ if (!question.default)
496
+ return false;
497
+ const defaults = Array.isArray(question.default)
498
+ ? question.default
499
+ : [question.default];
500
+ return defaults.includes(opt.name);
501
+ });
455
502
  const updateFilteredOptions = () => {
456
503
  filteredOptions = this.filterOptions(options, input);
457
504
  };
@@ -523,7 +570,8 @@ class Inquirerer {
523
570
  options.forEach((option, index) => {
524
571
  result.push({
525
572
  name: option.name,
526
- value: selections[index]
573
+ value: option.value,
574
+ selected: selections[index]
527
575
  });
528
576
  });
529
577
  }
@@ -533,7 +581,8 @@ class Inquirerer {
533
581
  if (selections[index]) {
534
582
  result.push({
535
583
  name: option.name,
536
- value: selections[index]
584
+ value: option.value,
585
+ selected: selections[index]
537
586
  });
538
587
  }
539
588
  });
@@ -633,6 +682,83 @@ class Inquirerer {
633
682
  });
634
683
  });
635
684
  }
685
+ async list(question, ctx) {
686
+ if (this.noTty || !this.rl) {
687
+ if ('default' in question) {
688
+ return question.default;
689
+ }
690
+ return;
691
+ }
692
+ if (!question.options.length) {
693
+ throw new Error('list requires options');
694
+ }
695
+ this.keypress.resume();
696
+ const options = this.sanitizeOptions(question);
697
+ let input = '';
698
+ let selectedIndex = 0;
699
+ let startIndex = 0; // Start index for visible options
700
+ const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
701
+ const display = () => {
702
+ this.clearScreen();
703
+ this.displayPrompt(question, ctx, input);
704
+ // Determine the range of options to display
705
+ const endIndex = Math.min(startIndex + maxLines, options.length);
706
+ for (let i = startIndex; i < endIndex; i++) {
707
+ const option = options[i];
708
+ if (!option) {
709
+ this.log('No options'); // sometimes user searches and there are no options...
710
+ }
711
+ else if (i === selectedIndex) {
712
+ this.log(chalk_1.default.blue('> ' + option.name)); // Highlight the selected option with chalk
713
+ }
714
+ else {
715
+ this.log(' ' + option.name);
716
+ }
717
+ }
718
+ };
719
+ display();
720
+ // // Handling these because otherwise they're bound by OTHER methods! memory leak.
721
+ // // e.g. you have checkbox before, then you have a list.... if you don't also do keys and backspace
722
+ // // it will start to display the closure from the
723
+ // // @ts-ignore
724
+ // // this.keypress.input.removeAllListeners('data');
725
+ // // Handling BACKSPACE key
726
+ // this.keypress.off(KEY_CODES.BACKSPACE);
727
+ // // Register alphanumeric and space keypresses to accumulate input
728
+ // 'abcdefghijklmnopqrstuvwxyz0123456789 '.split('').forEach(char => {
729
+ // this.keypress.off(char);
730
+ // });
731
+ // this.keypress.on(KEY_CODES.SPACE, () => {
732
+ // display();
733
+ // });
734
+ // Navigation
735
+ this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
736
+ selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
737
+ if (selectedIndex < startIndex) {
738
+ startIndex = selectedIndex; // Scroll up
739
+ }
740
+ else if (selectedIndex === options.length - 1) {
741
+ startIndex = Math.max(0, options.length - maxLines); // Jump to the bottom of the list
742
+ }
743
+ display();
744
+ });
745
+ this.keypress.on(keypress_1.KEY_CODES.DOWN_ARROW, () => {
746
+ selectedIndex = (selectedIndex + 1) % options.length;
747
+ if (selectedIndex >= startIndex + maxLines) {
748
+ startIndex = selectedIndex - maxLines + 1; // Scroll down
749
+ }
750
+ else if (selectedIndex === 0) {
751
+ startIndex = 0; // Jump to the top of the list
752
+ }
753
+ display();
754
+ });
755
+ return new Promise(resolve => {
756
+ this.keypress.on(keypress_1.KEY_CODES.ENTER, () => {
757
+ this.keypress.pause();
758
+ resolve(options[selectedIndex]?.value || input);
759
+ });
760
+ });
761
+ }
636
762
  getOptionValue(option) {
637
763
  if (typeof option === 'string') {
638
764
  return { name: option, value: option };
@@ -5,6 +5,7 @@ export interface Value {
5
5
  export interface OptionValue {
6
6
  name: string;
7
7
  value: any;
8
+ selected?: boolean;
8
9
  }
9
10
  export interface Validation {
10
11
  type?: string;
@@ -34,12 +35,19 @@ export interface AutocompleteQuestion extends BaseQuestion {
34
35
  returnFullResults?: boolean;
35
36
  allowCustomOptions?: boolean;
36
37
  }
38
+ export interface ListQuestion extends BaseQuestion {
39
+ type: 'list';
40
+ options: (string | OptionValue)[];
41
+ maxDisplayLines?: number;
42
+ returnFullResults?: boolean;
43
+ allowCustomOptions?: boolean;
44
+ }
37
45
  export interface CheckboxQuestion extends BaseQuestion {
38
46
  type: 'checkbox';
39
47
  options: (string | OptionValue)[];
40
48
  maxDisplayLines?: number;
41
49
  returnFullResults?: boolean;
42
- default?: Value[];
50
+ default?: string[];
43
51
  }
44
52
  export interface TextQuestion extends BaseQuestion {
45
53
  type: 'text';
@@ -49,4 +57,4 @@ export interface NumberQuestion extends BaseQuestion {
49
57
  type: 'number';
50
58
  default?: number;
51
59
  }
52
- export type Question = ConfirmQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion;
60
+ export type Question = ConfirmQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion;