@shell-shock/plugin-prompts 0.2.3 → 0.3.1
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/dist/components/index.cjs +2 -1
- package/dist/components/index.d.cts +2 -2
- package/dist/components/index.d.mts +2 -2
- package/dist/components/index.mjs +2 -2
- package/dist/components/prompts-builtin.cjs +83 -40
- package/dist/components/prompts-builtin.d.cts +2 -1
- package/dist/components/prompts-builtin.d.cts.map +1 -1
- package/dist/components/prompts-builtin.d.mts +2 -1
- package/dist/components/prompts-builtin.d.mts.map +1 -1
- package/dist/components/prompts-builtin.mjs +82 -40
- package/dist/components/prompts-builtin.mjs.map +1 -1
- package/package.json +9 -9
|
@@ -181,7 +181,7 @@ function BasePromptDeclarations() {
|
|
|
181
181
|
createComponent(InterfaceMember, {
|
|
182
182
|
name: "validate",
|
|
183
183
|
optional: true,
|
|
184
|
-
type: "(value: TValue) => boolean | string |
|
|
184
|
+
type: "(value: TValue) => boolean | string | null | undefined | Promise<boolean | string | null | undefined>",
|
|
185
185
|
doc: "A validation function that returns true if the input is valid, false or a string error message if the input is invalid"
|
|
186
186
|
}),
|
|
187
187
|
createComponent(Spacing, {}),
|
|
@@ -367,21 +367,21 @@ function BasePromptDeclarations() {
|
|
|
367
367
|
}),
|
|
368
368
|
createIntrinsic("hbr", {}),
|
|
369
369
|
createComponent(ClassField, {
|
|
370
|
-
name: "
|
|
370
|
+
name: "validate",
|
|
371
371
|
"protected": true,
|
|
372
372
|
type: "(value: TValue) => boolean | string | null | undefined | Promise<boolean | string | null | undefined>",
|
|
373
373
|
children: code`() => true; `
|
|
374
374
|
}),
|
|
375
375
|
createIntrinsic("hbr", {}),
|
|
376
376
|
createComponent(ClassField, {
|
|
377
|
-
name: "
|
|
377
|
+
name: "parse",
|
|
378
378
|
"protected": true,
|
|
379
379
|
type: "PromptParser<TValue>",
|
|
380
380
|
children: code`(value: string) => value as TValue; `
|
|
381
381
|
}),
|
|
382
382
|
createIntrinsic("hbr", {}),
|
|
383
383
|
createComponent(ClassField, {
|
|
384
|
-
name: "
|
|
384
|
+
name: "format",
|
|
385
385
|
"protected": true,
|
|
386
386
|
type: "PromptFormatter<TValue>",
|
|
387
387
|
children: code`(value: TValue) => String(value); `
|
|
@@ -397,7 +397,8 @@ function BasePromptDeclarations() {
|
|
|
397
397
|
createComponent(ClassField, {
|
|
398
398
|
name: "maskCompleted",
|
|
399
399
|
"protected": true,
|
|
400
|
-
type: "(input: string) => string"
|
|
400
|
+
type: "(input: string) => string",
|
|
401
|
+
children: code`this.mask; `
|
|
401
402
|
}),
|
|
402
403
|
createIntrinsic("hbr", {}),
|
|
403
404
|
createComponent(ClassField, {
|
|
@@ -437,13 +438,13 @@ function BasePromptDeclarations() {
|
|
|
437
438
|
this.description = config.description;
|
|
438
439
|
}
|
|
439
440
|
if (config.validate) {
|
|
440
|
-
this.
|
|
441
|
+
this.validate = config.validate;
|
|
441
442
|
}
|
|
442
443
|
if (config.parse) {
|
|
443
|
-
this.
|
|
444
|
+
this.parse = config.parse.bind(this);
|
|
444
445
|
}
|
|
445
446
|
if (config.format) {
|
|
446
|
-
this.
|
|
447
|
+
this.format = config.format.bind(this);
|
|
447
448
|
}
|
|
448
449
|
|
|
449
450
|
if (config.maskCompleted) {
|
|
@@ -452,9 +453,6 @@ function BasePromptDeclarations() {
|
|
|
452
453
|
if (config.mask) {
|
|
453
454
|
this.mask = config.mask;
|
|
454
455
|
}
|
|
455
|
-
if (!this.maskCompleted) {
|
|
456
|
-
this.maskCompleted = this.mask;
|
|
457
|
-
}
|
|
458
456
|
|
|
459
457
|
if (config.timeout !== undefined && !Number.isNaN(config.timeout)) {
|
|
460
458
|
setTimeout(() => {
|
|
@@ -516,7 +514,7 @@ function BasePromptDeclarations() {
|
|
|
516
514
|
"protected": true,
|
|
517
515
|
name: "isPlaceholder",
|
|
518
516
|
type: "boolean",
|
|
519
|
-
children: code`return (this.displayValue === this.
|
|
517
|
+
children: code`return (this.displayValue === this.format(this.initialValue) && !this.#isDirty) || !this.#isKeyPressed; `
|
|
520
518
|
}),
|
|
521
519
|
createComponent(Spacing, {}),
|
|
522
520
|
createComponent(ClassPropertyGet, {
|
|
@@ -569,7 +567,7 @@ function BasePromptDeclarations() {
|
|
|
569
567
|
updatedValue = value;
|
|
570
568
|
}
|
|
571
569
|
|
|
572
|
-
this.displayValue = this.mask(this.
|
|
570
|
+
this.displayValue = this.mask(this.format(updatedValue));
|
|
573
571
|
this.#value = updatedValue;
|
|
574
572
|
|
|
575
573
|
if (!this.#isDirty && this.#value !== this.initialValue) {
|
|
@@ -578,7 +576,7 @@ function BasePromptDeclarations() {
|
|
|
578
576
|
|
|
579
577
|
this.onChange(previousValue);
|
|
580
578
|
setTimeout(() => {
|
|
581
|
-
Promise.resolve(this.
|
|
579
|
+
Promise.resolve(this.checkValidations(updatedValue)).then(() => this.sync());
|
|
582
580
|
}, 0);
|
|
583
581
|
|
|
584
582
|
this.sync(); `
|
|
@@ -675,14 +673,14 @@ function BasePromptDeclarations() {
|
|
|
675
673
|
createComponent(Spacing, {}),
|
|
676
674
|
createComponent(ClassMethod, {
|
|
677
675
|
doc: "A method to validate the prompt input using the provided validator function, which updates the error message and error state based on the validation result. This method is called whenever the prompt value changes and needs to be validated.",
|
|
678
|
-
name: "
|
|
676
|
+
name: "checkValidations",
|
|
679
677
|
async: true,
|
|
680
678
|
"protected": true,
|
|
681
679
|
parameters: [{
|
|
682
680
|
name: "value",
|
|
683
681
|
type: "TValue"
|
|
684
682
|
}],
|
|
685
|
-
children: code`let result = await this.
|
|
683
|
+
children: code`let result = await this.validate(value);
|
|
686
684
|
if (typeof result === "string") {
|
|
687
685
|
this.errorMessage = result;
|
|
688
686
|
} else if (typeof result === "boolean") {
|
|
@@ -759,11 +757,11 @@ function BasePromptDeclarations() {
|
|
|
759
757
|
return this.bell();
|
|
760
758
|
}
|
|
761
759
|
|
|
762
|
-
this.changeValue(\`\${
|
|
760
|
+
this.changeValue(this.parse(\`\${
|
|
763
761
|
this.displayValue.slice(0, this.cursor - 1)
|
|
764
762
|
}\${
|
|
765
763
|
this.displayValue.slice(this.cursor)
|
|
766
|
-
}\`);
|
|
764
|
+
}\`));
|
|
767
765
|
|
|
768
766
|
if (this.isCursorAtStart) {
|
|
769
767
|
this.cursorOffset = 0;
|
|
@@ -783,11 +781,11 @@ function BasePromptDeclarations() {
|
|
|
783
781
|
return this.bell();
|
|
784
782
|
}
|
|
785
783
|
|
|
786
|
-
this.changeValue(\`\${
|
|
784
|
+
this.changeValue(this.parse(\`\${
|
|
787
785
|
this.displayValue.slice(0, this.cursor)
|
|
788
786
|
}\${
|
|
789
787
|
this.displayValue.slice(this.cursor + 1)
|
|
790
|
-
}\`);
|
|
788
|
+
}\`));
|
|
791
789
|
|
|
792
790
|
if (this.isCursorAtEnd) {
|
|
793
791
|
this.cursorOffset = 0;
|
|
@@ -833,7 +831,7 @@ function BasePromptDeclarations() {
|
|
|
833
831
|
children: code`this.cursorOffset = 0;
|
|
834
832
|
this.cursor = this.displayValue.length;
|
|
835
833
|
|
|
836
|
-
await this.
|
|
834
|
+
await this.checkValidations(this.value);
|
|
837
835
|
if (this.isError) {
|
|
838
836
|
this.sync();
|
|
839
837
|
this.bell();
|
|
@@ -1084,7 +1082,7 @@ function TextPromptDeclarations() {
|
|
|
1084
1082
|
"protected": true,
|
|
1085
1083
|
parameters: [{
|
|
1086
1084
|
name: "previousValue",
|
|
1087
|
-
type: "
|
|
1085
|
+
type: "string"
|
|
1088
1086
|
}],
|
|
1089
1087
|
children: code`this.#isInvalid = false;
|
|
1090
1088
|
this.cursor = this.displayValue.slice(0, this.cursor).length + 1; `
|
|
@@ -1101,11 +1099,11 @@ function TextPromptDeclarations() {
|
|
|
1101
1099
|
createComponent(Spacing, {}),
|
|
1102
1100
|
createComponent(ClassMethod, {
|
|
1103
1101
|
doc: "A method to validate the prompt input",
|
|
1104
|
-
name: "
|
|
1102
|
+
name: "checkValidations",
|
|
1105
1103
|
override: true,
|
|
1106
1104
|
async: true,
|
|
1107
1105
|
"protected": true,
|
|
1108
|
-
children: code`await super.
|
|
1106
|
+
children: code`await super.checkValidations(this.value);
|
|
1109
1107
|
this.#isInvalid = this.isError; `
|
|
1110
1108
|
}),
|
|
1111
1109
|
createComponent(Spacing, {}),
|
|
@@ -1834,17 +1832,17 @@ function NumericPromptDeclarations() {
|
|
|
1834
1832
|
}
|
|
1835
1833
|
|
|
1836
1834
|
if (config.parse) {
|
|
1837
|
-
this.
|
|
1835
|
+
this.parse = config.parse.bind(this);
|
|
1838
1836
|
} else {
|
|
1839
|
-
const
|
|
1840
|
-
this.
|
|
1837
|
+
const parse = (value: string) => this.isFloat ? Math.round(Math.pow(10, this.precision) * Number.parseFloat(value)) / Math.pow(10, this.precision) : Number.parseInt(value);
|
|
1838
|
+
this.parse = parse.bind(this);
|
|
1841
1839
|
}
|
|
1842
1840
|
|
|
1843
1841
|
if (config.validate) {
|
|
1844
|
-
this.
|
|
1842
|
+
this.validate = config.validate.bind(this);
|
|
1845
1843
|
} else {
|
|
1846
|
-
const
|
|
1847
|
-
this.
|
|
1844
|
+
const validate = (value: number) => !Number.isNaN(value) && value >= this.min && value <= this.max;
|
|
1845
|
+
this.validate = validate.bind(this);
|
|
1848
1846
|
}
|
|
1849
1847
|
|
|
1850
1848
|
this.changeValue(this.initialValue);
|
|
@@ -1880,7 +1878,7 @@ function NumericPromptDeclarations() {
|
|
|
1880
1878
|
this.displayValue.slice(this.cursor)
|
|
1881
1879
|
}\`;
|
|
1882
1880
|
|
|
1883
|
-
let value = this.
|
|
1881
|
+
let value = this.parse(displayValue);
|
|
1884
1882
|
if (!Number.isNaN(value)) {
|
|
1885
1883
|
|
|
1886
1884
|
value = Math.min(value, this.max);
|
|
@@ -1903,7 +1901,7 @@ function NumericPromptDeclarations() {
|
|
|
1903
1901
|
"protected": true,
|
|
1904
1902
|
parameters: [{
|
|
1905
1903
|
name: "previousValue",
|
|
1906
|
-
type: "
|
|
1904
|
+
type: "number"
|
|
1907
1905
|
}],
|
|
1908
1906
|
children: code`this.#isInvalid = false;
|
|
1909
1907
|
this.cursor = this.displayValue.slice(0, this.cursor).length + 1; `
|
|
@@ -1911,11 +1909,11 @@ function NumericPromptDeclarations() {
|
|
|
1911
1909
|
createComponent(Spacing, {}),
|
|
1912
1910
|
createComponent(ClassMethod, {
|
|
1913
1911
|
doc: "A method to validate the prompt input",
|
|
1914
|
-
name: "
|
|
1912
|
+
name: "checkValidations",
|
|
1915
1913
|
override: true,
|
|
1916
1914
|
async: true,
|
|
1917
1915
|
"protected": true,
|
|
1918
|
-
children: code`await super.
|
|
1916
|
+
children: code`await super.checkValidations(this.value);
|
|
1919
1917
|
this.#isInvalid = this.isError; `
|
|
1920
1918
|
}),
|
|
1921
1919
|
createComponent(Spacing, {}),
|
|
@@ -2501,16 +2499,14 @@ function ConfirmPromptDeclarations() {
|
|
|
2501
2499
|
}
|
|
2502
2500
|
|
|
2503
2501
|
if (char.toLowerCase() === "y" || char.toLowerCase() === "t" || char.toLowerCase() === "0") {
|
|
2504
|
-
this.
|
|
2502
|
+
this.changeValue(true);
|
|
2505
2503
|
return this.submit();
|
|
2506
2504
|
} else if (char.toLowerCase() === "n" || char.toLowerCase() === "f" || char.toLowerCase() === "1") {
|
|
2507
|
-
this.
|
|
2505
|
+
this.changeValue(false);
|
|
2508
2506
|
return this.submit();
|
|
2509
2507
|
} else {
|
|
2510
2508
|
return this.bell();
|
|
2511
|
-
}
|
|
2512
|
-
|
|
2513
|
-
this.sync(); `
|
|
2509
|
+
} `
|
|
2514
2510
|
}),
|
|
2515
2511
|
createComponent(Spacing, {}),
|
|
2516
2512
|
createComponent(ClassMethod, {
|
|
@@ -2652,6 +2648,50 @@ run(); ` }),
|
|
|
2652
2648
|
})
|
|
2653
2649
|
];
|
|
2654
2650
|
}
|
|
2651
|
+
function WaitForKeyPressDeclaration() {
|
|
2652
|
+
return [createComponent(TSDoc, {
|
|
2653
|
+
heading: "A function to create and run a wait-for-key-press prompt, which returns a promise that resolves when any key is pressed or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
2654
|
+
get children() {
|
|
2655
|
+
return [
|
|
2656
|
+
createComponent(TSDocRemarks, { children: code`This function creates an instance of the Prompt class with a custom onKeyPress handler that resolves the promise when any key is pressed. It sets up event listeners for state updates and cancellation to handle the prompt interactions and return the appropriate results. The wait-for-key-press prompt is useful for scenarios where you want to pause execution until the user presses any key, such as waiting for user input before proceeding with a task.` }),
|
|
2657
|
+
createComponent(Spacing, {}),
|
|
2658
|
+
createComponent(TSDocExample, { children: `import { waitForKeyPress } from "shell-shock:prompts";
|
|
2659
|
+
|
|
2660
|
+
async function run() {
|
|
2661
|
+
const result = await waitForKeyPress();
|
|
2662
|
+
console.log("A key was pressed!");
|
|
2663
|
+
}
|
|
2664
|
+
|
|
2665
|
+
run(); ` }),
|
|
2666
|
+
createComponent(Spacing, {}),
|
|
2667
|
+
createComponent(TSDocParam, {
|
|
2668
|
+
name: "timeout",
|
|
2669
|
+
children: `The amount of time in milliseconds to wait before automatically resolving the prompt, defaults to 2 hours (7200000 ms)`
|
|
2670
|
+
}),
|
|
2671
|
+
createComponent(TSDocReturns, { children: `A promise that resolves when any key is pressed` })
|
|
2672
|
+
];
|
|
2673
|
+
}
|
|
2674
|
+
}), createComponent(FunctionDeclaration, {
|
|
2675
|
+
name: "waitForKeyPress",
|
|
2676
|
+
"export": true,
|
|
2677
|
+
parameters: [{
|
|
2678
|
+
name: "timeout",
|
|
2679
|
+
default: "7200000"
|
|
2680
|
+
}],
|
|
2681
|
+
children: code`process.stdin.setRawMode(true);
|
|
2682
|
+
return new Promise(resolve => process.stdin.once("data", () => {
|
|
2683
|
+
if (timeout >= 0) {
|
|
2684
|
+
setTimeout(() => {
|
|
2685
|
+
process.stdin.setRawMode(false);
|
|
2686
|
+
resolve(void 0);
|
|
2687
|
+
}, timeout);
|
|
2688
|
+
}
|
|
2689
|
+
|
|
2690
|
+
process.stdin.setRawMode(false);
|
|
2691
|
+
resolve(void 0);
|
|
2692
|
+
})); `
|
|
2693
|
+
})];
|
|
2694
|
+
}
|
|
2655
2695
|
/**
|
|
2656
2696
|
* A built-in prompts module for Shell Shock.
|
|
2657
2697
|
*/
|
|
@@ -2705,6 +2745,8 @@ function PromptsBuiltin(props) {
|
|
|
2705
2745
|
createComponent(Spacing, {}),
|
|
2706
2746
|
createComponent(ConfirmPromptDeclarations, {}),
|
|
2707
2747
|
createComponent(Spacing, {}),
|
|
2748
|
+
createComponent(WaitForKeyPressDeclaration, {}),
|
|
2749
|
+
createComponent(Spacing, {}),
|
|
2708
2750
|
createComponent(Show, {
|
|
2709
2751
|
get when() {
|
|
2710
2752
|
return Boolean(children);
|
|
@@ -2717,5 +2759,5 @@ function PromptsBuiltin(props) {
|
|
|
2717
2759
|
}
|
|
2718
2760
|
|
|
2719
2761
|
//#endregion
|
|
2720
|
-
export { BasePromptDeclarations, ConfirmPromptDeclarations, NumericPromptDeclarations, PasswordPromptDeclaration, PromptsBuiltin, SelectPromptDeclarations, TextPromptDeclarations, TogglePromptDeclarations };
|
|
2762
|
+
export { BasePromptDeclarations, ConfirmPromptDeclarations, NumericPromptDeclarations, PasswordPromptDeclaration, PromptsBuiltin, SelectPromptDeclarations, TextPromptDeclarations, TogglePromptDeclarations, WaitForKeyPressDeclaration };
|
|
2721
2763
|
//# sourceMappingURL=prompts-builtin.mjs.map
|