inquirerer 2.0.0 → 2.0.2
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 +3 -0
- package/esm/prompt.js +164 -22
- package/keypress.d.ts +1 -0
- package/keypress.js +3 -0
- package/package.json +2 -2
- package/prompt.d.ts +3 -1
- package/prompt.js +164 -22
package/esm/keypress.js
CHANGED
package/esm/prompt.js
CHANGED
|
@@ -52,39 +52,95 @@ class PromptContext {
|
|
|
52
52
|
return this.validation;
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
-
function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
55
|
+
function generatePromptMessageSuccinct(question, ctx) {
|
|
56
|
+
const { message, name, type, default: def, options = [] } = question;
|
|
57
|
+
const lines = [];
|
|
58
|
+
// 1. Main prompt label
|
|
59
|
+
lines.push(chalk.whiteBright.bold(message || `${name}?`));
|
|
60
|
+
// 2. Validation message if applicable
|
|
61
|
+
const validation = validationMessage(question, ctx);
|
|
62
|
+
if (validation) {
|
|
63
|
+
lines.push(validation); // already styled red
|
|
64
|
+
}
|
|
65
|
+
// 3. Append default inline (only if present)
|
|
66
|
+
let inline = '';
|
|
67
|
+
switch (type) {
|
|
63
68
|
case 'confirm':
|
|
64
|
-
|
|
69
|
+
inline = '(y/n)';
|
|
70
|
+
if (def !== undefined) {
|
|
71
|
+
inline += ` ${chalk.yellow(`[${def ? 'y' : 'n'}]`)}`;
|
|
72
|
+
}
|
|
65
73
|
break;
|
|
66
74
|
case 'text':
|
|
67
|
-
|
|
68
|
-
|
|
75
|
+
case 'number':
|
|
76
|
+
if (def !== undefined) {
|
|
77
|
+
inline += `${chalk.yellow(`[${def}]`)}`;
|
|
69
78
|
}
|
|
70
79
|
break;
|
|
80
|
+
case 'autocomplete':
|
|
81
|
+
case 'list':
|
|
82
|
+
case 'checkbox':
|
|
83
|
+
if (def !== undefined) {
|
|
84
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
85
|
+
const rendered = defaults.map(d => chalk.yellow(d)).join(chalk.gray(', '));
|
|
86
|
+
inline += `${chalk.yellow(`[${rendered}]`)}`;
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
if (inline) {
|
|
91
|
+
lines[lines.length - 1] += ' ' + inline; // append to prompt line
|
|
92
|
+
}
|
|
93
|
+
// 4. Final input line
|
|
94
|
+
lines.push('> ');
|
|
95
|
+
return lines.join('\n');
|
|
96
|
+
}
|
|
97
|
+
function generatePromptMessage(question, ctx) {
|
|
98
|
+
const { message, name, type, default: def, options = [], description } = question;
|
|
99
|
+
const lines = [];
|
|
100
|
+
// 1. Title Message
|
|
101
|
+
lines.push(chalk.whiteBright.bold(message || `${name}?`));
|
|
102
|
+
// 2. Optional description below title
|
|
103
|
+
if (description) {
|
|
104
|
+
lines.push(chalk.dim(description));
|
|
105
|
+
}
|
|
106
|
+
// 3. Validation warning (if failed before)
|
|
107
|
+
const validation = validationMessage(question, ctx);
|
|
108
|
+
if (validation) {
|
|
109
|
+
lines.push(validation); // already red-colored
|
|
110
|
+
}
|
|
111
|
+
// 4. Metadata (name/type)
|
|
112
|
+
lines.push(`${chalk.dim('Argument')} ${chalk.green(`--${name}`)} ${chalk.dim('type')} ${chalk.cyan(`[${type}]`)}`);
|
|
113
|
+
// 5. Default value or guidance
|
|
114
|
+
let guidance = '';
|
|
115
|
+
switch (type) {
|
|
116
|
+
case 'confirm':
|
|
117
|
+
guidance = '(y/n)';
|
|
118
|
+
if (def !== undefined) {
|
|
119
|
+
guidance += ` ${chalk.yellow(`[default: ${def ? 'y' : 'n'}]`)}`;
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
case 'text':
|
|
71
123
|
case 'number':
|
|
72
|
-
if (
|
|
73
|
-
|
|
124
|
+
if (def !== undefined) {
|
|
125
|
+
guidance = chalk.yellow(`[default: ${def}]`);
|
|
74
126
|
}
|
|
75
127
|
break;
|
|
76
128
|
case 'autocomplete':
|
|
77
129
|
case 'list':
|
|
78
130
|
case 'checkbox':
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
promptMessage += ` [${defaultText}]`;
|
|
131
|
+
if (def !== undefined) {
|
|
132
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
133
|
+
const rendered = defaults.map(d => chalk.yellow(d)).join(chalk.gray(', '));
|
|
134
|
+
guidance += `${chalk.yellow(`[default: ${rendered}]`)}`;
|
|
84
135
|
}
|
|
85
136
|
break;
|
|
86
137
|
}
|
|
87
|
-
|
|
138
|
+
if (guidance) {
|
|
139
|
+
lines.push(guidance);
|
|
140
|
+
}
|
|
141
|
+
// 6. Final input prompt
|
|
142
|
+
lines.push(chalk.white('> ') + chalk.dim('Your input:'));
|
|
143
|
+
return lines.join('\n') + '\n';
|
|
88
144
|
}
|
|
89
145
|
export class Inquirerer {
|
|
90
146
|
rl;
|
|
@@ -332,6 +388,9 @@ export class Inquirerer {
|
|
|
332
388
|
});
|
|
333
389
|
}
|
|
334
390
|
handleOverrides(argv, obj, question) {
|
|
391
|
+
if (!Object.prototype.hasOwnProperty.call(argv, question.name)) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
335
394
|
switch (question.type) {
|
|
336
395
|
case 'text':
|
|
337
396
|
case 'number':
|
|
@@ -339,6 +398,8 @@ export class Inquirerer {
|
|
|
339
398
|
// do nothing, already set!
|
|
340
399
|
break;
|
|
341
400
|
case 'checkbox':
|
|
401
|
+
this.handleOverridesForCheckboxOptions(argv, obj, question);
|
|
402
|
+
break;
|
|
342
403
|
case 'autocomplete':
|
|
343
404
|
case 'list':
|
|
344
405
|
// get the value from options :)
|
|
@@ -348,23 +409,41 @@ export class Inquirerer {
|
|
|
348
409
|
return;
|
|
349
410
|
}
|
|
350
411
|
}
|
|
412
|
+
handleOverridesForCheckboxOptions(argv, obj, question) {
|
|
413
|
+
const options = this.sanitizeOptions(question);
|
|
414
|
+
const input = argv[question.name];
|
|
415
|
+
if (typeof input === 'string') {
|
|
416
|
+
const found = options.filter(opt => opt.name === input || String(opt.value) === input);
|
|
417
|
+
if (found.length) {
|
|
418
|
+
obj[question.name] = found;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
else if (Array.isArray(input)) {
|
|
422
|
+
const found = options.filter(opt => input.includes(opt.name) || input.includes(String(opt.value)));
|
|
423
|
+
if (found.length) {
|
|
424
|
+
obj[question.name] = found;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
351
428
|
handleOverridesWithOptions(argv, obj, question) {
|
|
352
|
-
|
|
353
|
-
if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
|
|
429
|
+
if (typeof argv[question.name] === 'string') {
|
|
354
430
|
const options = this.sanitizeOptions(question);
|
|
355
|
-
const
|
|
431
|
+
const input = argv[question.name];
|
|
432
|
+
const found = options.find(opt => opt.name === input || String(opt.value) === input);
|
|
356
433
|
if (typeof found !== 'undefined') {
|
|
357
434
|
obj[question.name] = found.value;
|
|
358
435
|
}
|
|
359
436
|
}
|
|
360
437
|
}
|
|
361
438
|
async handleQuestionType(question, ctx) {
|
|
439
|
+
this.keypress.clearHandlers();
|
|
362
440
|
switch (question.type) {
|
|
363
441
|
case 'confirm':
|
|
364
442
|
return this.confirm(question, ctx);
|
|
365
443
|
case 'checkbox':
|
|
366
444
|
return this.checkbox(question, ctx);
|
|
367
445
|
case 'list':
|
|
446
|
+
return this.list(question, ctx);
|
|
368
447
|
case 'autocomplete':
|
|
369
448
|
return this.autocomplete(question, ctx);
|
|
370
449
|
case 'number':
|
|
@@ -674,6 +753,69 @@ export class Inquirerer {
|
|
|
674
753
|
});
|
|
675
754
|
});
|
|
676
755
|
}
|
|
756
|
+
async list(question, ctx) {
|
|
757
|
+
if (this.noTty || !this.rl) {
|
|
758
|
+
if ('default' in question) {
|
|
759
|
+
return question.default;
|
|
760
|
+
}
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
if (!question.options.length) {
|
|
764
|
+
throw new Error('list requires options');
|
|
765
|
+
}
|
|
766
|
+
this.keypress.resume();
|
|
767
|
+
const options = this.sanitizeOptions(question);
|
|
768
|
+
let input = '';
|
|
769
|
+
let selectedIndex = 0;
|
|
770
|
+
let startIndex = 0; // Start index for visible options
|
|
771
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
772
|
+
const display = () => {
|
|
773
|
+
this.clearScreen();
|
|
774
|
+
this.displayPrompt(question, ctx, input);
|
|
775
|
+
// Determine the range of options to display
|
|
776
|
+
const endIndex = Math.min(startIndex + maxLines, options.length);
|
|
777
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
778
|
+
const option = options[i];
|
|
779
|
+
if (!option) {
|
|
780
|
+
this.log('No options'); // sometimes user searches and there are no options...
|
|
781
|
+
}
|
|
782
|
+
else if (i === selectedIndex) {
|
|
783
|
+
this.log(chalk.blue('> ' + option.name)); // Highlight the selected option with chalk
|
|
784
|
+
}
|
|
785
|
+
else {
|
|
786
|
+
this.log(' ' + option.name);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
display();
|
|
791
|
+
// Navigation
|
|
792
|
+
this.keypress.on(KEY_CODES.UP_ARROW, () => {
|
|
793
|
+
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
|
|
794
|
+
if (selectedIndex < startIndex) {
|
|
795
|
+
startIndex = selectedIndex; // Scroll up
|
|
796
|
+
}
|
|
797
|
+
else if (selectedIndex === options.length - 1) {
|
|
798
|
+
startIndex = Math.max(0, options.length - maxLines); // Jump to the bottom of the list
|
|
799
|
+
}
|
|
800
|
+
display();
|
|
801
|
+
});
|
|
802
|
+
this.keypress.on(KEY_CODES.DOWN_ARROW, () => {
|
|
803
|
+
selectedIndex = (selectedIndex + 1) % options.length;
|
|
804
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
805
|
+
startIndex = selectedIndex - maxLines + 1; // Scroll down
|
|
806
|
+
}
|
|
807
|
+
else if (selectedIndex === 0) {
|
|
808
|
+
startIndex = 0; // Jump to the top of the list
|
|
809
|
+
}
|
|
810
|
+
display();
|
|
811
|
+
});
|
|
812
|
+
return new Promise(resolve => {
|
|
813
|
+
this.keypress.on(KEY_CODES.ENTER, () => {
|
|
814
|
+
this.keypress.pause();
|
|
815
|
+
resolve(options[selectedIndex]?.value || input);
|
|
816
|
+
});
|
|
817
|
+
});
|
|
818
|
+
}
|
|
677
819
|
getOptionValue(option) {
|
|
678
820
|
if (typeof option === 'string') {
|
|
679
821
|
return { name: option, value: option };
|
package/keypress.d.ts
CHANGED
package/keypress.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
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": "
|
|
38
|
+
"gitHead": "f98034d733c0a9f3f1b1c9565698362534d2c720"
|
|
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[];
|
|
@@ -59,6 +59,7 @@ export declare class Inquirerer {
|
|
|
59
59
|
private applyDefaultValues;
|
|
60
60
|
private applyOverrides;
|
|
61
61
|
private handleOverrides;
|
|
62
|
+
private handleOverridesForCheckboxOptions;
|
|
62
63
|
private handleOverridesWithOptions;
|
|
63
64
|
private handleQuestionType;
|
|
64
65
|
confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
|
|
@@ -66,6 +67,7 @@ export declare class Inquirerer {
|
|
|
66
67
|
number(question: NumberQuestion, ctx: PromptContext): Promise<number | null>;
|
|
67
68
|
checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<OptionValue[]>;
|
|
68
69
|
autocomplete(question: AutocompleteQuestion, ctx: PromptContext): Promise<any>;
|
|
70
|
+
list(question: ListQuestion, ctx: PromptContext): Promise<any>;
|
|
69
71
|
private getOptionValue;
|
|
70
72
|
private sanitizeOptions;
|
|
71
73
|
private filterOptions;
|
package/prompt.js
CHANGED
|
@@ -58,39 +58,95 @@ class PromptContext {
|
|
|
58
58
|
return this.validation;
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
function generatePromptMessageSuccinct(question, ctx) {
|
|
62
|
+
const { message, name, type, default: def, options = [] } = question;
|
|
63
|
+
const lines = [];
|
|
64
|
+
// 1. Main prompt label
|
|
65
|
+
lines.push(chalk_1.default.whiteBright.bold(message || `${name}?`));
|
|
66
|
+
// 2. Validation message if applicable
|
|
67
|
+
const validation = validationMessage(question, ctx);
|
|
68
|
+
if (validation) {
|
|
69
|
+
lines.push(validation); // already styled red
|
|
70
|
+
}
|
|
71
|
+
// 3. Append default inline (only if present)
|
|
72
|
+
let inline = '';
|
|
73
|
+
switch (type) {
|
|
69
74
|
case 'confirm':
|
|
70
|
-
|
|
75
|
+
inline = '(y/n)';
|
|
76
|
+
if (def !== undefined) {
|
|
77
|
+
inline += ` ${chalk_1.default.yellow(`[${def ? 'y' : 'n'}]`)}`;
|
|
78
|
+
}
|
|
71
79
|
break;
|
|
72
80
|
case 'text':
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
case 'number':
|
|
82
|
+
if (def !== undefined) {
|
|
83
|
+
inline += `${chalk_1.default.yellow(`[${def}]`)}`;
|
|
75
84
|
}
|
|
76
85
|
break;
|
|
86
|
+
case 'autocomplete':
|
|
87
|
+
case 'list':
|
|
88
|
+
case 'checkbox':
|
|
89
|
+
if (def !== undefined) {
|
|
90
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
91
|
+
const rendered = defaults.map(d => chalk_1.default.yellow(d)).join(chalk_1.default.gray(', '));
|
|
92
|
+
inline += `${chalk_1.default.yellow(`[${rendered}]`)}`;
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
if (inline) {
|
|
97
|
+
lines[lines.length - 1] += ' ' + inline; // append to prompt line
|
|
98
|
+
}
|
|
99
|
+
// 4. Final input line
|
|
100
|
+
lines.push('> ');
|
|
101
|
+
return lines.join('\n');
|
|
102
|
+
}
|
|
103
|
+
function generatePromptMessage(question, ctx) {
|
|
104
|
+
const { message, name, type, default: def, options = [], description } = question;
|
|
105
|
+
const lines = [];
|
|
106
|
+
// 1. Title Message
|
|
107
|
+
lines.push(chalk_1.default.whiteBright.bold(message || `${name}?`));
|
|
108
|
+
// 2. Optional description below title
|
|
109
|
+
if (description) {
|
|
110
|
+
lines.push(chalk_1.default.dim(description));
|
|
111
|
+
}
|
|
112
|
+
// 3. Validation warning (if failed before)
|
|
113
|
+
const validation = validationMessage(question, ctx);
|
|
114
|
+
if (validation) {
|
|
115
|
+
lines.push(validation); // already red-colored
|
|
116
|
+
}
|
|
117
|
+
// 4. Metadata (name/type)
|
|
118
|
+
lines.push(`${chalk_1.default.dim('Argument')} ${chalk_1.default.green(`--${name}`)} ${chalk_1.default.dim('type')} ${chalk_1.default.cyan(`[${type}]`)}`);
|
|
119
|
+
// 5. Default value or guidance
|
|
120
|
+
let guidance = '';
|
|
121
|
+
switch (type) {
|
|
122
|
+
case 'confirm':
|
|
123
|
+
guidance = '(y/n)';
|
|
124
|
+
if (def !== undefined) {
|
|
125
|
+
guidance += ` ${chalk_1.default.yellow(`[default: ${def ? 'y' : 'n'}]`)}`;
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
case 'text':
|
|
77
129
|
case 'number':
|
|
78
|
-
if (
|
|
79
|
-
|
|
130
|
+
if (def !== undefined) {
|
|
131
|
+
guidance = chalk_1.default.yellow(`[default: ${def}]`);
|
|
80
132
|
}
|
|
81
133
|
break;
|
|
82
134
|
case 'autocomplete':
|
|
83
135
|
case 'list':
|
|
84
136
|
case 'checkbox':
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
promptMessage += ` [${defaultText}]`;
|
|
137
|
+
if (def !== undefined) {
|
|
138
|
+
const defaults = Array.isArray(def) ? def : [def];
|
|
139
|
+
const rendered = defaults.map(d => chalk_1.default.yellow(d)).join(chalk_1.default.gray(', '));
|
|
140
|
+
guidance += `${chalk_1.default.yellow(`[default: ${rendered}]`)}`;
|
|
90
141
|
}
|
|
91
142
|
break;
|
|
92
143
|
}
|
|
93
|
-
|
|
144
|
+
if (guidance) {
|
|
145
|
+
lines.push(guidance);
|
|
146
|
+
}
|
|
147
|
+
// 6. Final input prompt
|
|
148
|
+
lines.push(chalk_1.default.white('> ') + chalk_1.default.dim('Your input:'));
|
|
149
|
+
return lines.join('\n') + '\n';
|
|
94
150
|
}
|
|
95
151
|
class Inquirerer {
|
|
96
152
|
rl;
|
|
@@ -338,6 +394,9 @@ class Inquirerer {
|
|
|
338
394
|
});
|
|
339
395
|
}
|
|
340
396
|
handleOverrides(argv, obj, question) {
|
|
397
|
+
if (!Object.prototype.hasOwnProperty.call(argv, question.name)) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
341
400
|
switch (question.type) {
|
|
342
401
|
case 'text':
|
|
343
402
|
case 'number':
|
|
@@ -345,6 +404,8 @@ class Inquirerer {
|
|
|
345
404
|
// do nothing, already set!
|
|
346
405
|
break;
|
|
347
406
|
case 'checkbox':
|
|
407
|
+
this.handleOverridesForCheckboxOptions(argv, obj, question);
|
|
408
|
+
break;
|
|
348
409
|
case 'autocomplete':
|
|
349
410
|
case 'list':
|
|
350
411
|
// get the value from options :)
|
|
@@ -354,23 +415,41 @@ class Inquirerer {
|
|
|
354
415
|
return;
|
|
355
416
|
}
|
|
356
417
|
}
|
|
418
|
+
handleOverridesForCheckboxOptions(argv, obj, question) {
|
|
419
|
+
const options = this.sanitizeOptions(question);
|
|
420
|
+
const input = argv[question.name];
|
|
421
|
+
if (typeof input === 'string') {
|
|
422
|
+
const found = options.filter(opt => opt.name === input || String(opt.value) === input);
|
|
423
|
+
if (found.length) {
|
|
424
|
+
obj[question.name] = found;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
else if (Array.isArray(input)) {
|
|
428
|
+
const found = options.filter(opt => input.includes(opt.name) || input.includes(String(opt.value)));
|
|
429
|
+
if (found.length) {
|
|
430
|
+
obj[question.name] = found;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
357
434
|
handleOverridesWithOptions(argv, obj, question) {
|
|
358
|
-
|
|
359
|
-
if (Object.prototype.hasOwnProperty.call(argv, question.name) && typeof argv[question.name] === 'string') {
|
|
435
|
+
if (typeof argv[question.name] === 'string') {
|
|
360
436
|
const options = this.sanitizeOptions(question);
|
|
361
|
-
const
|
|
437
|
+
const input = argv[question.name];
|
|
438
|
+
const found = options.find(opt => opt.name === input || String(opt.value) === input);
|
|
362
439
|
if (typeof found !== 'undefined') {
|
|
363
440
|
obj[question.name] = found.value;
|
|
364
441
|
}
|
|
365
442
|
}
|
|
366
443
|
}
|
|
367
444
|
async handleQuestionType(question, ctx) {
|
|
445
|
+
this.keypress.clearHandlers();
|
|
368
446
|
switch (question.type) {
|
|
369
447
|
case 'confirm':
|
|
370
448
|
return this.confirm(question, ctx);
|
|
371
449
|
case 'checkbox':
|
|
372
450
|
return this.checkbox(question, ctx);
|
|
373
451
|
case 'list':
|
|
452
|
+
return this.list(question, ctx);
|
|
374
453
|
case 'autocomplete':
|
|
375
454
|
return this.autocomplete(question, ctx);
|
|
376
455
|
case 'number':
|
|
@@ -680,6 +759,69 @@ class Inquirerer {
|
|
|
680
759
|
});
|
|
681
760
|
});
|
|
682
761
|
}
|
|
762
|
+
async list(question, ctx) {
|
|
763
|
+
if (this.noTty || !this.rl) {
|
|
764
|
+
if ('default' in question) {
|
|
765
|
+
return question.default;
|
|
766
|
+
}
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
if (!question.options.length) {
|
|
770
|
+
throw new Error('list requires options');
|
|
771
|
+
}
|
|
772
|
+
this.keypress.resume();
|
|
773
|
+
const options = this.sanitizeOptions(question);
|
|
774
|
+
let input = '';
|
|
775
|
+
let selectedIndex = 0;
|
|
776
|
+
let startIndex = 0; // Start index for visible options
|
|
777
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
778
|
+
const display = () => {
|
|
779
|
+
this.clearScreen();
|
|
780
|
+
this.displayPrompt(question, ctx, input);
|
|
781
|
+
// Determine the range of options to display
|
|
782
|
+
const endIndex = Math.min(startIndex + maxLines, options.length);
|
|
783
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
784
|
+
const option = options[i];
|
|
785
|
+
if (!option) {
|
|
786
|
+
this.log('No options'); // sometimes user searches and there are no options...
|
|
787
|
+
}
|
|
788
|
+
else if (i === selectedIndex) {
|
|
789
|
+
this.log(chalk_1.default.blue('> ' + option.name)); // Highlight the selected option with chalk
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
this.log(' ' + option.name);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
display();
|
|
797
|
+
// Navigation
|
|
798
|
+
this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
|
|
799
|
+
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
|
|
800
|
+
if (selectedIndex < startIndex) {
|
|
801
|
+
startIndex = selectedIndex; // Scroll up
|
|
802
|
+
}
|
|
803
|
+
else if (selectedIndex === options.length - 1) {
|
|
804
|
+
startIndex = Math.max(0, options.length - maxLines); // Jump to the bottom of the list
|
|
805
|
+
}
|
|
806
|
+
display();
|
|
807
|
+
});
|
|
808
|
+
this.keypress.on(keypress_1.KEY_CODES.DOWN_ARROW, () => {
|
|
809
|
+
selectedIndex = (selectedIndex + 1) % options.length;
|
|
810
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
811
|
+
startIndex = selectedIndex - maxLines + 1; // Scroll down
|
|
812
|
+
}
|
|
813
|
+
else if (selectedIndex === 0) {
|
|
814
|
+
startIndex = 0; // Jump to the top of the list
|
|
815
|
+
}
|
|
816
|
+
display();
|
|
817
|
+
});
|
|
818
|
+
return new Promise(resolve => {
|
|
819
|
+
this.keypress.on(keypress_1.KEY_CODES.ENTER, () => {
|
|
820
|
+
this.keypress.pause();
|
|
821
|
+
resolve(options[selectedIndex]?.value || input);
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
}
|
|
683
825
|
getOptionValue(option) {
|
|
684
826
|
if (typeof option === 'string') {
|
|
685
827
|
return { name: option, value: option };
|