@topcli/prompts 1.9.0 → 1.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/README.md +51 -24
- package/dist/index.cjs +233 -172
- package/dist/index.d.cts +22 -16
- package/dist/index.d.ts +22 -16
- package/dist/index.js +233 -172
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import wcwidth from "@topcli/wcwidth";
|
|
|
7
7
|
import { EOL } from "os";
|
|
8
8
|
import { createInterface } from "readline";
|
|
9
9
|
import { Writable } from "stream";
|
|
10
|
+
import EventEmitter from "events";
|
|
10
11
|
|
|
11
12
|
// src/utils.ts
|
|
12
13
|
import process2 from "process";
|
|
@@ -72,25 +73,43 @@ var PromptAgent = class _PromptAgent {
|
|
|
72
73
|
}
|
|
73
74
|
};
|
|
74
75
|
|
|
76
|
+
// src/errors/abort.ts
|
|
77
|
+
var AbortError = class extends Error {
|
|
78
|
+
constructor(message) {
|
|
79
|
+
super(message);
|
|
80
|
+
this.name = "AbortError";
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
75
84
|
// src/prompts/abstract.ts
|
|
76
|
-
var AbstractPrompt = class _AbstractPrompt {
|
|
85
|
+
var AbstractPrompt = class _AbstractPrompt extends EventEmitter {
|
|
77
86
|
stdin;
|
|
78
87
|
stdout;
|
|
79
88
|
message;
|
|
89
|
+
signal;
|
|
80
90
|
history;
|
|
81
91
|
agent;
|
|
82
92
|
mute;
|
|
83
93
|
rl;
|
|
84
|
-
|
|
94
|
+
#signalHandler;
|
|
95
|
+
constructor(options) {
|
|
96
|
+
super();
|
|
85
97
|
if (this.constructor === _AbstractPrompt) {
|
|
86
98
|
throw new Error("AbstractPrompt can't be instantiated.");
|
|
87
99
|
}
|
|
100
|
+
const {
|
|
101
|
+
stdin: input = process.stdin,
|
|
102
|
+
stdout: output = process.stdout,
|
|
103
|
+
message,
|
|
104
|
+
signal
|
|
105
|
+
} = options;
|
|
88
106
|
if (typeof message !== "string") {
|
|
89
107
|
throw new TypeError(`message must be string, ${typeof message} given.`);
|
|
90
108
|
}
|
|
91
109
|
this.stdin = input;
|
|
92
110
|
this.stdout = output;
|
|
93
111
|
this.message = message;
|
|
112
|
+
this.signal = signal;
|
|
94
113
|
this.history = [];
|
|
95
114
|
this.agent = PromptAgent.agent();
|
|
96
115
|
this.mute = false;
|
|
@@ -101,7 +120,7 @@ var AbstractPrompt = class _AbstractPrompt {
|
|
|
101
120
|
input,
|
|
102
121
|
output: new Writable({
|
|
103
122
|
write: (chunk, encoding, callback) => {
|
|
104
|
-
if (!this.mute) {
|
|
123
|
+
if (!this.mute && chunk) {
|
|
105
124
|
this.stdout.write(chunk, encoding);
|
|
106
125
|
}
|
|
107
126
|
callback();
|
|
@@ -109,6 +128,16 @@ var AbstractPrompt = class _AbstractPrompt {
|
|
|
109
128
|
}),
|
|
110
129
|
terminal: true
|
|
111
130
|
});
|
|
131
|
+
if (this.signal) {
|
|
132
|
+
this.#signalHandler = () => {
|
|
133
|
+
this.rl.close();
|
|
134
|
+
for (let i = 0; i < this.history.length; i++) {
|
|
135
|
+
this.clearLastLine();
|
|
136
|
+
}
|
|
137
|
+
this.emit("error", new AbortError("Prompt aborted"));
|
|
138
|
+
};
|
|
139
|
+
this.signal.addEventListener("abort", this.#signalHandler, { once: true });
|
|
140
|
+
}
|
|
112
141
|
}
|
|
113
142
|
write(data) {
|
|
114
143
|
const formattedData = stripAnsi(data).replace(EOL, "");
|
|
@@ -128,6 +157,9 @@ var AbstractPrompt = class _AbstractPrompt {
|
|
|
128
157
|
}
|
|
129
158
|
destroy() {
|
|
130
159
|
this.rl.close();
|
|
160
|
+
if (this.signal) {
|
|
161
|
+
this.signal.removeEventListener("abort", this.#signalHandler);
|
|
162
|
+
}
|
|
131
163
|
}
|
|
132
164
|
};
|
|
133
165
|
|
|
@@ -175,15 +207,14 @@ var QuestionPrompt = class extends AbstractPrompt {
|
|
|
175
207
|
answerBuffer;
|
|
176
208
|
#validators;
|
|
177
209
|
#secure;
|
|
178
|
-
constructor(
|
|
210
|
+
constructor(options) {
|
|
179
211
|
const {
|
|
180
|
-
stdin = process.stdin,
|
|
181
|
-
stdout = process.stdout,
|
|
182
212
|
defaultValue,
|
|
183
213
|
validators = [],
|
|
184
|
-
secure = false
|
|
214
|
+
secure = false,
|
|
215
|
+
...baseOptions
|
|
185
216
|
} = options;
|
|
186
|
-
super(
|
|
217
|
+
super({ ...baseOptions });
|
|
187
218
|
if (defaultValue && typeof defaultValue !== "string") {
|
|
188
219
|
throw new TypeError("defaultValue must be a string");
|
|
189
220
|
}
|
|
@@ -196,6 +227,7 @@ var QuestionPrompt = class extends AbstractPrompt {
|
|
|
196
227
|
#question() {
|
|
197
228
|
return new Promise((resolve) => {
|
|
198
229
|
const questionQuery = this.#getQuestionQuery();
|
|
230
|
+
this.history.push(questionQuery);
|
|
199
231
|
this.rl.question(questionQuery, (answer) => {
|
|
200
232
|
this.history.push(questionQuery + answer);
|
|
201
233
|
this.mute = false;
|
|
@@ -233,24 +265,30 @@ var QuestionPrompt = class extends AbstractPrompt {
|
|
|
233
265
|
this.answerBuffer = void 0;
|
|
234
266
|
this.#writeAnswer();
|
|
235
267
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
this
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
this.
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
this.answer
|
|
268
|
+
question() {
|
|
269
|
+
return new Promise(async (resolve, reject) => {
|
|
270
|
+
this.answer = this.agent.nextAnswers.shift();
|
|
271
|
+
if (this.answer !== void 0) {
|
|
272
|
+
this.#writeAnswer();
|
|
273
|
+
this.destroy();
|
|
274
|
+
resolve(this.answer);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
this.once("error", (error) => {
|
|
278
|
+
reject(error);
|
|
279
|
+
});
|
|
280
|
+
this.answer = await this.#question();
|
|
281
|
+
if (this.answer === "" && this.defaultValue) {
|
|
282
|
+
this.answer = this.defaultValue;
|
|
283
|
+
}
|
|
250
284
|
this.#onQuestionAnswer();
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
285
|
+
while (this.answerBuffer !== void 0) {
|
|
286
|
+
this.answer = await this.answerBuffer;
|
|
287
|
+
this.#onQuestionAnswer();
|
|
288
|
+
}
|
|
289
|
+
this.destroy();
|
|
290
|
+
resolve(this.answer);
|
|
291
|
+
});
|
|
254
292
|
}
|
|
255
293
|
};
|
|
256
294
|
|
|
@@ -277,13 +315,12 @@ var ConfirmPrompt = class extends AbstractPrompt {
|
|
|
277
315
|
fastAnswer;
|
|
278
316
|
#boundKeyPressEvent;
|
|
279
317
|
#boundExitEvent;
|
|
280
|
-
constructor(
|
|
318
|
+
constructor(options) {
|
|
281
319
|
const {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
initial = false
|
|
320
|
+
initial = false,
|
|
321
|
+
...baseOptions
|
|
285
322
|
} = options;
|
|
286
|
-
super(
|
|
323
|
+
super({ ...baseOptions });
|
|
287
324
|
this.initial = initial;
|
|
288
325
|
this.selectedValue = initial;
|
|
289
326
|
}
|
|
@@ -347,25 +384,31 @@ var ConfirmPrompt = class extends AbstractPrompt {
|
|
|
347
384
|
this.stdout.clearScreenDown();
|
|
348
385
|
this.write(`${this.selectedValue ? SYMBOLS.Tick : SYMBOLS.Cross} ${kleur3.bold(this.message)}${EOL3}`);
|
|
349
386
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
387
|
+
confirm() {
|
|
388
|
+
return new Promise(async (resolve, reject) => {
|
|
389
|
+
const answer = this.agent.nextAnswers.shift();
|
|
390
|
+
if (answer !== void 0) {
|
|
391
|
+
this.selectedValue = answer;
|
|
392
|
+
this.#onQuestionAnswer();
|
|
393
|
+
this.destroy();
|
|
394
|
+
resolve(answer);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
this.once("error", (error) => {
|
|
398
|
+
reject(error);
|
|
399
|
+
});
|
|
400
|
+
this.write(SYMBOLS.HideCursor);
|
|
401
|
+
try {
|
|
402
|
+
await this.#question();
|
|
403
|
+
this.#onQuestionAnswer();
|
|
404
|
+
resolve(this.selectedValue);
|
|
405
|
+
} finally {
|
|
406
|
+
this.write(SYMBOLS.ShowCursor);
|
|
407
|
+
this.#onProcessExit();
|
|
408
|
+
process.off("exit", this.#boundExitEvent);
|
|
409
|
+
this.destroy();
|
|
410
|
+
}
|
|
411
|
+
});
|
|
369
412
|
}
|
|
370
413
|
};
|
|
371
414
|
|
|
@@ -377,6 +420,7 @@ var kRequiredChoiceProperties = ["label", "value"];
|
|
|
377
420
|
var SelectPrompt = class extends AbstractPrompt {
|
|
378
421
|
#boundExitEvent = () => void 0;
|
|
379
422
|
#boundKeyPressEvent = () => void 0;
|
|
423
|
+
#validators;
|
|
380
424
|
activeIndex = 0;
|
|
381
425
|
questionMessage;
|
|
382
426
|
autocompleteValue = "";
|
|
@@ -414,22 +458,19 @@ var SelectPrompt = class extends AbstractPrompt {
|
|
|
414
458
|
return choice.label.length;
|
|
415
459
|
}));
|
|
416
460
|
}
|
|
417
|
-
constructor(
|
|
461
|
+
constructor(options) {
|
|
418
462
|
const {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
} = options
|
|
423
|
-
super(
|
|
424
|
-
if (!options) {
|
|
425
|
-
this.destroy();
|
|
426
|
-
throw new TypeError("Missing required options");
|
|
427
|
-
}
|
|
463
|
+
choices,
|
|
464
|
+
validators = [],
|
|
465
|
+
...baseOptions
|
|
466
|
+
} = options;
|
|
467
|
+
super({ ...baseOptions });
|
|
428
468
|
this.options = options;
|
|
429
469
|
if (!choices?.length) {
|
|
430
470
|
this.destroy();
|
|
431
471
|
throw new TypeError("Missing required param: choices");
|
|
432
472
|
}
|
|
473
|
+
this.#validators = validators;
|
|
433
474
|
for (const choice of choices) {
|
|
434
475
|
if (typeof choice === "string") {
|
|
435
476
|
continue;
|
|
@@ -509,6 +550,13 @@ var SelectPrompt = class extends AbstractPrompt {
|
|
|
509
550
|
const choice = this.filteredChoices[this.activeIndex] || "";
|
|
510
551
|
const label = typeof choice === "string" ? choice : choice.label;
|
|
511
552
|
const value = typeof choice === "string" ? choice : choice.value;
|
|
553
|
+
for (const validator of this.#validators) {
|
|
554
|
+
if (!validator.validate(value)) {
|
|
555
|
+
const error = validator.error(value);
|
|
556
|
+
render({ error });
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
512
560
|
render({ clearRender: true });
|
|
513
561
|
if (!this.options.ignoreValues?.includes(value)) {
|
|
514
562
|
this.#showAnsweredQuestion(label);
|
|
@@ -530,56 +578,71 @@ var SelectPrompt = class extends AbstractPrompt {
|
|
|
530
578
|
render();
|
|
531
579
|
}
|
|
532
580
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
}
|
|
553
|
-
if (
|
|
554
|
-
let
|
|
555
|
-
|
|
556
|
-
);
|
|
557
|
-
while (linesToClear2 > 0) {
|
|
581
|
+
select() {
|
|
582
|
+
return new Promise((resolve, reject) => {
|
|
583
|
+
const answer = this.agent.nextAnswers.shift();
|
|
584
|
+
if (answer !== void 0) {
|
|
585
|
+
this.#showAnsweredQuestion(answer);
|
|
586
|
+
this.destroy();
|
|
587
|
+
resolve(answer);
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
this.once("error", (error) => {
|
|
591
|
+
reject(error);
|
|
592
|
+
});
|
|
593
|
+
this.write(SYMBOLS.HideCursor);
|
|
594
|
+
this.#showQuestion();
|
|
595
|
+
const render = (options = {}) => {
|
|
596
|
+
const {
|
|
597
|
+
initialRender = false,
|
|
598
|
+
clearRender = false,
|
|
599
|
+
error = null
|
|
600
|
+
} = options;
|
|
601
|
+
if (!initialRender) {
|
|
602
|
+
let linesToClear = this.lastRender.endIndex - this.lastRender.startIndex;
|
|
603
|
+
while (linesToClear > 0) {
|
|
558
604
|
this.clearLastLine();
|
|
559
|
-
|
|
605
|
+
linesToClear--;
|
|
606
|
+
}
|
|
607
|
+
if (this.options.autocomplete) {
|
|
608
|
+
let linesToClear2 = Math.ceil(
|
|
609
|
+
wcwidth3(`${SYMBOLS.Pointer} ${this.autocompleteValue}`) / this.stdout.columns
|
|
610
|
+
);
|
|
611
|
+
while (linesToClear2 > 0) {
|
|
612
|
+
this.clearLastLine();
|
|
613
|
+
linesToClear2--;
|
|
614
|
+
}
|
|
560
615
|
}
|
|
561
616
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
617
|
+
if (clearRender) {
|
|
618
|
+
const questionLineCount = Math.ceil(
|
|
619
|
+
wcwidth3(stripAnsi(this.questionMessage)) / this.stdout.columns
|
|
620
|
+
);
|
|
621
|
+
this.stdout.moveCursor(-this.stdout.columns, -(1 + questionLineCount));
|
|
622
|
+
this.stdout.clearScreenDown();
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
if (error) {
|
|
626
|
+
const linesToClear = Math.ceil(wcwidth3(this.questionMessage) / this.stdout.columns) + 1;
|
|
627
|
+
this.stdout.moveCursor(0, -linesToClear);
|
|
628
|
+
this.stdout.clearScreenDown();
|
|
629
|
+
this.#showQuestion(error);
|
|
630
|
+
}
|
|
631
|
+
this.#showChoices();
|
|
632
|
+
};
|
|
633
|
+
render({ initialRender: true });
|
|
575
634
|
this.#boundKeyPressEvent = this.#onKeypress.bind(this, resolve, render);
|
|
576
635
|
this.stdin.on("keypress", this.#boundKeyPressEvent);
|
|
577
636
|
this.#boundExitEvent = this.#onProcessExit.bind(this);
|
|
578
637
|
process.once("exit", this.#boundExitEvent);
|
|
579
638
|
});
|
|
580
639
|
}
|
|
581
|
-
#showQuestion() {
|
|
582
|
-
|
|
640
|
+
#showQuestion(error = null) {
|
|
641
|
+
let hint = "";
|
|
642
|
+
if (error) {
|
|
643
|
+
hint = ` ${hint.length > 0 ? " " : ""}${kleur4.red().bold(`[${error}]`)}`;
|
|
644
|
+
}
|
|
645
|
+
this.questionMessage = `${SYMBOLS.QuestionMark} ${kleur4.bold(this.message)}${hint}`;
|
|
583
646
|
this.write(`${this.questionMessage}${EOL4}`);
|
|
584
647
|
}
|
|
585
648
|
};
|
|
@@ -595,7 +658,7 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
595
658
|
#validators;
|
|
596
659
|
#showHint;
|
|
597
660
|
activeIndex = 0;
|
|
598
|
-
selectedIndexes =
|
|
661
|
+
selectedIndexes = /* @__PURE__ */ new Set();
|
|
599
662
|
questionMessage;
|
|
600
663
|
autocompleteValue = "";
|
|
601
664
|
options;
|
|
@@ -632,20 +695,15 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
632
695
|
return choice.label.length;
|
|
633
696
|
}));
|
|
634
697
|
}
|
|
635
|
-
constructor(
|
|
698
|
+
constructor(options) {
|
|
636
699
|
const {
|
|
637
|
-
stdin = process.stdin,
|
|
638
|
-
stdout = process.stdout,
|
|
639
700
|
choices,
|
|
640
701
|
preSelectedChoices,
|
|
641
702
|
validators = [],
|
|
642
|
-
showHint = true
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
this.destroy();
|
|
647
|
-
throw new TypeError("Missing required options");
|
|
648
|
-
}
|
|
703
|
+
showHint = true,
|
|
704
|
+
...baseOptions
|
|
705
|
+
} = options;
|
|
706
|
+
super({ ...baseOptions });
|
|
649
707
|
this.options = options;
|
|
650
708
|
if (!choices?.length) {
|
|
651
709
|
this.destroy();
|
|
@@ -678,7 +736,7 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
678
736
|
this.destroy();
|
|
679
737
|
throw new Error(`Invalid pre-selected choice: ${typeof choice === "string" ? choice : choice.value}`);
|
|
680
738
|
}
|
|
681
|
-
this.selectedIndexes.
|
|
739
|
+
this.selectedIndexes.add(choiceIndex);
|
|
682
740
|
}
|
|
683
741
|
}
|
|
684
742
|
#getFormattedChoice(choiceIndex) {
|
|
@@ -706,7 +764,7 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
706
764
|
for (let choiceIndex = startIndex; choiceIndex < endIndex; choiceIndex++) {
|
|
707
765
|
const choice = this.#getFormattedChoice(choiceIndex);
|
|
708
766
|
const isChoiceActive = choiceIndex === this.activeIndex;
|
|
709
|
-
const isChoiceSelected = this.selectedIndexes.
|
|
767
|
+
const isChoiceSelected = this.selectedIndexes.has(choiceIndex);
|
|
710
768
|
const showPreviousChoicesArrow = startIndex > 0 && choiceIndex === startIndex;
|
|
711
769
|
const showNextChoicesArrow = endIndex < this.filteredChoices.length && choiceIndex === endIndex - 1;
|
|
712
770
|
let prefixArrow = " ";
|
|
@@ -726,7 +784,7 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
726
784
|
}
|
|
727
785
|
}
|
|
728
786
|
#showAnsweredQuestion(choices, isAgentAnswer = false) {
|
|
729
|
-
const prefixSymbol = this.selectedIndexes.
|
|
787
|
+
const prefixSymbol = this.selectedIndexes.size === 0 && !isAgentAnswer ? SYMBOLS.Cross : SYMBOLS.Tick;
|
|
730
788
|
const prefix = `${prefixSymbol} ${kleur5.bold(this.message)} ${SYMBOLS.Pointer}`;
|
|
731
789
|
const formattedChoice = kleur5.yellow(choices);
|
|
732
790
|
this.write(`${prefix}${choices ? ` ${formattedChoice}` : ""}${EOL5}`);
|
|
@@ -746,20 +804,20 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
746
804
|
this.activeIndex = this.activeIndex === this.filteredChoices.length - 1 ? 0 : this.activeIndex + 1;
|
|
747
805
|
render();
|
|
748
806
|
} else if (key.ctrl && key.name === "a") {
|
|
749
|
-
this.selectedIndexes = this.selectedIndexes.
|
|
807
|
+
this.selectedIndexes = this.selectedIndexes.size === this.filteredChoices.length ? /* @__PURE__ */ new Set() : new Set(this.filteredChoices.map((_, index) => index));
|
|
750
808
|
render();
|
|
751
809
|
} else if (key.name === "right") {
|
|
752
|
-
this.selectedIndexes.
|
|
810
|
+
this.selectedIndexes.add(this.activeIndex);
|
|
753
811
|
render();
|
|
754
812
|
} else if (key.name === "left") {
|
|
755
|
-
this.selectedIndexes = this.selectedIndexes.filter((index) => index !== this.activeIndex);
|
|
813
|
+
this.selectedIndexes = new Set([...this.selectedIndexes].filter((index) => index !== this.activeIndex));
|
|
756
814
|
render();
|
|
757
815
|
} else if (key.name === "return") {
|
|
758
|
-
const labels = this.selectedIndexes.map((index) => {
|
|
816
|
+
const labels = [...this.selectedIndexes].map((index) => {
|
|
759
817
|
const choice = this.filteredChoices[index];
|
|
760
818
|
return typeof choice === "string" ? choice : choice.label;
|
|
761
819
|
});
|
|
762
|
-
const values = this.selectedIndexes.map((index) => {
|
|
820
|
+
const values = [...this.selectedIndexes].map((index) => {
|
|
763
821
|
const choice = this.filteredChoices[index];
|
|
764
822
|
return typeof choice === "string" ? choice : choice.value;
|
|
765
823
|
});
|
|
@@ -779,7 +837,7 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
779
837
|
resolve(values);
|
|
780
838
|
} else {
|
|
781
839
|
if (!key.ctrl && this.options.autocomplete) {
|
|
782
|
-
this.selectedIndexes
|
|
840
|
+
this.selectedIndexes.clear();
|
|
783
841
|
this.activeIndex = 0;
|
|
784
842
|
if (key.name === "backspace" && this.autocompleteValue.length > 0) {
|
|
785
843
|
this.autocompleteValue = this.autocompleteValue.slice(0, -1);
|
|
@@ -790,56 +848,60 @@ var MultiselectPrompt = class extends AbstractPrompt {
|
|
|
790
848
|
render();
|
|
791
849
|
}
|
|
792
850
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
wcwidth4(`${SYMBOLS.Pointer} ${this.autocompleteValue}`) / this.stdout.columns
|
|
818
|
-
);
|
|
819
|
-
while (linesToClear2 > 0) {
|
|
851
|
+
multiselect() {
|
|
852
|
+
return new Promise((resolve, reject) => {
|
|
853
|
+
const answer = this.agent.nextAnswers.shift();
|
|
854
|
+
if (answer !== void 0) {
|
|
855
|
+
const formatedAnser = Array.isArray(answer) ? answer.join(", ") : answer;
|
|
856
|
+
this.#showAnsweredQuestion(formatedAnser, true);
|
|
857
|
+
this.destroy();
|
|
858
|
+
resolve(Array.isArray(answer) ? answer : [answer]);
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
this.once("error", (error) => {
|
|
862
|
+
reject(error);
|
|
863
|
+
});
|
|
864
|
+
this.write(SYMBOLS.HideCursor);
|
|
865
|
+
this.#showQuestion();
|
|
866
|
+
const render = (options = {}) => {
|
|
867
|
+
const {
|
|
868
|
+
initialRender = false,
|
|
869
|
+
clearRender = false,
|
|
870
|
+
error = null
|
|
871
|
+
} = options;
|
|
872
|
+
if (!initialRender) {
|
|
873
|
+
let linesToClear = this.lastRender.endIndex - this.lastRender.startIndex;
|
|
874
|
+
while (linesToClear > 0) {
|
|
820
875
|
this.clearLastLine();
|
|
821
|
-
|
|
876
|
+
linesToClear--;
|
|
877
|
+
}
|
|
878
|
+
if (this.options.autocomplete) {
|
|
879
|
+
let linesToClear2 = Math.ceil(
|
|
880
|
+
wcwidth4(`${SYMBOLS.Pointer} ${this.autocompleteValue}`) / this.stdout.columns
|
|
881
|
+
);
|
|
882
|
+
while (linesToClear2 > 0) {
|
|
883
|
+
this.clearLastLine();
|
|
884
|
+
linesToClear2--;
|
|
885
|
+
}
|
|
822
886
|
}
|
|
823
887
|
}
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
render({ initialRender: true });
|
|
842
|
-
return new Promise((resolve) => {
|
|
888
|
+
if (clearRender) {
|
|
889
|
+
const questionLineCount = Math.ceil(
|
|
890
|
+
wcwidth4(stripAnsi(this.questionMessage)) / this.stdout.columns
|
|
891
|
+
);
|
|
892
|
+
this.stdout.moveCursor(-this.stdout.columns, -(1 + questionLineCount));
|
|
893
|
+
this.stdout.clearScreenDown();
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
if (error) {
|
|
897
|
+
const linesToClear = Math.ceil(wcwidth4(this.questionMessage) / this.stdout.columns) + 1;
|
|
898
|
+
this.stdout.moveCursor(0, -linesToClear);
|
|
899
|
+
this.stdout.clearScreenDown();
|
|
900
|
+
this.#showQuestion(error);
|
|
901
|
+
}
|
|
902
|
+
this.#showChoices();
|
|
903
|
+
};
|
|
904
|
+
render({ initialRender: true });
|
|
843
905
|
this.#boundKeyPressEvent = this.#onKeypress.bind(this, resolve, render);
|
|
844
906
|
this.stdin.on("keypress", this.#boundKeyPressEvent);
|
|
845
907
|
this.#boundExitEvent = this.#onProcessExit.bind(this);
|
|
@@ -869,19 +931,18 @@ function required() {
|
|
|
869
931
|
|
|
870
932
|
// index.ts
|
|
871
933
|
async function question(message, options = {}) {
|
|
872
|
-
|
|
873
|
-
return questionPrompt.question();
|
|
934
|
+
return new QuestionPrompt({ ...options, message }).question();
|
|
874
935
|
}
|
|
875
936
|
async function select(message, options) {
|
|
876
|
-
const selectPrompt = new SelectPrompt(
|
|
937
|
+
const selectPrompt = new SelectPrompt({ ...options, message });
|
|
877
938
|
return selectPrompt.select();
|
|
878
939
|
}
|
|
879
940
|
async function confirm(message, options) {
|
|
880
|
-
const confirmPrompt = new ConfirmPrompt(
|
|
941
|
+
const confirmPrompt = new ConfirmPrompt({ ...options, message });
|
|
881
942
|
return confirmPrompt.confirm();
|
|
882
943
|
}
|
|
883
944
|
async function multiselect(message, options) {
|
|
884
|
-
const multiselectPrompt = new MultiselectPrompt(
|
|
945
|
+
const multiselectPrompt = new MultiselectPrompt({ ...options, message });
|
|
885
946
|
return multiselectPrompt.multiselect();
|
|
886
947
|
}
|
|
887
948
|
export {
|