inquirerer 0.8.0 → 0.9.0
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/prompt.js +87 -38
- package/package.json +2 -2
- package/prompt.d.ts +14 -4
- package/prompt.js +87 -38
- package/question/types.d.ts +6 -2
package/esm/prompt.js
CHANGED
|
@@ -3,7 +3,8 @@ import readline from 'readline';
|
|
|
3
3
|
import { KEY_CODES, TerminalKeypress } from './keypress';
|
|
4
4
|
const requiredMessage = (question) => chalk.red(`The field "${question.name}" is required. Please provide a value.\n`);
|
|
5
5
|
function generatePromptMessage(question, ctx) {
|
|
6
|
-
let promptMessage = question.message ?
|
|
6
|
+
let promptMessage = question.message ? question.message : question.name;
|
|
7
|
+
promptMessage = `${chalk.white('[')}${chalk.green('--' + promptMessage)}${chalk.white(']:')}\n`;
|
|
7
8
|
if (ctx.numTries > 0 && question.required) {
|
|
8
9
|
promptMessage = requiredMessage(question) + promptMessage;
|
|
9
10
|
}
|
|
@@ -33,16 +34,26 @@ export class Inquirerer {
|
|
|
33
34
|
keypress;
|
|
34
35
|
noTty;
|
|
35
36
|
output;
|
|
37
|
+
input;
|
|
36
38
|
useDefaults;
|
|
39
|
+
globalMaxLines;
|
|
37
40
|
constructor(options) {
|
|
38
|
-
const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false } = options ?? {};
|
|
41
|
+
const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false, globalMaxLines = 10 } = options ?? {};
|
|
39
42
|
this.useDefaults = useDefaults;
|
|
40
43
|
this.noTty = noTty;
|
|
41
44
|
this.output = output;
|
|
45
|
+
this.input = input;
|
|
46
|
+
this.globalMaxLines = globalMaxLines;
|
|
42
47
|
if (!noTty) {
|
|
43
48
|
this.rl = readline.createInterface({
|
|
44
49
|
input,
|
|
50
|
+
// dissallow readline from prompting user, since we'll handle it!
|
|
45
51
|
output
|
|
52
|
+
// : new Writable({
|
|
53
|
+
// write(chunk, encoding, callback) {
|
|
54
|
+
// callback(); // Do nothing with the data
|
|
55
|
+
// }
|
|
56
|
+
// })
|
|
46
57
|
});
|
|
47
58
|
this.keypress = new TerminalKeypress(noTty, input);
|
|
48
59
|
}
|
|
@@ -60,6 +71,17 @@ export class Inquirerer {
|
|
|
60
71
|
log(message) {
|
|
61
72
|
this.output.write(message + '\n');
|
|
62
73
|
}
|
|
74
|
+
getInput(input) {
|
|
75
|
+
return `${chalk.white.bold('$')} ${input}`;
|
|
76
|
+
}
|
|
77
|
+
getPrompt(question, ctx, input) {
|
|
78
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
79
|
+
return promptMessage + this.getInput(input);
|
|
80
|
+
}
|
|
81
|
+
displayPrompt(question, ctx, input) {
|
|
82
|
+
const prompt = this.getPrompt(question, ctx, input);
|
|
83
|
+
this.log(prompt);
|
|
84
|
+
}
|
|
63
85
|
async prompt(params, questions, usageText) {
|
|
64
86
|
const obj = { ...params };
|
|
65
87
|
// when interactive and missing a bunch of stuff, we should display to the user
|
|
@@ -77,12 +99,9 @@ export class Inquirerer {
|
|
|
77
99
|
let numTries = 0;
|
|
78
100
|
while (index < questions.length) {
|
|
79
101
|
const question = questions[index];
|
|
80
|
-
if ('default' in question && this.useDefaults) {
|
|
81
|
-
obj[question.name] = question.default;
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
if ('default' in question && question.useDefault) {
|
|
102
|
+
if ('default' in question && (this.useDefaults || question.useDefault)) {
|
|
85
103
|
obj[question.name] = question.default;
|
|
104
|
+
index++; // Move to the next question
|
|
86
105
|
continue;
|
|
87
106
|
}
|
|
88
107
|
const ctx = {
|
|
@@ -126,9 +145,8 @@ export class Inquirerer {
|
|
|
126
145
|
if (this.noTty || !this.rl)
|
|
127
146
|
return question.default ?? false; // Return default if non-interactive
|
|
128
147
|
return new Promise((resolve) => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
this.rl.question(promptMessage, (answer) => {
|
|
148
|
+
this.clearScreen();
|
|
149
|
+
this.rl.question(this.getPrompt(question, ctx, ''), (answer) => {
|
|
132
150
|
const userInput = answer.trim().toLowerCase();
|
|
133
151
|
if (userInput === '') {
|
|
134
152
|
resolve(question.default ?? false); // Use default value if input is empty
|
|
@@ -145,14 +163,13 @@ export class Inquirerer {
|
|
|
145
163
|
async text(question, ctx) {
|
|
146
164
|
if (this.noTty || !this.rl)
|
|
147
165
|
return question.default ?? null; // Return default if non-interactive
|
|
148
|
-
let
|
|
166
|
+
let input = '';
|
|
149
167
|
return new Promise((resolve) => {
|
|
150
|
-
this.clearScreen();
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
resolve(userInput); // Return input if not empty
|
|
168
|
+
this.clearScreen();
|
|
169
|
+
this.rl.question(this.getPrompt(question, ctx, input), (answer) => {
|
|
170
|
+
input = answer;
|
|
171
|
+
if (input.trim() !== '') {
|
|
172
|
+
resolve(input); // Return input if not empty
|
|
156
173
|
}
|
|
157
174
|
else {
|
|
158
175
|
resolve(null); // Return null if empty and not required
|
|
@@ -164,29 +181,33 @@ export class Inquirerer {
|
|
|
164
181
|
if (this.noTty || !this.rl)
|
|
165
182
|
return question.default ?? []; // Return default if non-interactive
|
|
166
183
|
this.keypress.resume();
|
|
167
|
-
const options = question.options
|
|
184
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
168
185
|
let input = ''; // Search input
|
|
169
186
|
let filteredOptions = options;
|
|
170
187
|
let selectedIndex = 0;
|
|
171
188
|
let startIndex = 0; // Start index for visible options
|
|
172
|
-
const maxLines = question
|
|
189
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
173
190
|
const selections = new Array(options.length).fill(false);
|
|
174
191
|
const updateFilteredOptions = () => {
|
|
175
|
-
filteredOptions = options.filter(option => option.toLowerCase().includes(input.toLowerCase()));
|
|
192
|
+
filteredOptions = options.filter(option => option.name.toLowerCase().includes(input.toLowerCase()));
|
|
176
193
|
};
|
|
177
194
|
const display = () => {
|
|
178
195
|
this.clearScreen();
|
|
179
|
-
|
|
180
|
-
this.write(promptMessage);
|
|
181
|
-
this.log(`${input}`);
|
|
196
|
+
this.displayPrompt(question, ctx, input);
|
|
182
197
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
183
198
|
for (let i = startIndex; i < endIndex; i++) {
|
|
184
199
|
const option = filteredOptions[i];
|
|
185
200
|
const isSelected = selectedIndex === i;
|
|
186
201
|
const marker = isSelected ? '>' : ' ';
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
202
|
+
const index = options.map(o => o.name).indexOf(option.name);
|
|
203
|
+
if (index >= 0) {
|
|
204
|
+
const isChecked = selections[index] ? '◉' : '○'; // Use the original index in options
|
|
205
|
+
const line = `${marker} ${isChecked} ${option.name}`;
|
|
206
|
+
this.log(isSelected ? chalk.blue(line) : line);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
this.log('No options'); // sometimes user searches and there are no options...
|
|
210
|
+
}
|
|
190
211
|
}
|
|
191
212
|
};
|
|
192
213
|
display();
|
|
@@ -237,7 +258,7 @@ export class Inquirerer {
|
|
|
237
258
|
// Return all options with their selected status
|
|
238
259
|
options.forEach((option, index) => {
|
|
239
260
|
result.push({
|
|
240
|
-
name: option,
|
|
261
|
+
name: option.name,
|
|
241
262
|
value: selections[index]
|
|
242
263
|
});
|
|
243
264
|
});
|
|
@@ -247,7 +268,7 @@ export class Inquirerer {
|
|
|
247
268
|
options.forEach((option, index) => {
|
|
248
269
|
if (selections[index]) {
|
|
249
270
|
result.push({
|
|
250
|
-
name: option,
|
|
271
|
+
name: option.name,
|
|
251
272
|
value: selections[index]
|
|
252
273
|
});
|
|
253
274
|
}
|
|
@@ -257,29 +278,39 @@ export class Inquirerer {
|
|
|
257
278
|
});
|
|
258
279
|
});
|
|
259
280
|
}
|
|
281
|
+
getOptionValue(option) {
|
|
282
|
+
if (typeof option === 'string') {
|
|
283
|
+
return { name: option, value: option };
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
return { name: option.name, value: option.value };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
260
289
|
async autocomplete(question, ctx) {
|
|
261
290
|
if (this.noTty || !this.rl)
|
|
262
291
|
return question.default ?? false; // Return default if non-interactive
|
|
263
292
|
this.keypress.resume();
|
|
264
|
-
const options = question.options
|
|
293
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
265
294
|
let input = '';
|
|
266
295
|
let filteredOptions = options;
|
|
267
296
|
let selectedIndex = 0;
|
|
268
297
|
let startIndex = 0; // Start index for visible options
|
|
269
|
-
const maxLines = question
|
|
298
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
270
299
|
const display = () => {
|
|
271
300
|
this.clearScreen();
|
|
272
|
-
|
|
273
|
-
this.log(promptMessage);
|
|
301
|
+
this.displayPrompt(question, ctx, input);
|
|
274
302
|
// Determine the range of options to display
|
|
275
303
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
276
304
|
for (let i = startIndex; i < endIndex; i++) {
|
|
277
305
|
const option = filteredOptions[i];
|
|
278
|
-
if (
|
|
279
|
-
this.log(
|
|
306
|
+
if (!option) {
|
|
307
|
+
this.log('No options'); // sometimes user searches and there are no options...
|
|
308
|
+
}
|
|
309
|
+
else if (i === selectedIndex) {
|
|
310
|
+
this.log(chalk.blue('> ' + option.name)); // Highlight the selected option with chalk
|
|
280
311
|
}
|
|
281
312
|
else {
|
|
282
|
-
this.log(' ' + option);
|
|
313
|
+
this.log(' ' + option.name);
|
|
283
314
|
}
|
|
284
315
|
}
|
|
285
316
|
};
|
|
@@ -335,14 +366,32 @@ export class Inquirerer {
|
|
|
335
366
|
return new Promise(resolve => {
|
|
336
367
|
this.keypress.on(KEY_CODES.ENTER, () => {
|
|
337
368
|
this.keypress.pause();
|
|
338
|
-
resolve(filteredOptions[selectedIndex] || input);
|
|
369
|
+
resolve(filteredOptions[selectedIndex]?.value || input);
|
|
339
370
|
});
|
|
340
371
|
});
|
|
341
372
|
}
|
|
342
373
|
filterOptions(options, input) {
|
|
343
374
|
return options
|
|
344
|
-
.filter(option => option.toLowerCase().startsWith(input.toLowerCase()))
|
|
345
|
-
.sort()
|
|
375
|
+
.filter(option => option.name.toLowerCase().startsWith(input.toLowerCase()))
|
|
376
|
+
.sort((a, b) => {
|
|
377
|
+
if (a.name < b.name) {
|
|
378
|
+
return -1; // b first
|
|
379
|
+
}
|
|
380
|
+
if (a.name > b.name) {
|
|
381
|
+
return 1; // a is first
|
|
382
|
+
}
|
|
383
|
+
return 0; // equal
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
getMaxLines(question, defaultLength) {
|
|
387
|
+
if (question.maxDisplayLines) {
|
|
388
|
+
return question.maxDisplayLines;
|
|
389
|
+
}
|
|
390
|
+
// if (!this.noTty && (this.output as any).isTTY) {
|
|
391
|
+
// const rows = Math.round(((this.output as any).rows ?? 0) / 7);
|
|
392
|
+
// return Math.max(rows, defaultLength);
|
|
393
|
+
// }
|
|
394
|
+
return Math.min(this.globalMaxLines, defaultLength);
|
|
346
395
|
}
|
|
347
396
|
// Method to cleanly close the readline interface
|
|
348
397
|
close() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
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": "6c687ab6a986a77725b0ada36bc21c18fed28842"
|
|
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, Question, TextQuestion
|
|
3
|
+
import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, OptionValue, Question, TextQuestion } from './question';
|
|
4
4
|
interface PromptContext {
|
|
5
5
|
numTries: number;
|
|
6
6
|
}
|
|
@@ -9,23 +9,33 @@ export interface InquirererOptions {
|
|
|
9
9
|
input?: Readable;
|
|
10
10
|
output?: Writable;
|
|
11
11
|
useDefaults?: boolean;
|
|
12
|
+
globalMaxLines?: number;
|
|
12
13
|
}
|
|
13
14
|
export declare class Inquirerer {
|
|
14
15
|
private rl;
|
|
15
16
|
private keypress;
|
|
16
17
|
private noTty;
|
|
17
18
|
private output;
|
|
19
|
+
private input;
|
|
18
20
|
private useDefaults;
|
|
21
|
+
private globalMaxLines;
|
|
19
22
|
constructor(options?: InquirererOptions);
|
|
20
23
|
clearScreen(): void;
|
|
21
24
|
write(message: string): void;
|
|
22
25
|
log(message: string): void;
|
|
26
|
+
getInput(input: string): string;
|
|
27
|
+
getPrompt(question: Question, ctx: PromptContext, input: string): string;
|
|
28
|
+
displayPrompt(question: Question, ctx: PromptContext, input: string): void;
|
|
23
29
|
prompt<T extends object>(params: T, questions: Question[], usageText?: string): Promise<T>;
|
|
24
30
|
confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
|
|
25
31
|
text(question: TextQuestion, ctx: PromptContext): Promise<string | null>;
|
|
26
|
-
checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<OptionValue[]>;
|
|
33
|
+
private getOptionValue;
|
|
34
|
+
autocomplete(question: AutocompleteQuestion, ctx: PromptContext): Promise<any>;
|
|
35
|
+
filterOptions(options: OptionValue[], input: string): OptionValue[];
|
|
36
|
+
getMaxLines(question: {
|
|
37
|
+
maxDisplayLines?: number;
|
|
38
|
+
}, defaultLength: number): number;
|
|
29
39
|
close(): void;
|
|
30
40
|
}
|
|
31
41
|
export {};
|
package/prompt.js
CHANGED
|
@@ -9,7 +9,8 @@ const readline_1 = __importDefault(require("readline"));
|
|
|
9
9
|
const keypress_1 = require("./keypress");
|
|
10
10
|
const requiredMessage = (question) => chalk_1.default.red(`The field "${question.name}" is required. Please provide a value.\n`);
|
|
11
11
|
function generatePromptMessage(question, ctx) {
|
|
12
|
-
let promptMessage = question.message ?
|
|
12
|
+
let promptMessage = question.message ? question.message : question.name;
|
|
13
|
+
promptMessage = `${chalk_1.default.white('[')}${chalk_1.default.green('--' + promptMessage)}${chalk_1.default.white(']:')}\n`;
|
|
13
14
|
if (ctx.numTries > 0 && question.required) {
|
|
14
15
|
promptMessage = requiredMessage(question) + promptMessage;
|
|
15
16
|
}
|
|
@@ -39,16 +40,26 @@ class Inquirerer {
|
|
|
39
40
|
keypress;
|
|
40
41
|
noTty;
|
|
41
42
|
output;
|
|
43
|
+
input;
|
|
42
44
|
useDefaults;
|
|
45
|
+
globalMaxLines;
|
|
43
46
|
constructor(options) {
|
|
44
|
-
const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false } = options ?? {};
|
|
47
|
+
const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false, globalMaxLines = 10 } = options ?? {};
|
|
45
48
|
this.useDefaults = useDefaults;
|
|
46
49
|
this.noTty = noTty;
|
|
47
50
|
this.output = output;
|
|
51
|
+
this.input = input;
|
|
52
|
+
this.globalMaxLines = globalMaxLines;
|
|
48
53
|
if (!noTty) {
|
|
49
54
|
this.rl = readline_1.default.createInterface({
|
|
50
55
|
input,
|
|
56
|
+
// dissallow readline from prompting user, since we'll handle it!
|
|
51
57
|
output
|
|
58
|
+
// : new Writable({
|
|
59
|
+
// write(chunk, encoding, callback) {
|
|
60
|
+
// callback(); // Do nothing with the data
|
|
61
|
+
// }
|
|
62
|
+
// })
|
|
52
63
|
});
|
|
53
64
|
this.keypress = new keypress_1.TerminalKeypress(noTty, input);
|
|
54
65
|
}
|
|
@@ -66,6 +77,17 @@ class Inquirerer {
|
|
|
66
77
|
log(message) {
|
|
67
78
|
this.output.write(message + '\n');
|
|
68
79
|
}
|
|
80
|
+
getInput(input) {
|
|
81
|
+
return `${chalk_1.default.white.bold('$')} ${input}`;
|
|
82
|
+
}
|
|
83
|
+
getPrompt(question, ctx, input) {
|
|
84
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
85
|
+
return promptMessage + this.getInput(input);
|
|
86
|
+
}
|
|
87
|
+
displayPrompt(question, ctx, input) {
|
|
88
|
+
const prompt = this.getPrompt(question, ctx, input);
|
|
89
|
+
this.log(prompt);
|
|
90
|
+
}
|
|
69
91
|
async prompt(params, questions, usageText) {
|
|
70
92
|
const obj = { ...params };
|
|
71
93
|
// when interactive and missing a bunch of stuff, we should display to the user
|
|
@@ -83,12 +105,9 @@ class Inquirerer {
|
|
|
83
105
|
let numTries = 0;
|
|
84
106
|
while (index < questions.length) {
|
|
85
107
|
const question = questions[index];
|
|
86
|
-
if ('default' in question && this.useDefaults) {
|
|
87
|
-
obj[question.name] = question.default;
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
if ('default' in question && question.useDefault) {
|
|
108
|
+
if ('default' in question && (this.useDefaults || question.useDefault)) {
|
|
91
109
|
obj[question.name] = question.default;
|
|
110
|
+
index++; // Move to the next question
|
|
92
111
|
continue;
|
|
93
112
|
}
|
|
94
113
|
const ctx = {
|
|
@@ -132,9 +151,8 @@ class Inquirerer {
|
|
|
132
151
|
if (this.noTty || !this.rl)
|
|
133
152
|
return question.default ?? false; // Return default if non-interactive
|
|
134
153
|
return new Promise((resolve) => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
this.rl.question(promptMessage, (answer) => {
|
|
154
|
+
this.clearScreen();
|
|
155
|
+
this.rl.question(this.getPrompt(question, ctx, ''), (answer) => {
|
|
138
156
|
const userInput = answer.trim().toLowerCase();
|
|
139
157
|
if (userInput === '') {
|
|
140
158
|
resolve(question.default ?? false); // Use default value if input is empty
|
|
@@ -151,14 +169,13 @@ class Inquirerer {
|
|
|
151
169
|
async text(question, ctx) {
|
|
152
170
|
if (this.noTty || !this.rl)
|
|
153
171
|
return question.default ?? null; // Return default if non-interactive
|
|
154
|
-
let
|
|
172
|
+
let input = '';
|
|
155
173
|
return new Promise((resolve) => {
|
|
156
|
-
this.clearScreen();
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
resolve(userInput); // Return input if not empty
|
|
174
|
+
this.clearScreen();
|
|
175
|
+
this.rl.question(this.getPrompt(question, ctx, input), (answer) => {
|
|
176
|
+
input = answer;
|
|
177
|
+
if (input.trim() !== '') {
|
|
178
|
+
resolve(input); // Return input if not empty
|
|
162
179
|
}
|
|
163
180
|
else {
|
|
164
181
|
resolve(null); // Return null if empty and not required
|
|
@@ -170,29 +187,33 @@ class Inquirerer {
|
|
|
170
187
|
if (this.noTty || !this.rl)
|
|
171
188
|
return question.default ?? []; // Return default if non-interactive
|
|
172
189
|
this.keypress.resume();
|
|
173
|
-
const options = question.options
|
|
190
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
174
191
|
let input = ''; // Search input
|
|
175
192
|
let filteredOptions = options;
|
|
176
193
|
let selectedIndex = 0;
|
|
177
194
|
let startIndex = 0; // Start index for visible options
|
|
178
|
-
const maxLines = question
|
|
195
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
179
196
|
const selections = new Array(options.length).fill(false);
|
|
180
197
|
const updateFilteredOptions = () => {
|
|
181
|
-
filteredOptions = options.filter(option => option.toLowerCase().includes(input.toLowerCase()));
|
|
198
|
+
filteredOptions = options.filter(option => option.name.toLowerCase().includes(input.toLowerCase()));
|
|
182
199
|
};
|
|
183
200
|
const display = () => {
|
|
184
201
|
this.clearScreen();
|
|
185
|
-
|
|
186
|
-
this.write(promptMessage);
|
|
187
|
-
this.log(`${input}`);
|
|
202
|
+
this.displayPrompt(question, ctx, input);
|
|
188
203
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
189
204
|
for (let i = startIndex; i < endIndex; i++) {
|
|
190
205
|
const option = filteredOptions[i];
|
|
191
206
|
const isSelected = selectedIndex === i;
|
|
192
207
|
const marker = isSelected ? '>' : ' ';
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
208
|
+
const index = options.map(o => o.name).indexOf(option.name);
|
|
209
|
+
if (index >= 0) {
|
|
210
|
+
const isChecked = selections[index] ? '◉' : '○'; // Use the original index in options
|
|
211
|
+
const line = `${marker} ${isChecked} ${option.name}`;
|
|
212
|
+
this.log(isSelected ? chalk_1.default.blue(line) : line);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
this.log('No options'); // sometimes user searches and there are no options...
|
|
216
|
+
}
|
|
196
217
|
}
|
|
197
218
|
};
|
|
198
219
|
display();
|
|
@@ -243,7 +264,7 @@ class Inquirerer {
|
|
|
243
264
|
// Return all options with their selected status
|
|
244
265
|
options.forEach((option, index) => {
|
|
245
266
|
result.push({
|
|
246
|
-
name: option,
|
|
267
|
+
name: option.name,
|
|
247
268
|
value: selections[index]
|
|
248
269
|
});
|
|
249
270
|
});
|
|
@@ -253,7 +274,7 @@ class Inquirerer {
|
|
|
253
274
|
options.forEach((option, index) => {
|
|
254
275
|
if (selections[index]) {
|
|
255
276
|
result.push({
|
|
256
|
-
name: option,
|
|
277
|
+
name: option.name,
|
|
257
278
|
value: selections[index]
|
|
258
279
|
});
|
|
259
280
|
}
|
|
@@ -263,29 +284,39 @@ class Inquirerer {
|
|
|
263
284
|
});
|
|
264
285
|
});
|
|
265
286
|
}
|
|
287
|
+
getOptionValue(option) {
|
|
288
|
+
if (typeof option === 'string') {
|
|
289
|
+
return { name: option, value: option };
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
return { name: option.name, value: option.value };
|
|
293
|
+
}
|
|
294
|
+
}
|
|
266
295
|
async autocomplete(question, ctx) {
|
|
267
296
|
if (this.noTty || !this.rl)
|
|
268
297
|
return question.default ?? false; // Return default if non-interactive
|
|
269
298
|
this.keypress.resume();
|
|
270
|
-
const options = question.options
|
|
299
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
271
300
|
let input = '';
|
|
272
301
|
let filteredOptions = options;
|
|
273
302
|
let selectedIndex = 0;
|
|
274
303
|
let startIndex = 0; // Start index for visible options
|
|
275
|
-
const maxLines = question
|
|
304
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
276
305
|
const display = () => {
|
|
277
306
|
this.clearScreen();
|
|
278
|
-
|
|
279
|
-
this.log(promptMessage);
|
|
307
|
+
this.displayPrompt(question, ctx, input);
|
|
280
308
|
// Determine the range of options to display
|
|
281
309
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
282
310
|
for (let i = startIndex; i < endIndex; i++) {
|
|
283
311
|
const option = filteredOptions[i];
|
|
284
|
-
if (
|
|
285
|
-
this.log(
|
|
312
|
+
if (!option) {
|
|
313
|
+
this.log('No options'); // sometimes user searches and there are no options...
|
|
314
|
+
}
|
|
315
|
+
else if (i === selectedIndex) {
|
|
316
|
+
this.log(chalk_1.default.blue('> ' + option.name)); // Highlight the selected option with chalk
|
|
286
317
|
}
|
|
287
318
|
else {
|
|
288
|
-
this.log(' ' + option);
|
|
319
|
+
this.log(' ' + option.name);
|
|
289
320
|
}
|
|
290
321
|
}
|
|
291
322
|
};
|
|
@@ -341,14 +372,32 @@ class Inquirerer {
|
|
|
341
372
|
return new Promise(resolve => {
|
|
342
373
|
this.keypress.on(keypress_1.KEY_CODES.ENTER, () => {
|
|
343
374
|
this.keypress.pause();
|
|
344
|
-
resolve(filteredOptions[selectedIndex] || input);
|
|
375
|
+
resolve(filteredOptions[selectedIndex]?.value || input);
|
|
345
376
|
});
|
|
346
377
|
});
|
|
347
378
|
}
|
|
348
379
|
filterOptions(options, input) {
|
|
349
380
|
return options
|
|
350
|
-
.filter(option => option.toLowerCase().startsWith(input.toLowerCase()))
|
|
351
|
-
.sort()
|
|
381
|
+
.filter(option => option.name.toLowerCase().startsWith(input.toLowerCase()))
|
|
382
|
+
.sort((a, b) => {
|
|
383
|
+
if (a.name < b.name) {
|
|
384
|
+
return -1; // b first
|
|
385
|
+
}
|
|
386
|
+
if (a.name > b.name) {
|
|
387
|
+
return 1; // a is first
|
|
388
|
+
}
|
|
389
|
+
return 0; // equal
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
getMaxLines(question, defaultLength) {
|
|
393
|
+
if (question.maxDisplayLines) {
|
|
394
|
+
return question.maxDisplayLines;
|
|
395
|
+
}
|
|
396
|
+
// if (!this.noTty && (this.output as any).isTTY) {
|
|
397
|
+
// const rows = Math.round(((this.output as any).rows ?? 0) / 7);
|
|
398
|
+
// return Math.max(rows, defaultLength);
|
|
399
|
+
// }
|
|
400
|
+
return Math.min(this.globalMaxLines, defaultLength);
|
|
352
401
|
}
|
|
353
402
|
// Method to cleanly close the readline interface
|
|
354
403
|
close() {
|
package/question/types.d.ts
CHANGED
|
@@ -2,6 +2,10 @@ export interface Value {
|
|
|
2
2
|
name: string;
|
|
3
3
|
value: boolean;
|
|
4
4
|
}
|
|
5
|
+
export interface OptionValue {
|
|
6
|
+
name: string;
|
|
7
|
+
value: any;
|
|
8
|
+
}
|
|
5
9
|
export interface BaseQuestion {
|
|
6
10
|
name: string;
|
|
7
11
|
type: string;
|
|
@@ -16,14 +20,14 @@ export interface ConfirmQuestion extends BaseQuestion {
|
|
|
16
20
|
}
|
|
17
21
|
export interface AutocompleteQuestion extends BaseQuestion {
|
|
18
22
|
type: 'autocomplete';
|
|
19
|
-
options: string[];
|
|
23
|
+
options: (string | OptionValue)[];
|
|
20
24
|
maxDisplayLines?: number;
|
|
21
25
|
returnFullResults?: boolean;
|
|
22
26
|
allowCustomOptions?: boolean;
|
|
23
27
|
}
|
|
24
28
|
export interface CheckboxQuestion extends BaseQuestion {
|
|
25
29
|
type: 'checkbox';
|
|
26
|
-
options: string[];
|
|
30
|
+
options: (string | OptionValue)[];
|
|
27
31
|
maxDisplayLines?: number;
|
|
28
32
|
returnFullResults?: boolean;
|
|
29
33
|
default?: Value[];
|