inquirerer 2.0.0 → 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
@@ -359,12 +359,14 @@ export class Inquirerer {
359
359
  }
360
360
  }
361
361
  async handleQuestionType(question, ctx) {
362
+ this.keypress.clearHandlers();
362
363
  switch (question.type) {
363
364
  case 'confirm':
364
365
  return this.confirm(question, ctx);
365
366
  case 'checkbox':
366
367
  return this.checkbox(question, ctx);
367
368
  case 'list':
369
+ return this.list(question, ctx);
368
370
  case 'autocomplete':
369
371
  return this.autocomplete(question, ctx);
370
372
  case 'number':
@@ -674,6 +676,83 @@ export class Inquirerer {
674
676
  });
675
677
  });
676
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
+ }
677
756
  getOptionValue(option) {
678
757
  if (typeof option === 'string') {
679
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": "2.0.0",
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": "aa6ac282c2a3019fef455f0c45e5cad1bbdeb53d"
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
@@ -365,12 +365,14 @@ class Inquirerer {
365
365
  }
366
366
  }
367
367
  async handleQuestionType(question, ctx) {
368
+ this.keypress.clearHandlers();
368
369
  switch (question.type) {
369
370
  case 'confirm':
370
371
  return this.confirm(question, ctx);
371
372
  case 'checkbox':
372
373
  return this.checkbox(question, ctx);
373
374
  case 'list':
375
+ return this.list(question, ctx);
374
376
  case 'autocomplete':
375
377
  return this.autocomplete(question, ctx);
376
378
  case 'number':
@@ -680,6 +682,83 @@ class Inquirerer {
680
682
  });
681
683
  });
682
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
+ }
683
762
  getOptionValue(option) {
684
763
  if (typeof option === 'string') {
685
764
  return { name: option, value: option };