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