inquirerer 0.8.1 → 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 +85 -33
- package/package.json +2 -2
- package/prompt.d.ts +14 -4
- package/prompt.js +85 -33
- 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
|
|
@@ -123,9 +145,8 @@ export class Inquirerer {
|
|
|
123
145
|
if (this.noTty || !this.rl)
|
|
124
146
|
return question.default ?? false; // Return default if non-interactive
|
|
125
147
|
return new Promise((resolve) => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
this.rl.question(promptMessage, (answer) => {
|
|
148
|
+
this.clearScreen();
|
|
149
|
+
this.rl.question(this.getPrompt(question, ctx, ''), (answer) => {
|
|
129
150
|
const userInput = answer.trim().toLowerCase();
|
|
130
151
|
if (userInput === '') {
|
|
131
152
|
resolve(question.default ?? false); // Use default value if input is empty
|
|
@@ -142,14 +163,13 @@ export class Inquirerer {
|
|
|
142
163
|
async text(question, ctx) {
|
|
143
164
|
if (this.noTty || !this.rl)
|
|
144
165
|
return question.default ?? null; // Return default if non-interactive
|
|
145
|
-
let
|
|
166
|
+
let input = '';
|
|
146
167
|
return new Promise((resolve) => {
|
|
147
|
-
this.clearScreen();
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
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
|
|
153
173
|
}
|
|
154
174
|
else {
|
|
155
175
|
resolve(null); // Return null if empty and not required
|
|
@@ -161,29 +181,33 @@ export class Inquirerer {
|
|
|
161
181
|
if (this.noTty || !this.rl)
|
|
162
182
|
return question.default ?? []; // Return default if non-interactive
|
|
163
183
|
this.keypress.resume();
|
|
164
|
-
const options = question.options
|
|
184
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
165
185
|
let input = ''; // Search input
|
|
166
186
|
let filteredOptions = options;
|
|
167
187
|
let selectedIndex = 0;
|
|
168
188
|
let startIndex = 0; // Start index for visible options
|
|
169
|
-
const maxLines = question
|
|
189
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
170
190
|
const selections = new Array(options.length).fill(false);
|
|
171
191
|
const updateFilteredOptions = () => {
|
|
172
|
-
filteredOptions = options.filter(option => option.toLowerCase().includes(input.toLowerCase()));
|
|
192
|
+
filteredOptions = options.filter(option => option.name.toLowerCase().includes(input.toLowerCase()));
|
|
173
193
|
};
|
|
174
194
|
const display = () => {
|
|
175
195
|
this.clearScreen();
|
|
176
|
-
|
|
177
|
-
this.write(promptMessage);
|
|
178
|
-
this.log(`${input}`);
|
|
196
|
+
this.displayPrompt(question, ctx, input);
|
|
179
197
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
180
198
|
for (let i = startIndex; i < endIndex; i++) {
|
|
181
199
|
const option = filteredOptions[i];
|
|
182
200
|
const isSelected = selectedIndex === i;
|
|
183
201
|
const marker = isSelected ? '>' : ' ';
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
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
|
+
}
|
|
187
211
|
}
|
|
188
212
|
};
|
|
189
213
|
display();
|
|
@@ -234,7 +258,7 @@ export class Inquirerer {
|
|
|
234
258
|
// Return all options with their selected status
|
|
235
259
|
options.forEach((option, index) => {
|
|
236
260
|
result.push({
|
|
237
|
-
name: option,
|
|
261
|
+
name: option.name,
|
|
238
262
|
value: selections[index]
|
|
239
263
|
});
|
|
240
264
|
});
|
|
@@ -244,7 +268,7 @@ export class Inquirerer {
|
|
|
244
268
|
options.forEach((option, index) => {
|
|
245
269
|
if (selections[index]) {
|
|
246
270
|
result.push({
|
|
247
|
-
name: option,
|
|
271
|
+
name: option.name,
|
|
248
272
|
value: selections[index]
|
|
249
273
|
});
|
|
250
274
|
}
|
|
@@ -254,29 +278,39 @@ export class Inquirerer {
|
|
|
254
278
|
});
|
|
255
279
|
});
|
|
256
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
|
+
}
|
|
257
289
|
async autocomplete(question, ctx) {
|
|
258
290
|
if (this.noTty || !this.rl)
|
|
259
291
|
return question.default ?? false; // Return default if non-interactive
|
|
260
292
|
this.keypress.resume();
|
|
261
|
-
const options = question.options
|
|
293
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
262
294
|
let input = '';
|
|
263
295
|
let filteredOptions = options;
|
|
264
296
|
let selectedIndex = 0;
|
|
265
297
|
let startIndex = 0; // Start index for visible options
|
|
266
|
-
const maxLines = question
|
|
298
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
267
299
|
const display = () => {
|
|
268
300
|
this.clearScreen();
|
|
269
|
-
|
|
270
|
-
this.log(promptMessage);
|
|
301
|
+
this.displayPrompt(question, ctx, input);
|
|
271
302
|
// Determine the range of options to display
|
|
272
303
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
273
304
|
for (let i = startIndex; i < endIndex; i++) {
|
|
274
305
|
const option = filteredOptions[i];
|
|
275
|
-
if (
|
|
276
|
-
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
|
|
277
311
|
}
|
|
278
312
|
else {
|
|
279
|
-
this.log(' ' + option);
|
|
313
|
+
this.log(' ' + option.name);
|
|
280
314
|
}
|
|
281
315
|
}
|
|
282
316
|
};
|
|
@@ -332,14 +366,32 @@ export class Inquirerer {
|
|
|
332
366
|
return new Promise(resolve => {
|
|
333
367
|
this.keypress.on(KEY_CODES.ENTER, () => {
|
|
334
368
|
this.keypress.pause();
|
|
335
|
-
resolve(filteredOptions[selectedIndex] || input);
|
|
369
|
+
resolve(filteredOptions[selectedIndex]?.value || input);
|
|
336
370
|
});
|
|
337
371
|
});
|
|
338
372
|
}
|
|
339
373
|
filterOptions(options, input) {
|
|
340
374
|
return options
|
|
341
|
-
.filter(option => option.toLowerCase().startsWith(input.toLowerCase()))
|
|
342
|
-
.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);
|
|
343
395
|
}
|
|
344
396
|
// Method to cleanly close the readline interface
|
|
345
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
|
|
@@ -129,9 +151,8 @@ class Inquirerer {
|
|
|
129
151
|
if (this.noTty || !this.rl)
|
|
130
152
|
return question.default ?? false; // Return default if non-interactive
|
|
131
153
|
return new Promise((resolve) => {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
this.rl.question(promptMessage, (answer) => {
|
|
154
|
+
this.clearScreen();
|
|
155
|
+
this.rl.question(this.getPrompt(question, ctx, ''), (answer) => {
|
|
135
156
|
const userInput = answer.trim().toLowerCase();
|
|
136
157
|
if (userInput === '') {
|
|
137
158
|
resolve(question.default ?? false); // Use default value if input is empty
|
|
@@ -148,14 +169,13 @@ class Inquirerer {
|
|
|
148
169
|
async text(question, ctx) {
|
|
149
170
|
if (this.noTty || !this.rl)
|
|
150
171
|
return question.default ?? null; // Return default if non-interactive
|
|
151
|
-
let
|
|
172
|
+
let input = '';
|
|
152
173
|
return new Promise((resolve) => {
|
|
153
|
-
this.clearScreen();
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
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
|
|
159
179
|
}
|
|
160
180
|
else {
|
|
161
181
|
resolve(null); // Return null if empty and not required
|
|
@@ -167,29 +187,33 @@ class Inquirerer {
|
|
|
167
187
|
if (this.noTty || !this.rl)
|
|
168
188
|
return question.default ?? []; // Return default if non-interactive
|
|
169
189
|
this.keypress.resume();
|
|
170
|
-
const options = question.options
|
|
190
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
171
191
|
let input = ''; // Search input
|
|
172
192
|
let filteredOptions = options;
|
|
173
193
|
let selectedIndex = 0;
|
|
174
194
|
let startIndex = 0; // Start index for visible options
|
|
175
|
-
const maxLines = question
|
|
195
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
176
196
|
const selections = new Array(options.length).fill(false);
|
|
177
197
|
const updateFilteredOptions = () => {
|
|
178
|
-
filteredOptions = options.filter(option => option.toLowerCase().includes(input.toLowerCase()));
|
|
198
|
+
filteredOptions = options.filter(option => option.name.toLowerCase().includes(input.toLowerCase()));
|
|
179
199
|
};
|
|
180
200
|
const display = () => {
|
|
181
201
|
this.clearScreen();
|
|
182
|
-
|
|
183
|
-
this.write(promptMessage);
|
|
184
|
-
this.log(`${input}`);
|
|
202
|
+
this.displayPrompt(question, ctx, input);
|
|
185
203
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
186
204
|
for (let i = startIndex; i < endIndex; i++) {
|
|
187
205
|
const option = filteredOptions[i];
|
|
188
206
|
const isSelected = selectedIndex === i;
|
|
189
207
|
const marker = isSelected ? '>' : ' ';
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
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
|
+
}
|
|
193
217
|
}
|
|
194
218
|
};
|
|
195
219
|
display();
|
|
@@ -240,7 +264,7 @@ class Inquirerer {
|
|
|
240
264
|
// Return all options with their selected status
|
|
241
265
|
options.forEach((option, index) => {
|
|
242
266
|
result.push({
|
|
243
|
-
name: option,
|
|
267
|
+
name: option.name,
|
|
244
268
|
value: selections[index]
|
|
245
269
|
});
|
|
246
270
|
});
|
|
@@ -250,7 +274,7 @@ class Inquirerer {
|
|
|
250
274
|
options.forEach((option, index) => {
|
|
251
275
|
if (selections[index]) {
|
|
252
276
|
result.push({
|
|
253
|
-
name: option,
|
|
277
|
+
name: option.name,
|
|
254
278
|
value: selections[index]
|
|
255
279
|
});
|
|
256
280
|
}
|
|
@@ -260,29 +284,39 @@ class Inquirerer {
|
|
|
260
284
|
});
|
|
261
285
|
});
|
|
262
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
|
+
}
|
|
263
295
|
async autocomplete(question, ctx) {
|
|
264
296
|
if (this.noTty || !this.rl)
|
|
265
297
|
return question.default ?? false; // Return default if non-interactive
|
|
266
298
|
this.keypress.resume();
|
|
267
|
-
const options = question.options
|
|
299
|
+
const options = (question.options ?? []).map(option => this.getOptionValue(option));
|
|
268
300
|
let input = '';
|
|
269
301
|
let filteredOptions = options;
|
|
270
302
|
let selectedIndex = 0;
|
|
271
303
|
let startIndex = 0; // Start index for visible options
|
|
272
|
-
const maxLines = question
|
|
304
|
+
const maxLines = this.getMaxLines(question, options.length); // Use provided max or total options
|
|
273
305
|
const display = () => {
|
|
274
306
|
this.clearScreen();
|
|
275
|
-
|
|
276
|
-
this.log(promptMessage);
|
|
307
|
+
this.displayPrompt(question, ctx, input);
|
|
277
308
|
// Determine the range of options to display
|
|
278
309
|
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
279
310
|
for (let i = startIndex; i < endIndex; i++) {
|
|
280
311
|
const option = filteredOptions[i];
|
|
281
|
-
if (
|
|
282
|
-
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
|
|
283
317
|
}
|
|
284
318
|
else {
|
|
285
|
-
this.log(' ' + option);
|
|
319
|
+
this.log(' ' + option.name);
|
|
286
320
|
}
|
|
287
321
|
}
|
|
288
322
|
};
|
|
@@ -338,14 +372,32 @@ class Inquirerer {
|
|
|
338
372
|
return new Promise(resolve => {
|
|
339
373
|
this.keypress.on(keypress_1.KEY_CODES.ENTER, () => {
|
|
340
374
|
this.keypress.pause();
|
|
341
|
-
resolve(filteredOptions[selectedIndex] || input);
|
|
375
|
+
resolve(filteredOptions[selectedIndex]?.value || input);
|
|
342
376
|
});
|
|
343
377
|
});
|
|
344
378
|
}
|
|
345
379
|
filterOptions(options, input) {
|
|
346
380
|
return options
|
|
347
|
-
.filter(option => option.toLowerCase().startsWith(input.toLowerCase()))
|
|
348
|
-
.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);
|
|
349
401
|
}
|
|
350
402
|
// Method to cleanly close the readline interface
|
|
351
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[];
|