@shell-shock/plugin-prompts 0.3.49 → 0.3.52
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/_virtual/_rolldown/runtime.cjs +29 -1
- package/dist/components/index.cjs +13 -1
- package/dist/components/index.d.cts +2 -2
- package/dist/components/index.d.mts +2 -2
- package/dist/components/index.mjs +3 -1
- package/dist/components/prompts-builtin.cjs +2446 -80
- package/dist/components/prompts-builtin.d.cts +14 -11
- package/dist/components/prompts-builtin.d.cts.map +1 -1
- package/dist/components/prompts-builtin.d.mts +14 -11
- package/dist/components/prompts-builtin.d.mts.map +1 -1
- package/dist/components/prompts-builtin.mjs +2434 -80
- package/dist/components/prompts-builtin.mjs.map +1 -1
- package/dist/index.cjs +30 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +26 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types/index.mjs +1 -1
- package/dist/types/plugin.d.cts.map +1 -1
- package/dist/types/plugin.d.mts.map +1 -1
- package/dist/types/plugin.mjs +1 -1
- package/package.json +12 -12
|
@@ -1,4 +1,399 @@
|
|
|
1
|
-
import{createComponent
|
|
1
|
+
import { createComponent, createIntrinsic, mergeProps } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Show, code, splitProps } from "@alloy-js/core";
|
|
3
|
+
import { FunctionDeclaration, VarDeclaration } from "@alloy-js/typescript";
|
|
4
|
+
import { Spacing } from "@powerlines/plugin-alloy/core/components/spacing";
|
|
5
|
+
import { BuiltinFile } from "@powerlines/plugin-alloy/typescript/components/builtin-file";
|
|
6
|
+
import { ClassDeclaration, ClassField, ClassMethod, ClassPropertyGet } from "@powerlines/plugin-alloy/typescript/components/class-declaration";
|
|
7
|
+
import { InterfaceDeclaration, InterfaceMember } from "@powerlines/plugin-alloy/typescript/components/interface-declaration";
|
|
8
|
+
import { TSDoc, TSDocDefaultValue, TSDocExample, TSDocParam, TSDocRemarks, TSDocReturns } from "@powerlines/plugin-alloy/typescript/components/tsdoc";
|
|
9
|
+
import { TypeDeclaration } from "@powerlines/plugin-alloy/typescript/components/type-declaration";
|
|
10
|
+
import { useTheme } from "@shell-shock/plugin-theme/contexts/theme";
|
|
11
|
+
import defu from "defu";
|
|
12
|
+
|
|
13
|
+
//#region src/components/prompts-builtin.tsx
|
|
14
|
+
/**
|
|
15
|
+
* A component that generates TypeScript declarations for built-in prompt types and related utilities, such as the base Prompt class, specific prompt types like TextPrompt and SelectPrompt, and utility functions for handling prompt cancellations. This component serves as a central place to define the types and interfaces for prompts used in the Shell Shock CLI, providing a consistent API for creating and managing prompts throughout the application.
|
|
16
|
+
*/
|
|
17
|
+
function BasePromptDeclarations() {
|
|
18
|
+
const theme = useTheme();
|
|
19
|
+
return [
|
|
20
|
+
createComponent(TypeDeclaration, {
|
|
21
|
+
"export": true,
|
|
22
|
+
name: "PromptParser",
|
|
23
|
+
typeParameters: [{
|
|
24
|
+
name: "TValue",
|
|
25
|
+
default: "string"
|
|
26
|
+
}],
|
|
27
|
+
doc: "A type for a custom prompt input parser, which can be used to create custom input styles for prompts. The function should return the parsed value for the given input string.",
|
|
28
|
+
children: code`(this: Prompt<TValue>, input: string) => TValue; `
|
|
29
|
+
}),
|
|
30
|
+
createComponent(Spacing, {}),
|
|
31
|
+
createComponent(TypeDeclaration, {
|
|
32
|
+
"export": true,
|
|
33
|
+
name: "PromptFormatter",
|
|
34
|
+
typeParameters: [{
|
|
35
|
+
name: "TValue",
|
|
36
|
+
default: "string"
|
|
37
|
+
}],
|
|
38
|
+
doc: "A type for a custom prompt input formatter, which can be used to create custom display styles for prompts. The function should return the formatted string to display for the given input value.",
|
|
39
|
+
children: code`(this: Prompt<TValue>, input: TValue) => string; `
|
|
40
|
+
}),
|
|
41
|
+
createComponent(Spacing, {}),
|
|
42
|
+
createComponent(FunctionDeclaration, {
|
|
43
|
+
"export": true,
|
|
44
|
+
name: "noMask",
|
|
45
|
+
doc: "A built-in prompt mask function that just returns the input as is, making it invisible",
|
|
46
|
+
parameters: [{
|
|
47
|
+
name: "input",
|
|
48
|
+
type: "string"
|
|
49
|
+
}],
|
|
50
|
+
returnType: "string",
|
|
51
|
+
children: code`return input; `
|
|
52
|
+
}),
|
|
53
|
+
createComponent(Spacing, {}),
|
|
54
|
+
createComponent(FunctionDeclaration, {
|
|
55
|
+
"export": true,
|
|
56
|
+
name: "invisibleMask",
|
|
57
|
+
doc: "A built-in prompt mask function that makes input invisible",
|
|
58
|
+
parameters: [{
|
|
59
|
+
name: "input",
|
|
60
|
+
type: "string"
|
|
61
|
+
}],
|
|
62
|
+
returnType: "string",
|
|
63
|
+
children: code`return " ".repeat(input.length); `
|
|
64
|
+
}),
|
|
65
|
+
createComponent(Spacing, {}),
|
|
66
|
+
createComponent(InterfaceDeclaration, {
|
|
67
|
+
"export": true,
|
|
68
|
+
name: "PromptState",
|
|
69
|
+
doc: "The current state of a prompt",
|
|
70
|
+
typeParameters: [{
|
|
71
|
+
name: "TValue",
|
|
72
|
+
default: "string"
|
|
73
|
+
}],
|
|
74
|
+
get children() {
|
|
75
|
+
return [
|
|
76
|
+
createComponent(InterfaceMember, {
|
|
77
|
+
name: "value",
|
|
78
|
+
type: "TValue",
|
|
79
|
+
doc: "The current value of the prompt"
|
|
80
|
+
}),
|
|
81
|
+
createComponent(Spacing, {}),
|
|
82
|
+
createComponent(InterfaceMember, {
|
|
83
|
+
name: "isError",
|
|
84
|
+
type: "boolean",
|
|
85
|
+
doc: "Indicates whether the prompt is in an error state"
|
|
86
|
+
}),
|
|
87
|
+
createComponent(Spacing, {}),
|
|
88
|
+
createComponent(InterfaceMember, {
|
|
89
|
+
name: "errorMessage",
|
|
90
|
+
required: false,
|
|
91
|
+
type: "string",
|
|
92
|
+
doc: "If the prompt is in an error state, this will contain the error message to display"
|
|
93
|
+
}),
|
|
94
|
+
createComponent(Spacing, {}),
|
|
95
|
+
createComponent(InterfaceMember, {
|
|
96
|
+
name: "isSubmitted",
|
|
97
|
+
type: "boolean",
|
|
98
|
+
doc: "Indicates whether the prompt is submitted"
|
|
99
|
+
}),
|
|
100
|
+
createComponent(Spacing, {}),
|
|
101
|
+
createComponent(InterfaceMember, {
|
|
102
|
+
name: "isCancelled",
|
|
103
|
+
type: "boolean",
|
|
104
|
+
doc: "Indicates whether the prompt is cancelled"
|
|
105
|
+
}),
|
|
106
|
+
createComponent(Spacing, {}),
|
|
107
|
+
createComponent(InterfaceMember, {
|
|
108
|
+
name: "isCompleted",
|
|
109
|
+
type: "boolean",
|
|
110
|
+
doc: "Indicates whether the prompt is completed, which can be used to indicate that the prompt interaction is finished regardless of whether it was submitted or cancelled"
|
|
111
|
+
}),
|
|
112
|
+
createComponent(Spacing, {})
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
116
|
+
createComponent(Spacing, {}),
|
|
117
|
+
createComponent(InterfaceDeclaration, {
|
|
118
|
+
name: "PromptConfig",
|
|
119
|
+
doc: "Configuration options for creating a prompt",
|
|
120
|
+
typeParameters: [{
|
|
121
|
+
name: "TValue",
|
|
122
|
+
default: "string"
|
|
123
|
+
}],
|
|
124
|
+
get children() {
|
|
125
|
+
return [
|
|
126
|
+
createComponent(InterfaceMember, {
|
|
127
|
+
name: "input",
|
|
128
|
+
required: false,
|
|
129
|
+
type: "NodeJS.ReadStream",
|
|
130
|
+
doc: "The readable stream to use for prompt input, defaults to process.stdin"
|
|
131
|
+
}),
|
|
132
|
+
createComponent(Spacing, {}),
|
|
133
|
+
createComponent(InterfaceMember, {
|
|
134
|
+
name: "output",
|
|
135
|
+
required: false,
|
|
136
|
+
type: "NodeJS.WriteStream",
|
|
137
|
+
doc: "The writable stream to use for prompt output, defaults to process.stdout"
|
|
138
|
+
}),
|
|
139
|
+
createComponent(Spacing, {}),
|
|
140
|
+
createComponent(InterfaceMember, {
|
|
141
|
+
name: "message",
|
|
142
|
+
type: "string",
|
|
143
|
+
doc: "The prompt message to display"
|
|
144
|
+
}),
|
|
145
|
+
createComponent(Spacing, {}),
|
|
146
|
+
createComponent(InterfaceMember, {
|
|
147
|
+
name: "description",
|
|
148
|
+
required: false,
|
|
149
|
+
type: "string",
|
|
150
|
+
doc: "The prompt description message to display"
|
|
151
|
+
}),
|
|
152
|
+
createComponent(Spacing, {}),
|
|
153
|
+
createComponent(InterfaceMember, {
|
|
154
|
+
name: "initialValue",
|
|
155
|
+
required: false,
|
|
156
|
+
type: "TValue",
|
|
157
|
+
doc: "The initial value of the prompt"
|
|
158
|
+
}),
|
|
159
|
+
createComponent(Spacing, {}),
|
|
160
|
+
createComponent(InterfaceMember, {
|
|
161
|
+
name: "validate",
|
|
162
|
+
required: false,
|
|
163
|
+
type: "(value: TValue) => boolean | string | null | undefined | Promise<boolean | string | null | undefined>",
|
|
164
|
+
doc: "A validation function that returns true if the input is valid, false or a string error message if the input is invalid"
|
|
165
|
+
}),
|
|
166
|
+
createComponent(Spacing, {}),
|
|
167
|
+
createComponent(InterfaceMember, {
|
|
168
|
+
name: "parse",
|
|
169
|
+
required: false,
|
|
170
|
+
type: "PromptParser<TValue>",
|
|
171
|
+
doc: "A function that parses the input value and returns the parsed result or throws an error if the input is invalid"
|
|
172
|
+
}),
|
|
173
|
+
createComponent(Spacing, {}),
|
|
174
|
+
createComponent(InterfaceMember, {
|
|
175
|
+
name: "format",
|
|
176
|
+
required: false,
|
|
177
|
+
type: "PromptFormatter<TValue>",
|
|
178
|
+
doc: "A function that formats the input value and returns the formatted result or throws an error if the input is invalid"
|
|
179
|
+
}),
|
|
180
|
+
createComponent(Spacing, {}),
|
|
181
|
+
createComponent(InterfaceMember, {
|
|
182
|
+
name: "mask",
|
|
183
|
+
required: false,
|
|
184
|
+
type: "(input: string) => string",
|
|
185
|
+
doc: "A function that masks the input value and returns the masked result. This can be used to create password inputs or other sensitive input types where the actual input value should not be displayed. If not provided, the prompt will display the input as is without masking."
|
|
186
|
+
}),
|
|
187
|
+
createComponent(Spacing, {}),
|
|
188
|
+
createComponent(InterfaceMember, {
|
|
189
|
+
name: "maskCompleted",
|
|
190
|
+
required: false,
|
|
191
|
+
type: "(input: string) => string",
|
|
192
|
+
doc: "A function that masks the value submitted by the user so that it can then be used in the console output or elsewhere without exposing sensitive information. If not provided, the prompt will use the same mask function for both input and submitted value masking."
|
|
193
|
+
}),
|
|
194
|
+
createComponent(Spacing, {}),
|
|
195
|
+
createComponent(InterfaceMember, {
|
|
196
|
+
name: "defaultErrorMessage",
|
|
197
|
+
required: false,
|
|
198
|
+
type: "string",
|
|
199
|
+
doc: "The default error message to display when validation fails"
|
|
200
|
+
}),
|
|
201
|
+
createComponent(Spacing, {}),
|
|
202
|
+
createComponent(InterfaceMember, {
|
|
203
|
+
name: "timeout",
|
|
204
|
+
required: false,
|
|
205
|
+
type: "number",
|
|
206
|
+
doc: "The timeout duration in milliseconds for the prompt. If none is provided, the prompt will not time out."
|
|
207
|
+
})
|
|
208
|
+
];
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
createComponent(Spacing, {}),
|
|
212
|
+
createComponent(ClassDeclaration, {
|
|
213
|
+
abstract: true,
|
|
214
|
+
name: "Prompt",
|
|
215
|
+
doc: "Base prompt class that other prompt types can extend from",
|
|
216
|
+
"extends": "EventEmitter",
|
|
217
|
+
typeParameters: [{
|
|
218
|
+
name: "TValue",
|
|
219
|
+
default: "string"
|
|
220
|
+
}],
|
|
221
|
+
get children() {
|
|
222
|
+
return [
|
|
223
|
+
createComponent(ClassField, {
|
|
224
|
+
name: "readline",
|
|
225
|
+
isPrivateMember: true,
|
|
226
|
+
type: "Interface"
|
|
227
|
+
}),
|
|
228
|
+
createIntrinsic("hbr", {}),
|
|
229
|
+
createComponent(ClassField, {
|
|
230
|
+
name: "value",
|
|
231
|
+
isPrivateMember: true,
|
|
232
|
+
type: "TValue | undefined"
|
|
233
|
+
}),
|
|
234
|
+
createIntrinsic("hbr", {}),
|
|
235
|
+
createComponent(ClassField, {
|
|
236
|
+
name: "isKeyPressed",
|
|
237
|
+
isPrivateMember: true,
|
|
238
|
+
type: "boolean",
|
|
239
|
+
children: code`false; `
|
|
240
|
+
}),
|
|
241
|
+
createIntrinsic("hbr", {}),
|
|
242
|
+
createComponent(ClassField, {
|
|
243
|
+
name: "isDirty",
|
|
244
|
+
isPrivateMember: true,
|
|
245
|
+
type: "boolean",
|
|
246
|
+
children: code`false; `
|
|
247
|
+
}),
|
|
248
|
+
createIntrinsic("hbr", {}),
|
|
249
|
+
createComponent(ClassField, {
|
|
250
|
+
name: "isClosed",
|
|
251
|
+
isPrivateMember: true,
|
|
252
|
+
type: "boolean",
|
|
253
|
+
children: code`false; `
|
|
254
|
+
}),
|
|
255
|
+
createComponent(Spacing, {}),
|
|
256
|
+
createComponent(ClassField, {
|
|
257
|
+
name: "initialValue",
|
|
258
|
+
abstract: true,
|
|
259
|
+
"protected": true,
|
|
260
|
+
type: "TValue"
|
|
261
|
+
}),
|
|
262
|
+
createIntrinsic("hbr", {}),
|
|
263
|
+
createComponent(ClassField, {
|
|
264
|
+
name: "input",
|
|
265
|
+
"protected": true,
|
|
266
|
+
type: "NodeJS.ReadStream",
|
|
267
|
+
children: code`process.stdin; `
|
|
268
|
+
}),
|
|
269
|
+
createIntrinsic("hbr", {}),
|
|
270
|
+
createComponent(ClassField, {
|
|
271
|
+
name: "output",
|
|
272
|
+
"protected": true,
|
|
273
|
+
type: "NodeJS.WriteStream",
|
|
274
|
+
children: code`process.stdout; `
|
|
275
|
+
}),
|
|
276
|
+
createIntrinsic("hbr", {}),
|
|
277
|
+
createComponent(ClassField, {
|
|
278
|
+
name: "message",
|
|
279
|
+
"protected": true,
|
|
280
|
+
type: "string",
|
|
281
|
+
children: code`""; `
|
|
282
|
+
}),
|
|
283
|
+
createIntrinsic("hbr", {}),
|
|
284
|
+
createComponent(ClassField, {
|
|
285
|
+
name: "description",
|
|
286
|
+
"protected": true,
|
|
287
|
+
type: "string",
|
|
288
|
+
children: code`""; `
|
|
289
|
+
}),
|
|
290
|
+
createIntrinsic("hbr", {}),
|
|
291
|
+
createComponent(ClassField, {
|
|
292
|
+
name: "errorMessage",
|
|
293
|
+
"protected": true,
|
|
294
|
+
type: "string | null",
|
|
295
|
+
children: code`null; `
|
|
296
|
+
}),
|
|
297
|
+
createIntrinsic("hbr", {}),
|
|
298
|
+
createComponent(ClassField, {
|
|
299
|
+
name: "defaultErrorMessage",
|
|
300
|
+
"protected": true,
|
|
301
|
+
type: "string",
|
|
302
|
+
children: code`"An invalid value was provided"; `
|
|
303
|
+
}),
|
|
304
|
+
createIntrinsic("hbr", {}),
|
|
305
|
+
createComponent(ClassField, {
|
|
306
|
+
name: "isSubmitted",
|
|
307
|
+
"protected": true,
|
|
308
|
+
type: "boolean",
|
|
309
|
+
children: code`false; `
|
|
310
|
+
}),
|
|
311
|
+
createIntrinsic("hbr", {}),
|
|
312
|
+
createComponent(ClassField, {
|
|
313
|
+
name: "isCancelled",
|
|
314
|
+
"protected": true,
|
|
315
|
+
type: "boolean",
|
|
316
|
+
children: code`false; `
|
|
317
|
+
}),
|
|
318
|
+
createIntrinsic("hbr", {}),
|
|
319
|
+
createComponent(ClassField, {
|
|
320
|
+
name: "isInitial",
|
|
321
|
+
"protected": true,
|
|
322
|
+
type: "boolean",
|
|
323
|
+
children: code`true; `
|
|
324
|
+
}),
|
|
325
|
+
createIntrinsic("hbr", {}),
|
|
326
|
+
createComponent(ClassField, {
|
|
327
|
+
name: "consoleOutput",
|
|
328
|
+
"protected": true,
|
|
329
|
+
type: "string",
|
|
330
|
+
children: code`""; `
|
|
331
|
+
}),
|
|
332
|
+
createIntrinsic("hbr", {}),
|
|
333
|
+
createComponent(ClassField, {
|
|
334
|
+
name: "consoleStatus",
|
|
335
|
+
"protected": true,
|
|
336
|
+
type: "string",
|
|
337
|
+
children: code`""; `
|
|
338
|
+
}),
|
|
339
|
+
createIntrinsic("hbr", {}),
|
|
340
|
+
createComponent(ClassField, {
|
|
341
|
+
name: "displayValue",
|
|
342
|
+
"protected": true,
|
|
343
|
+
type: "string",
|
|
344
|
+
children: code`""; `
|
|
345
|
+
}),
|
|
346
|
+
createIntrinsic("hbr", {}),
|
|
347
|
+
createComponent(ClassField, {
|
|
348
|
+
name: "validate",
|
|
349
|
+
"protected": true,
|
|
350
|
+
type: "(value: TValue) => boolean | string | null | undefined | Promise<boolean | string | null | undefined>",
|
|
351
|
+
children: code`() => true; `
|
|
352
|
+
}),
|
|
353
|
+
createIntrinsic("hbr", {}),
|
|
354
|
+
createComponent(ClassField, {
|
|
355
|
+
name: "parse",
|
|
356
|
+
"protected": true,
|
|
357
|
+
type: "PromptParser<TValue>",
|
|
358
|
+
children: code`(value: string) => value as TValue; `
|
|
359
|
+
}),
|
|
360
|
+
createIntrinsic("hbr", {}),
|
|
361
|
+
createComponent(ClassField, {
|
|
362
|
+
name: "format",
|
|
363
|
+
"protected": true,
|
|
364
|
+
type: "PromptFormatter<TValue>",
|
|
365
|
+
children: code`(value: TValue) => String(value); `
|
|
366
|
+
}),
|
|
367
|
+
createIntrinsic("hbr", {}),
|
|
368
|
+
createComponent(ClassField, {
|
|
369
|
+
name: "mask",
|
|
370
|
+
"protected": true,
|
|
371
|
+
type: "(input: string) => string",
|
|
372
|
+
children: code`noMask; `
|
|
373
|
+
}),
|
|
374
|
+
createIntrinsic("hbr", {}),
|
|
375
|
+
createComponent(ClassField, {
|
|
376
|
+
name: "maskCompleted",
|
|
377
|
+
"protected": true,
|
|
378
|
+
type: "(input: string) => string",
|
|
379
|
+
children: code`this.mask; `
|
|
380
|
+
}),
|
|
381
|
+
createIntrinsic("hbr", {}),
|
|
382
|
+
createComponent(ClassField, {
|
|
383
|
+
name: "cursor",
|
|
384
|
+
"protected": true,
|
|
385
|
+
type: "number",
|
|
386
|
+
children: code`0; `
|
|
387
|
+
}),
|
|
388
|
+
createIntrinsic("hbr", {}),
|
|
389
|
+
createComponent(ClassField, {
|
|
390
|
+
name: "cursorHidden",
|
|
391
|
+
"protected": true,
|
|
392
|
+
type: "boolean",
|
|
393
|
+
children: code`false; `
|
|
394
|
+
}),
|
|
395
|
+
createComponent(Spacing, {}),
|
|
396
|
+
code`constructor(protected config: PromptConfig<TValue>) {
|
|
2
397
|
super();
|
|
3
398
|
|
|
4
399
|
if (config.input) {
|
|
@@ -55,7 +450,49 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
55
450
|
|
|
56
451
|
[Symbol.dispose]() {
|
|
57
452
|
this.close();
|
|
58
|
-
} `,
|
|
453
|
+
} `,
|
|
454
|
+
createComponent(Spacing, {}),
|
|
455
|
+
createComponent(ClassPropertyGet, {
|
|
456
|
+
"public": true,
|
|
457
|
+
name: "value",
|
|
458
|
+
type: "TValue",
|
|
459
|
+
doc: "A getter for the prompt value that returns the current value or the initial value if the current value is not set",
|
|
460
|
+
children: code`return this.#value || this.initialValue; `
|
|
461
|
+
}),
|
|
462
|
+
createComponent(Spacing, {}),
|
|
463
|
+
createComponent(ClassPropertyGet, {
|
|
464
|
+
"public": true,
|
|
465
|
+
name: "isError",
|
|
466
|
+
type: "boolean",
|
|
467
|
+
children: code`return !!this.errorMessage; `
|
|
468
|
+
}),
|
|
469
|
+
createComponent(Spacing, {}),
|
|
470
|
+
createComponent(ClassPropertyGet, {
|
|
471
|
+
"protected": true,
|
|
472
|
+
name: "isSelect",
|
|
473
|
+
type: "boolean",
|
|
474
|
+
children: code`return false; `
|
|
475
|
+
}),
|
|
476
|
+
createComponent(Spacing, {}),
|
|
477
|
+
createComponent(ClassPropertyGet, {
|
|
478
|
+
"protected": true,
|
|
479
|
+
name: "isCompleted",
|
|
480
|
+
type: "boolean",
|
|
481
|
+
children: code`return this.isCancelled || this.isSubmitted; `
|
|
482
|
+
}),
|
|
483
|
+
createComponent(Spacing, {}),
|
|
484
|
+
createComponent(ClassPropertyGet, {
|
|
485
|
+
"protected": true,
|
|
486
|
+
name: "isPlaceholder",
|
|
487
|
+
type: "boolean",
|
|
488
|
+
children: code`return (this.displayValue === this.format(this.initialValue) && !this.#isDirty) || !this.#isKeyPressed; `
|
|
489
|
+
}),
|
|
490
|
+
createComponent(Spacing, {}),
|
|
491
|
+
createComponent(ClassPropertyGet, {
|
|
492
|
+
"protected": true,
|
|
493
|
+
name: "status",
|
|
494
|
+
type: "string",
|
|
495
|
+
children: code`return this.isSubmitted ? "" : \` \\n \${
|
|
59
496
|
italic(
|
|
60
497
|
this.isError
|
|
61
498
|
? textColors.prompt.description.error(splitText(this.errorMessage, "3/4").join("\\n"))
|
|
@@ -65,7 +502,34 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
65
502
|
? textColors.prompt.description.active(splitText(this.description, "3/4").join("\\n"))
|
|
66
503
|
: ""
|
|
67
504
|
)
|
|
68
|
-
}\`; `
|
|
505
|
+
}\`; `
|
|
506
|
+
}),
|
|
507
|
+
createComponent(Spacing, {}),
|
|
508
|
+
createComponent(ClassPropertyGet, {
|
|
509
|
+
doc: "A property to check if the cursor is at the start",
|
|
510
|
+
name: "isCursorAtStart",
|
|
511
|
+
"protected": true,
|
|
512
|
+
type: "boolean",
|
|
513
|
+
children: code`return this.cursor <= 0 || (this.isPlaceholder && this.cursor <= 1); `
|
|
514
|
+
}),
|
|
515
|
+
createComponent(Spacing, {}),
|
|
516
|
+
createComponent(ClassPropertyGet, {
|
|
517
|
+
doc: "A property to check if the cursor is at the end",
|
|
518
|
+
name: "isCursorAtEnd",
|
|
519
|
+
"protected": true,
|
|
520
|
+
type: "boolean",
|
|
521
|
+
children: code`return this.cursor >= this.displayValue.length || (this.isPlaceholder && this.cursor >= this.displayValue.length - 1); `
|
|
522
|
+
}),
|
|
523
|
+
createComponent(Spacing, {}),
|
|
524
|
+
createComponent(ClassMethod, {
|
|
525
|
+
doc: "A method to change the prompt value, which also updates the display value and fires a state update event. This method can be called by subclasses whenever the prompt value needs to be updated based on user input or other interactions.",
|
|
526
|
+
name: "changeValue",
|
|
527
|
+
"protected": true,
|
|
528
|
+
parameters: [{
|
|
529
|
+
name: "value",
|
|
530
|
+
type: "TValue"
|
|
531
|
+
}],
|
|
532
|
+
children: code`const previousValue = this.value;
|
|
69
533
|
|
|
70
534
|
let updatedValue = value;
|
|
71
535
|
if (value === undefined || value === "") {
|
|
@@ -86,7 +550,14 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
86
550
|
Promise.resolve(this.checkValidations(updatedValue)).then(() => this.sync());
|
|
87
551
|
}, 0);
|
|
88
552
|
|
|
89
|
-
this.sync(); `
|
|
553
|
+
this.sync(); `
|
|
554
|
+
}),
|
|
555
|
+
createComponent(Spacing, {}),
|
|
556
|
+
createComponent(ClassMethod, {
|
|
557
|
+
doc: "A method to emit the current state",
|
|
558
|
+
name: "sync",
|
|
559
|
+
"protected": true,
|
|
560
|
+
children: code`this.emit("state", {
|
|
90
561
|
value: this.value,
|
|
91
562
|
errorMessage: this.errorMessage,
|
|
92
563
|
isError: this.isError,
|
|
@@ -94,7 +565,22 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
94
565
|
isCancelled: this.isCancelled,
|
|
95
566
|
isCompleted: this.isCompleted
|
|
96
567
|
});
|
|
97
|
-
this.render(); `
|
|
568
|
+
this.render(); `
|
|
569
|
+
}),
|
|
570
|
+
createComponent(Spacing, {}),
|
|
571
|
+
createComponent(ClassMethod, {
|
|
572
|
+
doc: "A method to ring the bell",
|
|
573
|
+
name: "bell",
|
|
574
|
+
"protected": true,
|
|
575
|
+
children: code`this.output.write(beep); `
|
|
576
|
+
}),
|
|
577
|
+
createComponent(Spacing, {}),
|
|
578
|
+
createComponent(ClassMethod, {
|
|
579
|
+
doc: "A method to render the prompt",
|
|
580
|
+
name: "onRender",
|
|
581
|
+
"protected": true,
|
|
582
|
+
returnType: "string",
|
|
583
|
+
children: code`return this.isPlaceholder
|
|
98
584
|
? textColors.prompt.input.disabled(this.displayValue)
|
|
99
585
|
: this.isError
|
|
100
586
|
? textColors.prompt.input.error(this.displayValue)
|
|
@@ -102,12 +588,45 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
102
588
|
? textColors.prompt.input.submitted(this.maskCompleted(this.displayValue))
|
|
103
589
|
: this.isCancelled
|
|
104
590
|
? textColors.prompt.input.cancelled(this.maskCompleted(this.displayValue))
|
|
105
|
-
: bold(textColors.prompt.input.active(this.displayValue)); `
|
|
591
|
+
: bold(textColors.prompt.input.active(this.displayValue)); `
|
|
592
|
+
}),
|
|
593
|
+
createComponent(Spacing, {}),
|
|
594
|
+
createComponent(ClassMethod, {
|
|
595
|
+
doc: "A method to handle changes in the prompt value",
|
|
596
|
+
name: "onChange",
|
|
597
|
+
"protected": true,
|
|
598
|
+
parameters: [{
|
|
599
|
+
name: "previousValue",
|
|
600
|
+
type: "TValue"
|
|
601
|
+
}],
|
|
602
|
+
children: code` // can be implemented by subclasses to handle value changes if needed, this method is called whenever the prompt value changes and receives the previous value as an argument for reference`
|
|
603
|
+
}),
|
|
604
|
+
createComponent(Spacing, {}),
|
|
605
|
+
createComponent(ClassMethod, {
|
|
606
|
+
doc: "A method to handle key press events and determine the corresponding action",
|
|
607
|
+
name: "onKeyPress",
|
|
608
|
+
"protected": true,
|
|
609
|
+
parameters: [{
|
|
610
|
+
name: "char",
|
|
611
|
+
type: "string"
|
|
612
|
+
}, {
|
|
613
|
+
name: "key",
|
|
614
|
+
type: "Key"
|
|
615
|
+
}],
|
|
616
|
+
children: code`const action = this.getAction(key);
|
|
106
617
|
if (!action) {
|
|
107
618
|
this.bell();
|
|
108
619
|
} else if (typeof (this as any)[action] === "function") {
|
|
109
620
|
(this as any)[action](key);
|
|
110
|
-
} `
|
|
621
|
+
} `
|
|
622
|
+
}),
|
|
623
|
+
createComponent(Spacing, {}),
|
|
624
|
+
createComponent(ClassMethod, {
|
|
625
|
+
doc: "A method to close the prompt and clean up resources, which also emits a submit or cancel event based on the prompt state. This method should be called when the prompt interaction is finished and the prompt needs to be closed.",
|
|
626
|
+
name: "close",
|
|
627
|
+
async: true,
|
|
628
|
+
"protected": true,
|
|
629
|
+
children: code`if (this.#isClosed) {
|
|
111
630
|
return;
|
|
112
631
|
}
|
|
113
632
|
|
|
@@ -120,14 +639,38 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
120
639
|
|
|
121
640
|
this.#readline.close();
|
|
122
641
|
this.emit(this.isSubmitted ? "submit" : "cancel", this.value);
|
|
123
|
-
this.#isClosed = true; `
|
|
642
|
+
this.#isClosed = true; `
|
|
643
|
+
}),
|
|
644
|
+
createComponent(Spacing, {}),
|
|
645
|
+
createComponent(ClassMethod, {
|
|
646
|
+
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.",
|
|
647
|
+
name: "checkValidations",
|
|
648
|
+
async: true,
|
|
649
|
+
"protected": true,
|
|
650
|
+
parameters: [{
|
|
651
|
+
name: "value",
|
|
652
|
+
type: "TValue"
|
|
653
|
+
}],
|
|
654
|
+
children: code`let result = await this.validate(value);
|
|
124
655
|
if (typeof result === "string") {
|
|
125
656
|
this.errorMessage = result;
|
|
126
657
|
} else if (typeof result === "boolean") {
|
|
127
658
|
this.errorMessage = result ? null : this.defaultErrorMessage;
|
|
128
659
|
} else {
|
|
129
660
|
this.errorMessage = null;
|
|
130
|
-
} `
|
|
661
|
+
} `
|
|
662
|
+
}),
|
|
663
|
+
createComponent(Spacing, {}),
|
|
664
|
+
createComponent(ClassMethod, {
|
|
665
|
+
doc: "A method to route key press events to specific prompt actions based on the key pressed. This method maps various key combinations and keys to corresponding actions that can be handled by the prompt, such as submitting, cancelling, navigating, etc.",
|
|
666
|
+
name: "getAction",
|
|
667
|
+
"protected": true,
|
|
668
|
+
parameters: [{
|
|
669
|
+
name: "key",
|
|
670
|
+
type: "Key"
|
|
671
|
+
}],
|
|
672
|
+
returnType: "string | false",
|
|
673
|
+
children: code`if (key.meta && key.name !== "escape") {
|
|
131
674
|
return false;
|
|
132
675
|
}
|
|
133
676
|
|
|
@@ -157,7 +700,18 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
157
700
|
if (key.name === "right") action = "right";
|
|
158
701
|
if (key.name === "left") action = "left";
|
|
159
702
|
|
|
160
|
-
return action || false; `
|
|
703
|
+
return action || false; `
|
|
704
|
+
}),
|
|
705
|
+
createComponent(Spacing, {}),
|
|
706
|
+
createComponent(ClassMethod, {
|
|
707
|
+
doc: "A method to move the cursor to the left or right by a \\`count\\` of positions",
|
|
708
|
+
name: "moveCursor",
|
|
709
|
+
parameters: [{
|
|
710
|
+
name: "count",
|
|
711
|
+
type: "number"
|
|
712
|
+
}],
|
|
713
|
+
"protected": true,
|
|
714
|
+
children: code`if (this.cursor + count < 0) {
|
|
161
715
|
this.cursor = 0;
|
|
162
716
|
} else if (this.cursor + count > this.displayValue.length) {
|
|
163
717
|
this.cursor = this.displayValue.length;
|
|
@@ -165,7 +719,14 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
165
719
|
this.cursor += count;
|
|
166
720
|
}
|
|
167
721
|
|
|
168
|
-
`
|
|
722
|
+
`
|
|
723
|
+
}),
|
|
724
|
+
createComponent(Spacing, {}),
|
|
725
|
+
createComponent(ClassMethod, {
|
|
726
|
+
doc: "A method to remove the character backward of the cursor",
|
|
727
|
+
name: "backspace",
|
|
728
|
+
"protected": true,
|
|
729
|
+
children: code`if (this.isCursorAtStart) {
|
|
169
730
|
return this.bell();
|
|
170
731
|
}
|
|
171
732
|
|
|
@@ -183,7 +744,14 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
183
744
|
|
|
184
745
|
this.moveCursor(isCursorAtEnd ? -1 : -2);
|
|
185
746
|
|
|
186
|
-
this.sync(); `
|
|
747
|
+
this.sync(); `
|
|
748
|
+
}),
|
|
749
|
+
createComponent(Spacing, {}),
|
|
750
|
+
createComponent(ClassMethod, {
|
|
751
|
+
doc: "A method to remove the character forward of the cursor",
|
|
752
|
+
name: "delete",
|
|
753
|
+
"protected": true,
|
|
754
|
+
children: code`if (this.isCursorAtEnd) {
|
|
187
755
|
return this.bell();
|
|
188
756
|
}
|
|
189
757
|
|
|
@@ -197,20 +765,42 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
197
765
|
this.moveCursor(-1);
|
|
198
766
|
}
|
|
199
767
|
|
|
200
|
-
this.sync(); `
|
|
768
|
+
this.sync(); `
|
|
769
|
+
}),
|
|
770
|
+
createComponent(Spacing, {}),
|
|
771
|
+
createComponent(ClassMethod, {
|
|
772
|
+
doc: "A method to reset the prompt input",
|
|
773
|
+
name: "reset",
|
|
774
|
+
"protected": true,
|
|
775
|
+
children: code`this.changeValue(this.initialValue);
|
|
201
776
|
this.cursor = 0;
|
|
202
777
|
|
|
203
778
|
this.errorMessage = null;
|
|
204
779
|
this.isCancelled = false;
|
|
205
780
|
this.isSubmitted = false;
|
|
206
781
|
|
|
207
|
-
this.sync(); `
|
|
782
|
+
this.sync(); `
|
|
783
|
+
}),
|
|
784
|
+
createComponent(Spacing, {}),
|
|
785
|
+
createComponent(ClassMethod, {
|
|
786
|
+
doc: "A method to cancel the prompt input",
|
|
787
|
+
name: "cancel",
|
|
788
|
+
"protected": true,
|
|
789
|
+
children: code`this.errorMessage = null;
|
|
208
790
|
this.isCancelled = true;
|
|
209
791
|
this.isSubmitted = false;
|
|
210
792
|
|
|
211
793
|
this.sync();
|
|
212
794
|
this.output.write("\\n");
|
|
213
|
-
this.close(); `
|
|
795
|
+
this.close(); `
|
|
796
|
+
}),
|
|
797
|
+
createComponent(Spacing, {}),
|
|
798
|
+
createComponent(ClassMethod, {
|
|
799
|
+
doc: "A method to submit the prompt input",
|
|
800
|
+
name: "submit",
|
|
801
|
+
async: true,
|
|
802
|
+
"protected": true,
|
|
803
|
+
children: code`this.cursor = this.displayValue.length;
|
|
214
804
|
|
|
215
805
|
await this.checkValidations(this.value);
|
|
216
806
|
if (this.isError) {
|
|
@@ -223,7 +813,15 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
223
813
|
this.sync();
|
|
224
814
|
this.output.write("\\n");
|
|
225
815
|
this.close();
|
|
226
|
-
} `
|
|
816
|
+
} `
|
|
817
|
+
}),
|
|
818
|
+
createComponent(Spacing, {}),
|
|
819
|
+
createComponent(ClassMethod, {
|
|
820
|
+
doc: "A method to render the prompt",
|
|
821
|
+
name: "render",
|
|
822
|
+
"private": true,
|
|
823
|
+
get children() {
|
|
824
|
+
return code`if (this.#isClosed) {
|
|
227
825
|
return;
|
|
228
826
|
}
|
|
229
827
|
|
|
@@ -244,12 +842,12 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
244
842
|
|
|
245
843
|
this.consoleOutput = \` \${
|
|
246
844
|
this.isSubmitted
|
|
247
|
-
? textColors.prompt.icon.submitted("${
|
|
845
|
+
? textColors.prompt.icon.submitted("${theme.icons.prompt.submitted}")
|
|
248
846
|
: this.isCancelled
|
|
249
|
-
? textColors.prompt.icon.cancelled("${
|
|
847
|
+
? textColors.prompt.icon.cancelled("${theme.icons.prompt.cancelled}")
|
|
250
848
|
: this.isError
|
|
251
|
-
? textColors.prompt.icon.error("${
|
|
252
|
-
: textColors.prompt.icon.active("${
|
|
849
|
+
? textColors.prompt.icon.error("${theme.icons.prompt.error}")
|
|
850
|
+
: textColors.prompt.icon.active("${theme.icons.prompt.active}")
|
|
253
851
|
} \${
|
|
254
852
|
this.isCompleted
|
|
255
853
|
? textColors.prompt.message.submitted(this.message)
|
|
@@ -272,7 +870,22 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
272
870
|
this.consoleStatus = this.status;
|
|
273
871
|
} finally {
|
|
274
872
|
clearTimeout(timeout);
|
|
275
|
-
}
|
|
873
|
+
} `;
|
|
874
|
+
}
|
|
875
|
+
}),
|
|
876
|
+
createComponent(Spacing, {}),
|
|
877
|
+
createComponent(ClassMethod, {
|
|
878
|
+
doc: "A method to handle key press events and determine the corresponding action",
|
|
879
|
+
name: "keypress",
|
|
880
|
+
"private": true,
|
|
881
|
+
parameters: [{
|
|
882
|
+
name: "char",
|
|
883
|
+
type: "string"
|
|
884
|
+
}, {
|
|
885
|
+
name: "key",
|
|
886
|
+
type: "Key"
|
|
887
|
+
}],
|
|
888
|
+
children: code`if (this.#isClosed) {
|
|
276
889
|
return;
|
|
277
890
|
}
|
|
278
891
|
|
|
@@ -280,7 +893,124 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
280
893
|
this.#isKeyPressed = true;
|
|
281
894
|
}
|
|
282
895
|
|
|
283
|
-
return this.onKeyPress(char, key); `
|
|
896
|
+
return this.onKeyPress(char, key); `
|
|
897
|
+
})
|
|
898
|
+
];
|
|
899
|
+
}
|
|
900
|
+
}),
|
|
901
|
+
createComponent(Spacing, {}),
|
|
902
|
+
createComponent(InterfaceDeclaration, {
|
|
903
|
+
name: "PromptFactoryConfig",
|
|
904
|
+
"extends": "PromptConfig<TValue>",
|
|
905
|
+
doc: "Configuration options for creating a prompt with a prompt factory function",
|
|
906
|
+
typeParameters: [{
|
|
907
|
+
name: "TValue",
|
|
908
|
+
default: "string"
|
|
909
|
+
}],
|
|
910
|
+
get children() {
|
|
911
|
+
return [
|
|
912
|
+
createComponent(InterfaceMember, {
|
|
913
|
+
name: "onState",
|
|
914
|
+
required: false,
|
|
915
|
+
type: "(state: PromptState<TValue>) => any",
|
|
916
|
+
doc: "A function that is called when the prompt state changes, useful for updating the prompt message or other properties dynamically"
|
|
917
|
+
}),
|
|
918
|
+
createComponent(Spacing, {}),
|
|
919
|
+
createComponent(InterfaceMember, {
|
|
920
|
+
name: "onSubmit",
|
|
921
|
+
required: false,
|
|
922
|
+
type: "(value: TValue) => any",
|
|
923
|
+
doc: "A function that is called when the prompt is submitted, useful for handling the submitted value or performing actions based on the prompt state"
|
|
924
|
+
}),
|
|
925
|
+
createComponent(Spacing, {}),
|
|
926
|
+
createComponent(InterfaceMember, {
|
|
927
|
+
name: "onCancel",
|
|
928
|
+
required: false,
|
|
929
|
+
type: "(event: any) => any",
|
|
930
|
+
doc: "A function that is called when the prompt is canceled, useful for handling the canceled value or performing actions based on the prompt state"
|
|
931
|
+
})
|
|
932
|
+
];
|
|
933
|
+
}
|
|
934
|
+
}),
|
|
935
|
+
createComponent(Spacing, {}),
|
|
936
|
+
createComponent(TSDoc, { heading: "A unique symbol used to indicate that a prompt was cancelled, which can be returned from a prompt function to signal that the prompt interaction should be cancelled and any pending promises should be rejected with this symbol. This allows for a consistent way to handle prompt cancellations across different prompt types and interactions." }),
|
|
937
|
+
createComponent(VarDeclaration, {
|
|
938
|
+
"export": true,
|
|
939
|
+
name: "CANCEL_SYMBOL",
|
|
940
|
+
children: code`Symbol("shell-shock:prompts:cancel"); `
|
|
941
|
+
}),
|
|
942
|
+
createComponent(Spacing, {}),
|
|
943
|
+
createComponent(TSDoc, {
|
|
944
|
+
heading: "A utility function to check if a given value is the {@link CANCEL_SYMBOL | cancel symbol}, which can be used to determine if a prompt interaction was cancelled based on the value returned from a prompt factory function. This function checks if the provided value is strictly equal to the {@link CANCEL_SYMBOL | CANCEL_SYMBOL}, allowing for a consistent way to handle prompt cancellations across different prompt types and interactions.",
|
|
945
|
+
get children() {
|
|
946
|
+
return [createComponent(TSDocParam, {
|
|
947
|
+
name: "value",
|
|
948
|
+
children: `The value to check.`
|
|
949
|
+
}), createComponent(TSDocReturns, { children: `A boolean indicating whether the provided value is the {@link CANCEL_SYMBOL | cancel symbol}, which can be used to determine if a prompt interaction was cancelled.` })];
|
|
950
|
+
}
|
|
951
|
+
}),
|
|
952
|
+
createComponent(FunctionDeclaration, {
|
|
953
|
+
name: "isCancel",
|
|
954
|
+
"export": true,
|
|
955
|
+
parameters: [{
|
|
956
|
+
name: "value",
|
|
957
|
+
type: "any"
|
|
958
|
+
}],
|
|
959
|
+
returnType: "value is typeof CANCEL_SYMBOL",
|
|
960
|
+
children: code`return value === CANCEL_SYMBOL; `
|
|
961
|
+
})
|
|
962
|
+
];
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Declarations for a text-based prompt that allows users to input and edit text, with support for cursor movement, deletion, and custom masking. This prompt type can be used for various text input scenarios, such as entering a username, password, or any other string input. The TextPrompt class extends the base Prompt class and implements specific logic for handling text input and editing interactions.
|
|
966
|
+
*/
|
|
967
|
+
function TextPromptDeclarations() {
|
|
968
|
+
return [
|
|
969
|
+
createComponent(InterfaceDeclaration, {
|
|
970
|
+
name: "StringPromptConfig",
|
|
971
|
+
"extends": "PromptConfig<string>",
|
|
972
|
+
doc: "Configuration options for creating a text-based prompt",
|
|
973
|
+
get children() {
|
|
974
|
+
return [
|
|
975
|
+
createComponent(InterfaceMember, {
|
|
976
|
+
name: "initialValue",
|
|
977
|
+
required: false,
|
|
978
|
+
type: "string",
|
|
979
|
+
doc: "The initial value of the prompt"
|
|
980
|
+
}),
|
|
981
|
+
createComponent(Spacing, {}),
|
|
982
|
+
createComponent(InterfaceMember, {
|
|
983
|
+
name: "mask",
|
|
984
|
+
required: false,
|
|
985
|
+
type: "(input: string) => string",
|
|
986
|
+
doc: "A function that masks the input value and returns the masked result"
|
|
987
|
+
})
|
|
988
|
+
];
|
|
989
|
+
}
|
|
990
|
+
}),
|
|
991
|
+
createComponent(Spacing, {}),
|
|
992
|
+
createComponent(ClassDeclaration, {
|
|
993
|
+
name: "StringPrompt",
|
|
994
|
+
doc: "A prompt for text input",
|
|
995
|
+
"extends": "Prompt<string>",
|
|
996
|
+
get children() {
|
|
997
|
+
return [
|
|
998
|
+
createComponent(ClassField, {
|
|
999
|
+
name: "isInvalid",
|
|
1000
|
+
isPrivateMember: true,
|
|
1001
|
+
type: "boolean",
|
|
1002
|
+
children: code`false; `
|
|
1003
|
+
}),
|
|
1004
|
+
createIntrinsic("hbr", {}),
|
|
1005
|
+
createComponent(ClassField, {
|
|
1006
|
+
name: "initialValue",
|
|
1007
|
+
"protected": true,
|
|
1008
|
+
override: true,
|
|
1009
|
+
type: "string",
|
|
1010
|
+
children: code`""; `
|
|
1011
|
+
}),
|
|
1012
|
+
createComponent(Spacing, {}),
|
|
1013
|
+
code`constructor(config: StringPromptConfig) {
|
|
284
1014
|
super(config);
|
|
285
1015
|
|
|
286
1016
|
if (config.initialValue) {
|
|
@@ -294,7 +1024,21 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
294
1024
|
this.cursor = 0;
|
|
295
1025
|
this.sync();
|
|
296
1026
|
this.last();
|
|
297
|
-
} `,
|
|
1027
|
+
} `,
|
|
1028
|
+
createComponent(Spacing, {}),
|
|
1029
|
+
createComponent(ClassMethod, {
|
|
1030
|
+
doc: "A method to handle onKeyPress events and determine the corresponding action",
|
|
1031
|
+
name: "onKeyPress",
|
|
1032
|
+
override: true,
|
|
1033
|
+
"protected": true,
|
|
1034
|
+
parameters: [{
|
|
1035
|
+
name: "char",
|
|
1036
|
+
type: "string"
|
|
1037
|
+
}, {
|
|
1038
|
+
name: "key",
|
|
1039
|
+
type: "Key"
|
|
1040
|
+
}],
|
|
1041
|
+
children: code`const action = this.getAction(key);
|
|
298
1042
|
if (action && typeof (this as any)[action] === "function") {
|
|
299
1043
|
return (this as any)[action]();
|
|
300
1044
|
}
|
|
@@ -308,24 +1052,97 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
308
1052
|
}\${char}\${
|
|
309
1053
|
this.displayValue.slice(this.cursor)
|
|
310
1054
|
}\`);
|
|
311
|
-
this.sync(); `
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
1055
|
+
this.sync(); `
|
|
1056
|
+
}),
|
|
1057
|
+
createComponent(Spacing, {}),
|
|
1058
|
+
createComponent(ClassMethod, {
|
|
1059
|
+
doc: "A method to handle changes in the prompt value",
|
|
1060
|
+
name: "onChange",
|
|
1061
|
+
override: true,
|
|
1062
|
+
"protected": true,
|
|
1063
|
+
parameters: [{
|
|
1064
|
+
name: "previousValue",
|
|
1065
|
+
type: "string"
|
|
1066
|
+
}],
|
|
1067
|
+
children: code`this.#isInvalid = false;
|
|
1068
|
+
this.cursor = this.displayValue.slice(0, this.cursor).length + 1; `
|
|
1069
|
+
}),
|
|
1070
|
+
createComponent(Spacing, {}),
|
|
1071
|
+
createComponent(ClassMethod, {
|
|
1072
|
+
doc: "A method to reset the prompt input",
|
|
1073
|
+
name: "reset",
|
|
1074
|
+
override: true,
|
|
1075
|
+
"protected": true,
|
|
1076
|
+
children: code`this.cursor = Number(!!this.initialValue);
|
|
1077
|
+
super.reset(); `
|
|
1078
|
+
}),
|
|
1079
|
+
createComponent(Spacing, {}),
|
|
1080
|
+
createComponent(ClassMethod, {
|
|
1081
|
+
doc: "A method to validate the prompt input",
|
|
1082
|
+
name: "checkValidations",
|
|
1083
|
+
override: true,
|
|
1084
|
+
async: true,
|
|
1085
|
+
"protected": true,
|
|
1086
|
+
children: code`await super.checkValidations(this.value);
|
|
1087
|
+
this.#isInvalid = this.isError; `
|
|
1088
|
+
}),
|
|
1089
|
+
createComponent(Spacing, {}),
|
|
1090
|
+
createComponent(ClassMethod, {
|
|
1091
|
+
doc: "A method to move the cursor to the end of the input",
|
|
1092
|
+
name: "next",
|
|
1093
|
+
"protected": true,
|
|
1094
|
+
children: code`this.changeValue(this.initialValue);
|
|
315
1095
|
this.cursor = this.displayValue.length;
|
|
316
|
-
this.sync(); `
|
|
317
|
-
|
|
318
|
-
|
|
1096
|
+
this.sync(); `
|
|
1097
|
+
}),
|
|
1098
|
+
createComponent(Spacing, {}),
|
|
1099
|
+
createComponent(ClassMethod, {
|
|
1100
|
+
doc: "A method to move the cursor to the start",
|
|
1101
|
+
name: "first",
|
|
1102
|
+
"protected": true,
|
|
1103
|
+
children: code`this.cursor = 0;
|
|
1104
|
+
this.sync(); `
|
|
1105
|
+
}),
|
|
1106
|
+
createComponent(Spacing, {}),
|
|
1107
|
+
createComponent(ClassMethod, {
|
|
1108
|
+
doc: "A method to move the cursor to the end",
|
|
1109
|
+
name: "last",
|
|
1110
|
+
"protected": true,
|
|
1111
|
+
children: code`this.cursor = this.displayValue.length;
|
|
1112
|
+
this.sync(); `
|
|
1113
|
+
}),
|
|
1114
|
+
createComponent(Spacing, {}),
|
|
1115
|
+
createComponent(ClassMethod, {
|
|
1116
|
+
doc: "A method to move the cursor to the left",
|
|
1117
|
+
name: "left",
|
|
1118
|
+
"protected": true,
|
|
1119
|
+
children: code`if (this.cursor <= 0) {
|
|
319
1120
|
return this.bell();
|
|
320
1121
|
}
|
|
321
1122
|
|
|
322
1123
|
this.moveCursor(-1);
|
|
323
|
-
this.sync(); `
|
|
1124
|
+
this.sync(); `
|
|
1125
|
+
}),
|
|
1126
|
+
createComponent(Spacing, {}),
|
|
1127
|
+
createComponent(ClassMethod, {
|
|
1128
|
+
doc: "A method to move the cursor to the right",
|
|
1129
|
+
name: "right",
|
|
1130
|
+
"protected": true,
|
|
1131
|
+
children: code`if (this.cursor >= this.displayValue.length) {
|
|
324
1132
|
return this.bell();
|
|
325
1133
|
}
|
|
326
1134
|
|
|
327
1135
|
this.moveCursor(1);
|
|
328
|
-
this.sync(); `
|
|
1136
|
+
this.sync(); `
|
|
1137
|
+
}),
|
|
1138
|
+
createComponent(Spacing, {}),
|
|
1139
|
+
createComponent(ClassMethod, {
|
|
1140
|
+
doc: "A method to render the prompt",
|
|
1141
|
+
name: "onRender",
|
|
1142
|
+
override: true,
|
|
1143
|
+
"protected": true,
|
|
1144
|
+
returnType: "string",
|
|
1145
|
+
children: code`return this.isPlaceholder
|
|
329
1146
|
? textColors.prompt.input.disabled(this.displayValue)
|
|
330
1147
|
: this.#isInvalid
|
|
331
1148
|
? textColors.prompt.input.error(this.displayValue)
|
|
@@ -333,7 +1150,25 @@ import{createComponent as e,createIntrinsic as t,mergeProps as n}from"@alloy-js/
|
|
|
333
1150
|
? textColors.prompt.input.submitted(this.displayValue)
|
|
334
1151
|
: this.isCancelled
|
|
335
1152
|
? textColors.prompt.input.cancelled(this.displayValue)
|
|
336
|
-
: bold(textColors.prompt.input.active(this.displayValue)); `
|
|
1153
|
+
: bold(textColors.prompt.input.active(this.displayValue)); `
|
|
1154
|
+
})
|
|
1155
|
+
];
|
|
1156
|
+
}
|
|
1157
|
+
}),
|
|
1158
|
+
createComponent(Spacing, {}),
|
|
1159
|
+
createComponent(TSDoc, { heading: "A type definition for the configuration options to pass to the text prompt, which extends the base PromptConfig with additional options specific to text prompts. This type can be used when creating a text prompt using the {@link text | text prompt factory function} or when manually creating an instance of the TextPrompt class. The TextConfig type includes all the properties of the base PromptConfig, such as message, description, initialValue, validate, parse, format, mask, etc., as well as any additional properties that are specific to text prompts." }),
|
|
1160
|
+
createComponent(TypeDeclaration, {
|
|
1161
|
+
name: "TextConfig",
|
|
1162
|
+
"export": true,
|
|
1163
|
+
children: code`PromptFactoryConfig<string> & StringPromptConfig; `
|
|
1164
|
+
}),
|
|
1165
|
+
createComponent(Spacing, {}),
|
|
1166
|
+
createComponent(TSDoc, {
|
|
1167
|
+
heading: "A function to create and run a text prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
1168
|
+
get children() {
|
|
1169
|
+
return [
|
|
1170
|
+
createComponent(TSDocRemarks, { children: code`This function can be used to easily create and run a text prompt without needing to manually create an instance of the TextPrompt class and handle its events. The function accepts a configuration object that extends the base PromptFactoryConfig with additional options specific to text prompts, such as the initial value and mask function. The returned promise allows for easy handling of the prompt result using async/await syntax or traditional promise chaining.` }),
|
|
1171
|
+
createComponent(TSDocExample, { children: `import { text, isCancel } from "shell-shock:prompts";
|
|
337
1172
|
|
|
338
1173
|
async function run() {
|
|
339
1174
|
const name = await text({
|
|
@@ -349,13 +1184,202 @@ async function run() {
|
|
|
349
1184
|
console.log("Hello, " + name + "!");
|
|
350
1185
|
}
|
|
351
1186
|
|
|
352
|
-
run(); `
|
|
1187
|
+
run(); ` }),
|
|
1188
|
+
createComponent(Spacing, {}),
|
|
1189
|
+
createComponent(TSDocParam, {
|
|
1190
|
+
name: "config",
|
|
1191
|
+
children: `The configuration options to pass to the text prompt, which extends the base PromptConfig with additional options specific to text prompts`
|
|
1192
|
+
}),
|
|
1193
|
+
createComponent(TSDocReturns, { children: `A promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled` })
|
|
1194
|
+
];
|
|
1195
|
+
}
|
|
1196
|
+
}),
|
|
1197
|
+
createComponent(FunctionDeclaration, {
|
|
1198
|
+
name: "text",
|
|
1199
|
+
"export": true,
|
|
1200
|
+
parameters: [{
|
|
1201
|
+
name: "config",
|
|
1202
|
+
type: "TextConfig"
|
|
1203
|
+
}],
|
|
1204
|
+
returnType: "Promise<string | symbol>",
|
|
1205
|
+
children: code`return new Promise<string | symbol>((response, reject) => {
|
|
353
1206
|
const prompt = new StringPrompt(config);
|
|
354
1207
|
|
|
355
1208
|
prompt.on("state", state => config.onState?.(state));
|
|
356
1209
|
prompt.on("submit", value => response(value));
|
|
357
1210
|
prompt.on("cancel", event => response(CANCEL_SYMBOL));
|
|
358
|
-
});`
|
|
1211
|
+
});`
|
|
1212
|
+
})
|
|
1213
|
+
];
|
|
1214
|
+
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Declarations for a select prompt that allows users to choose from a list of options, with support for pagination, option descriptions, and disabled options. This prompt type can be used for scenarios where the user needs to select one option from a predefined list, such as choosing a color, selecting a file, or picking an item from a menu. The SelectPrompt class extends the base Prompt class and implements specific logic for handling option selection and navigation interactions.
|
|
1217
|
+
*/
|
|
1218
|
+
function SelectPromptDeclarations() {
|
|
1219
|
+
return [
|
|
1220
|
+
createComponent(InterfaceDeclaration, {
|
|
1221
|
+
name: "PromptOptionConfig",
|
|
1222
|
+
doc: "Configuration for an option the user can select from the select prompt",
|
|
1223
|
+
typeParameters: [{
|
|
1224
|
+
name: "TValue",
|
|
1225
|
+
default: "string"
|
|
1226
|
+
}],
|
|
1227
|
+
get children() {
|
|
1228
|
+
return [
|
|
1229
|
+
createComponent(InterfaceMember, {
|
|
1230
|
+
name: "label",
|
|
1231
|
+
schema: { type: "string" },
|
|
1232
|
+
required: false,
|
|
1233
|
+
doc: "The message label for the option"
|
|
1234
|
+
}),
|
|
1235
|
+
createComponent(Spacing, {}),
|
|
1236
|
+
createComponent(InterfaceMember, {
|
|
1237
|
+
name: "icon",
|
|
1238
|
+
schema: { type: "string" },
|
|
1239
|
+
required: false,
|
|
1240
|
+
doc: "An icon for the option"
|
|
1241
|
+
}),
|
|
1242
|
+
createComponent(Spacing, {}),
|
|
1243
|
+
createComponent(InterfaceMember, {
|
|
1244
|
+
name: "value",
|
|
1245
|
+
type: "TValue",
|
|
1246
|
+
doc: "The value of the option"
|
|
1247
|
+
}),
|
|
1248
|
+
createComponent(Spacing, {}),
|
|
1249
|
+
createComponent(InterfaceMember, {
|
|
1250
|
+
name: "description",
|
|
1251
|
+
schema: { type: "string" },
|
|
1252
|
+
required: false,
|
|
1253
|
+
doc: "The description of the option"
|
|
1254
|
+
}),
|
|
1255
|
+
createComponent(Spacing, {}),
|
|
1256
|
+
createComponent(InterfaceMember, {
|
|
1257
|
+
name: "selected",
|
|
1258
|
+
schema: { type: "boolean" },
|
|
1259
|
+
required: false,
|
|
1260
|
+
doc: "Whether the option is selected"
|
|
1261
|
+
}),
|
|
1262
|
+
createComponent(Spacing, {}),
|
|
1263
|
+
createComponent(InterfaceMember, {
|
|
1264
|
+
name: "disabled",
|
|
1265
|
+
schema: { type: "boolean" },
|
|
1266
|
+
required: false,
|
|
1267
|
+
doc: "Whether the option is disabled"
|
|
1268
|
+
})
|
|
1269
|
+
];
|
|
1270
|
+
}
|
|
1271
|
+
}),
|
|
1272
|
+
createComponent(Spacing, {}),
|
|
1273
|
+
createComponent(InterfaceDeclaration, {
|
|
1274
|
+
"export": true,
|
|
1275
|
+
name: "PromptOption",
|
|
1276
|
+
"extends": "PromptOptionConfig<TValue>",
|
|
1277
|
+
doc: "An option the user can select from the select prompt",
|
|
1278
|
+
typeParameters: [{
|
|
1279
|
+
name: "TValue",
|
|
1280
|
+
default: "string"
|
|
1281
|
+
}],
|
|
1282
|
+
get children() {
|
|
1283
|
+
return [
|
|
1284
|
+
createComponent(InterfaceMember, {
|
|
1285
|
+
name: "label",
|
|
1286
|
+
type: "string",
|
|
1287
|
+
doc: "The message label for the option"
|
|
1288
|
+
}),
|
|
1289
|
+
createComponent(InterfaceMember, {
|
|
1290
|
+
name: "index",
|
|
1291
|
+
type: "number",
|
|
1292
|
+
doc: "The index of the option"
|
|
1293
|
+
}),
|
|
1294
|
+
createComponent(Spacing, {}),
|
|
1295
|
+
createComponent(InterfaceMember, {
|
|
1296
|
+
name: "selected",
|
|
1297
|
+
type: "boolean",
|
|
1298
|
+
doc: "Whether the option is selected"
|
|
1299
|
+
}),
|
|
1300
|
+
createComponent(Spacing, {}),
|
|
1301
|
+
createComponent(InterfaceMember, {
|
|
1302
|
+
name: "disabled",
|
|
1303
|
+
type: "boolean",
|
|
1304
|
+
doc: "Whether the option is disabled"
|
|
1305
|
+
})
|
|
1306
|
+
];
|
|
1307
|
+
}
|
|
1308
|
+
}),
|
|
1309
|
+
createComponent(Spacing, {}),
|
|
1310
|
+
createComponent(InterfaceDeclaration, {
|
|
1311
|
+
name: "SelectPromptConfig",
|
|
1312
|
+
"extends": "PromptConfig<TValue>",
|
|
1313
|
+
doc: "An options object for configuring a select prompt",
|
|
1314
|
+
typeParameters: [{
|
|
1315
|
+
name: "TValue",
|
|
1316
|
+
default: "string"
|
|
1317
|
+
}],
|
|
1318
|
+
get children() {
|
|
1319
|
+
return [
|
|
1320
|
+
createComponent(InterfaceMember, {
|
|
1321
|
+
name: "hint",
|
|
1322
|
+
required: false,
|
|
1323
|
+
type: "string",
|
|
1324
|
+
doc: "A hint to display to the user"
|
|
1325
|
+
}),
|
|
1326
|
+
createComponent(Spacing, {}),
|
|
1327
|
+
createComponent(InterfaceMember, {
|
|
1328
|
+
name: "options",
|
|
1329
|
+
type: "Array<string | PromptOptionConfig<TValue>>",
|
|
1330
|
+
doc: "The options available for the select prompt"
|
|
1331
|
+
}),
|
|
1332
|
+
createComponent(Spacing, {}),
|
|
1333
|
+
createComponent(InterfaceMember, {
|
|
1334
|
+
name: "optionsPerPage",
|
|
1335
|
+
required: false,
|
|
1336
|
+
type: "number",
|
|
1337
|
+
doc: "The number of options to display per page, defaults to 8"
|
|
1338
|
+
})
|
|
1339
|
+
];
|
|
1340
|
+
}
|
|
1341
|
+
}),
|
|
1342
|
+
createComponent(Spacing, {}),
|
|
1343
|
+
createComponent(ClassDeclaration, {
|
|
1344
|
+
name: "SelectPrompt",
|
|
1345
|
+
doc: "A prompt for selecting an option from a list",
|
|
1346
|
+
"extends": "Prompt<TValue>",
|
|
1347
|
+
typeParameters: [{
|
|
1348
|
+
name: "TValue",
|
|
1349
|
+
default: "string"
|
|
1350
|
+
}],
|
|
1351
|
+
get children() {
|
|
1352
|
+
return [
|
|
1353
|
+
createComponent(ClassField, {
|
|
1354
|
+
name: "initialValue",
|
|
1355
|
+
"protected": true,
|
|
1356
|
+
override: true,
|
|
1357
|
+
type: "TValue"
|
|
1358
|
+
}),
|
|
1359
|
+
createIntrinsic("hbr", {}),
|
|
1360
|
+
createComponent(ClassField, {
|
|
1361
|
+
name: "optionsPerPage",
|
|
1362
|
+
"protected": true,
|
|
1363
|
+
type: "number",
|
|
1364
|
+
children: code`8; `
|
|
1365
|
+
}),
|
|
1366
|
+
createIntrinsic("hbr", {}),
|
|
1367
|
+
createComponent(ClassField, {
|
|
1368
|
+
name: "options",
|
|
1369
|
+
"protected": true,
|
|
1370
|
+
type: "PromptOption<TValue>[]",
|
|
1371
|
+
children: code`[]; `
|
|
1372
|
+
}),
|
|
1373
|
+
createIntrinsic("hbr", {}),
|
|
1374
|
+
createComponent(ClassField, {
|
|
1375
|
+
name: "cursorHidden",
|
|
1376
|
+
"protected": true,
|
|
1377
|
+
override: true,
|
|
1378
|
+
type: "boolean",
|
|
1379
|
+
children: code`true; `
|
|
1380
|
+
}),
|
|
1381
|
+
createComponent(Spacing, {}),
|
|
1382
|
+
code`constructor(config: SelectPromptConfig<TValue>) {
|
|
359
1383
|
super(config);
|
|
360
1384
|
|
|
361
1385
|
if (config.initialValue) {
|
|
@@ -400,37 +1424,136 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
400
1424
|
}
|
|
401
1425
|
|
|
402
1426
|
this.sync();
|
|
403
|
-
} `,
|
|
1427
|
+
} `,
|
|
1428
|
+
createComponent(Spacing, {}),
|
|
1429
|
+
createComponent(ClassPropertyGet, {
|
|
1430
|
+
doc: "Returns the currently selected option",
|
|
1431
|
+
name: "selectedOption",
|
|
1432
|
+
type: "PromptOption<TValue> | null",
|
|
1433
|
+
"protected": true,
|
|
1434
|
+
children: code`return this.options.find(option => option.value === this.value) ?? null; `
|
|
1435
|
+
}),
|
|
1436
|
+
createComponent(Spacing, {}),
|
|
1437
|
+
createComponent(ClassMethod, {
|
|
1438
|
+
doc: "A method to route key press events to specific prompt actions based on the key pressed. This method maps various key combinations and keys to corresponding actions that can be handled by the prompt, such as submitting, cancelling, navigating, etc.",
|
|
1439
|
+
name: "getAction",
|
|
1440
|
+
override: true,
|
|
1441
|
+
"protected": true,
|
|
1442
|
+
parameters: [{
|
|
1443
|
+
name: "key",
|
|
1444
|
+
type: "Key"
|
|
1445
|
+
}],
|
|
1446
|
+
returnType: "string | false",
|
|
1447
|
+
children: code`let action = super.getAction(key);
|
|
404
1448
|
if (!action) {
|
|
405
1449
|
if (key.name === "j") action = "down";
|
|
406
1450
|
if (key.name === "k") action = "up";
|
|
407
1451
|
}
|
|
408
1452
|
|
|
409
|
-
return action || false; `
|
|
410
|
-
|
|
1453
|
+
return action || false; `
|
|
1454
|
+
}),
|
|
1455
|
+
createComponent(Spacing, {}),
|
|
1456
|
+
createComponent(ClassMethod, {
|
|
1457
|
+
doc: "A method to reset the prompt input",
|
|
1458
|
+
name: "reset",
|
|
1459
|
+
override: true,
|
|
1460
|
+
"protected": true,
|
|
1461
|
+
children: code`this.moveCursor(0);
|
|
1462
|
+
super.reset(); `
|
|
1463
|
+
}),
|
|
1464
|
+
createComponent(Spacing, {}),
|
|
1465
|
+
createComponent(ClassMethod, {
|
|
1466
|
+
doc: "A method to submit the prompt input",
|
|
1467
|
+
name: "submit",
|
|
1468
|
+
async: true,
|
|
1469
|
+
override: true,
|
|
1470
|
+
"protected": true,
|
|
1471
|
+
children: code`if (!this.selectedOption?.disabled) {
|
|
411
1472
|
await super.submit();
|
|
412
1473
|
} else {
|
|
413
1474
|
this.bell();
|
|
414
|
-
} `
|
|
415
|
-
|
|
1475
|
+
} `
|
|
1476
|
+
}),
|
|
1477
|
+
createComponent(Spacing, {}),
|
|
1478
|
+
createComponent(ClassMethod, {
|
|
1479
|
+
doc: "A method to move the cursor to the end of the input",
|
|
1480
|
+
name: "next",
|
|
1481
|
+
"protected": true,
|
|
1482
|
+
children: code`this.moveCursor((this.cursor + 1) % this.options.length);
|
|
1483
|
+
this.sync(); `
|
|
1484
|
+
}),
|
|
1485
|
+
createComponent(Spacing, {}),
|
|
1486
|
+
createComponent(ClassMethod, {
|
|
1487
|
+
doc: "A method to move the cursor to the left or right by a \\`count\\` of positions",
|
|
1488
|
+
name: "moveCursor",
|
|
1489
|
+
parameters: [{
|
|
1490
|
+
name: "count",
|
|
1491
|
+
type: "number"
|
|
1492
|
+
}],
|
|
1493
|
+
override: true,
|
|
1494
|
+
"protected": true,
|
|
1495
|
+
children: code`this.cursor = count;
|
|
416
1496
|
|
|
417
1497
|
this.changeValue(this.options[count]!.value);
|
|
418
|
-
this.sync(); `
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
1498
|
+
this.sync(); `
|
|
1499
|
+
}),
|
|
1500
|
+
createComponent(Spacing, {}),
|
|
1501
|
+
createComponent(ClassMethod, {
|
|
1502
|
+
doc: "A method to move the cursor to the first option",
|
|
1503
|
+
name: "first",
|
|
1504
|
+
"protected": true,
|
|
1505
|
+
children: code`this.moveCursor(0);
|
|
1506
|
+
this.sync(); `
|
|
1507
|
+
}),
|
|
1508
|
+
createComponent(Spacing, {}),
|
|
1509
|
+
createComponent(ClassMethod, {
|
|
1510
|
+
doc: "A method to move the cursor to the last option",
|
|
1511
|
+
name: "last",
|
|
1512
|
+
"protected": true,
|
|
1513
|
+
children: code`this.moveCursor(this.options.length - 1);
|
|
1514
|
+
this.sync(); `
|
|
1515
|
+
}),
|
|
1516
|
+
createComponent(Spacing, {}),
|
|
1517
|
+
createComponent(ClassMethod, {
|
|
1518
|
+
doc: "A method to move the cursor to the start",
|
|
1519
|
+
name: "first",
|
|
1520
|
+
"protected": true,
|
|
1521
|
+
children: code`this.cursor = 0;
|
|
1522
|
+
this.sync(); `
|
|
1523
|
+
}),
|
|
1524
|
+
createComponent(Spacing, {}),
|
|
1525
|
+
createComponent(ClassMethod, {
|
|
1526
|
+
doc: "A method to move the cursor to the up",
|
|
1527
|
+
name: "up",
|
|
1528
|
+
"protected": true,
|
|
1529
|
+
children: code`if (this.cursor === 0) {
|
|
422
1530
|
this.moveCursor(this.options.length - 1);
|
|
423
1531
|
} else {
|
|
424
1532
|
this.moveCursor(this.cursor - 1);
|
|
425
1533
|
}
|
|
426
1534
|
|
|
427
|
-
this.sync(); `
|
|
1535
|
+
this.sync(); `
|
|
1536
|
+
}),
|
|
1537
|
+
createComponent(Spacing, {}),
|
|
1538
|
+
createComponent(ClassMethod, {
|
|
1539
|
+
doc: "A method to move the cursor to the down",
|
|
1540
|
+
name: "down",
|
|
1541
|
+
"protected": true,
|
|
1542
|
+
children: code`if (this.cursor === this.options.length - 1) {
|
|
428
1543
|
this.moveCursor(0);
|
|
429
1544
|
} else {
|
|
430
1545
|
this.moveCursor(this.cursor + 1);
|
|
431
1546
|
}
|
|
432
1547
|
|
|
433
|
-
this.sync(); `
|
|
1548
|
+
this.sync(); `
|
|
1549
|
+
}),
|
|
1550
|
+
createComponent(Spacing, {}),
|
|
1551
|
+
createComponent(ClassMethod, {
|
|
1552
|
+
doc: "A method to render the prompt",
|
|
1553
|
+
name: "onRender",
|
|
1554
|
+
override: true,
|
|
1555
|
+
"protected": true,
|
|
1556
|
+
children: code`const spacing = Math.max(...this.options.map(option => option.label?.length || 0)) + 2;
|
|
434
1557
|
|
|
435
1558
|
const startIndex = Math.max(Math.min(this.options.length - this.optionsPerPage, this.cursor - Math.floor(this.optionsPerPage / 2)), 0);
|
|
436
1559
|
const endIndex = Math.min(startIndex + this.optionsPerPage, this.options.length);
|
|
@@ -482,7 +1605,29 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
482
1605
|
output += super.onRender();
|
|
483
1606
|
}
|
|
484
1607
|
|
|
485
|
-
return output; `
|
|
1608
|
+
return output; `
|
|
1609
|
+
})
|
|
1610
|
+
];
|
|
1611
|
+
}
|
|
1612
|
+
}),
|
|
1613
|
+
createComponent(Spacing, {}),
|
|
1614
|
+
createComponent(TSDoc, {
|
|
1615
|
+
heading: "A type definition for the configuration options to pass to the select prompt, which extends the base PromptConfig with additional options specific to select prompts. This type can be used when creating a select prompt using the {@link select | select prompt factory function}.",
|
|
1616
|
+
get children() {
|
|
1617
|
+
return createComponent(TSDocRemarks, { children: `The Select Config type includes all the properties of the base PromptConfig, such as message, description, initialValue, validate, parse, format, etc., as well as any additional properties that are specific to select prompts, such as the list of options and pagination settings.` });
|
|
1618
|
+
}
|
|
1619
|
+
}),
|
|
1620
|
+
createComponent(TypeDeclaration, {
|
|
1621
|
+
"export": true,
|
|
1622
|
+
name: "SelectConfig",
|
|
1623
|
+
children: code`PromptFactoryConfig<string> & SelectPromptConfig; `
|
|
1624
|
+
}),
|
|
1625
|
+
createComponent(Spacing, {}),
|
|
1626
|
+
createComponent(TSDoc, {
|
|
1627
|
+
heading: "A function to create and run a select prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
1628
|
+
get children() {
|
|
1629
|
+
return [
|
|
1630
|
+
createComponent(TSDocExample, { children: `import { select, isCancel } from "shell-shock:prompts";
|
|
486
1631
|
|
|
487
1632
|
async function run() {
|
|
488
1633
|
const color = await select({
|
|
@@ -504,13 +1649,558 @@ async function run() {
|
|
|
504
1649
|
console.log("Your favorite color is " + color + "!");
|
|
505
1650
|
}
|
|
506
1651
|
|
|
507
|
-
run(); `
|
|
1652
|
+
run(); ` }),
|
|
1653
|
+
createComponent(Spacing, {}),
|
|
1654
|
+
createComponent(TSDocParam, {
|
|
1655
|
+
name: "config",
|
|
1656
|
+
children: `The configuration options to pass to the select prompt, which extends the base PromptConfig with additional options specific to select prompts`
|
|
1657
|
+
}),
|
|
1658
|
+
createComponent(TSDocReturns, { children: `A promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled` })
|
|
1659
|
+
];
|
|
1660
|
+
}
|
|
1661
|
+
}),
|
|
1662
|
+
createComponent(FunctionDeclaration, {
|
|
1663
|
+
name: "select",
|
|
1664
|
+
"export": true,
|
|
1665
|
+
parameters: [{
|
|
1666
|
+
name: "config",
|
|
1667
|
+
type: "SelectConfig"
|
|
1668
|
+
}],
|
|
1669
|
+
returnType: "Promise<string | symbol>",
|
|
1670
|
+
children: code`return new Promise<string | symbol>((response, reject) => {
|
|
508
1671
|
const prompt = new SelectPrompt(config);
|
|
509
1672
|
|
|
510
1673
|
prompt.on("state", state => config.onState?.(state));
|
|
511
1674
|
prompt.on("submit", value => response(value));
|
|
512
1675
|
prompt.on("cancel", event => response(CANCEL_SYMBOL));
|
|
513
|
-
});`
|
|
1676
|
+
});`
|
|
1677
|
+
})
|
|
1678
|
+
];
|
|
1679
|
+
}
|
|
1680
|
+
/**
|
|
1681
|
+
* Declarations for a multi-select prompt that allows users to choose multiple options from a list, with support for disabled options, bulk selection shortcuts, and required validation. This prompt type is useful for scenarios where users need to select one or more values from a predefined set.
|
|
1682
|
+
*/
|
|
1683
|
+
function MultiSelectPromptDeclarations() {
|
|
1684
|
+
return [
|
|
1685
|
+
createComponent(InterfaceDeclaration, {
|
|
1686
|
+
name: "MultiSelectPromptConfig",
|
|
1687
|
+
"extends": "PromptConfig<TValue[]>",
|
|
1688
|
+
doc: "An options object for configuring a multi-select prompt",
|
|
1689
|
+
typeParameters: [{
|
|
1690
|
+
name: "TValue",
|
|
1691
|
+
default: "string"
|
|
1692
|
+
}],
|
|
1693
|
+
get children() {
|
|
1694
|
+
return [
|
|
1695
|
+
createComponent(InterfaceMember, {
|
|
1696
|
+
name: "options",
|
|
1697
|
+
type: "Array<string | PromptOptionConfig<TValue>>",
|
|
1698
|
+
doc: "The options available for the multi-select prompt"
|
|
1699
|
+
}),
|
|
1700
|
+
createComponent(Spacing, {}),
|
|
1701
|
+
createComponent(InterfaceMember, {
|
|
1702
|
+
name: "required",
|
|
1703
|
+
required: false,
|
|
1704
|
+
type: "boolean",
|
|
1705
|
+
doc: "Whether at least one option must be selected, defaults to true"
|
|
1706
|
+
}),
|
|
1707
|
+
createComponent(Spacing, {}),
|
|
1708
|
+
createComponent(InterfaceMember, {
|
|
1709
|
+
name: "initialValue",
|
|
1710
|
+
required: false,
|
|
1711
|
+
type: "TValue[]",
|
|
1712
|
+
doc: "The initial selected values"
|
|
1713
|
+
}),
|
|
1714
|
+
createComponent(Spacing, {}),
|
|
1715
|
+
createComponent(InterfaceMember, {
|
|
1716
|
+
name: "cursorAt",
|
|
1717
|
+
required: false,
|
|
1718
|
+
type: "TValue",
|
|
1719
|
+
doc: "A value indicating which option should be focused initially"
|
|
1720
|
+
}),
|
|
1721
|
+
createComponent(Spacing, {}),
|
|
1722
|
+
createComponent(InterfaceMember, {
|
|
1723
|
+
name: "optionsPerPage",
|
|
1724
|
+
required: false,
|
|
1725
|
+
type: "number",
|
|
1726
|
+
doc: "The number of options to display per page, defaults to 8"
|
|
1727
|
+
})
|
|
1728
|
+
];
|
|
1729
|
+
}
|
|
1730
|
+
}),
|
|
1731
|
+
createComponent(Spacing, {}),
|
|
1732
|
+
createComponent(ClassDeclaration, {
|
|
1733
|
+
name: "MultiSelectPrompt",
|
|
1734
|
+
doc: "A prompt for selecting multiple options from a list",
|
|
1735
|
+
"extends": "Prompt<TValue[]>",
|
|
1736
|
+
typeParameters: [{
|
|
1737
|
+
name: "TValue",
|
|
1738
|
+
default: "string"
|
|
1739
|
+
}],
|
|
1740
|
+
get children() {
|
|
1741
|
+
return [
|
|
1742
|
+
createComponent(ClassField, {
|
|
1743
|
+
name: "initialValue",
|
|
1744
|
+
"protected": true,
|
|
1745
|
+
override: true,
|
|
1746
|
+
type: "TValue[]",
|
|
1747
|
+
children: code`[]; `
|
|
1748
|
+
}),
|
|
1749
|
+
createIntrinsic("hbr", {}),
|
|
1750
|
+
createComponent(ClassField, {
|
|
1751
|
+
name: "required",
|
|
1752
|
+
"protected": true,
|
|
1753
|
+
type: "boolean",
|
|
1754
|
+
children: code`true; `
|
|
1755
|
+
}),
|
|
1756
|
+
createIntrinsic("hbr", {}),
|
|
1757
|
+
createComponent(ClassField, {
|
|
1758
|
+
name: "optionsPerPage",
|
|
1759
|
+
"protected": true,
|
|
1760
|
+
type: "number",
|
|
1761
|
+
children: code`8; `
|
|
1762
|
+
}),
|
|
1763
|
+
createIntrinsic("hbr", {}),
|
|
1764
|
+
createComponent(ClassField, {
|
|
1765
|
+
name: "options",
|
|
1766
|
+
"protected": true,
|
|
1767
|
+
type: "PromptOption<TValue>[]",
|
|
1768
|
+
children: code`[]; `
|
|
1769
|
+
}),
|
|
1770
|
+
createIntrinsic("hbr", {}),
|
|
1771
|
+
createComponent(ClassField, {
|
|
1772
|
+
name: "cursorHidden",
|
|
1773
|
+
"protected": true,
|
|
1774
|
+
override: true,
|
|
1775
|
+
type: "boolean",
|
|
1776
|
+
children: code`true; `
|
|
1777
|
+
}),
|
|
1778
|
+
createComponent(Spacing, {}),
|
|
1779
|
+
code`constructor(config: MultiSelectPromptConfig<TValue>) {
|
|
1780
|
+
super(config);
|
|
1781
|
+
|
|
1782
|
+
this.required = config.required ?? true;
|
|
1783
|
+
this.initialValue = Array.isArray(config.initialValue) ? [...config.initialValue] : [];
|
|
1784
|
+
|
|
1785
|
+
if (config.optionsPerPage) {
|
|
1786
|
+
this.optionsPerPage = config.optionsPerPage;
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
this.options = config.options.map((opt, index) => {
|
|
1790
|
+
let option = {} as Partial<PromptOption<TValue>>;
|
|
1791
|
+
if (typeof opt === "string") {
|
|
1792
|
+
option = {
|
|
1793
|
+
label: opt,
|
|
1794
|
+
value: opt as TValue,
|
|
1795
|
+
selected: this.initialValue.includes(opt as TValue),
|
|
1796
|
+
disabled: false
|
|
1797
|
+
};
|
|
1798
|
+
} else if (typeof opt === "object" && opt !== null) {
|
|
1799
|
+
option = {
|
|
1800
|
+
...opt,
|
|
1801
|
+
selected: this.initialValue.includes(opt.value as TValue) || !!opt.selected
|
|
1802
|
+
};
|
|
1803
|
+
} else {
|
|
1804
|
+
throw new Error("Invalid option provided to MultiSelectPrompt at index #" + index);
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
return {
|
|
1808
|
+
label: option.label || String(option.value),
|
|
1809
|
+
...option,
|
|
1810
|
+
description: option.description,
|
|
1811
|
+
selected: !!option.selected,
|
|
1812
|
+
disabled: !!option.disabled,
|
|
1813
|
+
index
|
|
1814
|
+
} as PromptOption<TValue>;
|
|
1815
|
+
});
|
|
1816
|
+
|
|
1817
|
+
if (config.cursorAt !== undefined) {
|
|
1818
|
+
const index = this.options.findIndex(option => option.value === config.cursorAt);
|
|
1819
|
+
if (index > -1) {
|
|
1820
|
+
this.cursor = index;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
if (this.options[this.cursor]?.disabled) {
|
|
1825
|
+
this.moveCursor(this.findNextEnabled(this.cursor, 1));
|
|
1826
|
+
} else {
|
|
1827
|
+
this.changeValue(this.getSelectedValues());
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
this.sync();
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
protected getAction(key: Key): string | false {
|
|
1834
|
+
let action = super.getAction(key);
|
|
1835
|
+
if (!action) {
|
|
1836
|
+
if (key.name === "j") action = "down";
|
|
1837
|
+
if (key.name === "k") action = "up";
|
|
1838
|
+
if (key.name === "a") action = "toggleAll";
|
|
1839
|
+
if (key.name === "i") action = "toggleInvert";
|
|
1840
|
+
if (key.name === "space") action = "toggleCurrent";
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
return action || false;
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
protected override async submit() {
|
|
1847
|
+
const selectedValues = this.getSelectedValues();
|
|
1848
|
+
if (this.required && selectedValues.length === 0) {
|
|
1849
|
+
this.error(
|
|
1850
|
+
"Please select at least one option. Press <space> to toggle and <enter> to submit."
|
|
1851
|
+
);
|
|
1852
|
+
this.bell();
|
|
1853
|
+
return;
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
this.changeValue(selectedValues);
|
|
1857
|
+
await super.submit();
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
protected toggleCurrent() {
|
|
1861
|
+
const option = this.options[this.cursor];
|
|
1862
|
+
if (!option || option.disabled) {
|
|
1863
|
+
this.bell();
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
option.selected = !option.selected;
|
|
1868
|
+
this.changeValue(this.getSelectedValues());
|
|
1869
|
+
this.sync();
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1872
|
+
protected toggleAll() {
|
|
1873
|
+
const enabled = this.options.filter(option => !option.disabled);
|
|
1874
|
+
const areAllSelected = enabled.every(option => option.selected);
|
|
1875
|
+
for (const option of enabled) {
|
|
1876
|
+
option.selected = !areAllSelected;
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
this.changeValue(this.getSelectedValues());
|
|
1880
|
+
this.sync();
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
protected toggleInvert() {
|
|
1884
|
+
for (const option of this.options) {
|
|
1885
|
+
if (!option.disabled) {
|
|
1886
|
+
option.selected = !option.selected;
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
this.changeValue(this.getSelectedValues());
|
|
1891
|
+
this.sync();
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
protected findNextEnabled(from: number, direction: 1 | -1): number {
|
|
1895
|
+
if (this.options.length === 0) {
|
|
1896
|
+
return 0;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
let index = from;
|
|
1900
|
+
for (let i = 0; i < this.options.length; i++) {
|
|
1901
|
+
index = (index + direction + this.options.length) % this.options.length;
|
|
1902
|
+
if (!this.options[index]?.disabled) {
|
|
1903
|
+
return index;
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
return from;
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
protected getSelectedValues(): TValue[] {
|
|
1911
|
+
return this.options
|
|
1912
|
+
.filter(option => option.selected)
|
|
1913
|
+
.map(option => option.value as TValue);
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
protected next() {
|
|
1917
|
+
this.moveCursor(this.findNextEnabled(this.cursor, 1));
|
|
1918
|
+
this.sync();
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
protected up() {
|
|
1922
|
+
this.moveCursor(this.findNextEnabled(this.cursor, -1));
|
|
1923
|
+
this.sync();
|
|
1924
|
+
}
|
|
1925
|
+
|
|
1926
|
+
protected down() {
|
|
1927
|
+
this.moveCursor(this.findNextEnabled(this.cursor, 1));
|
|
1928
|
+
this.sync();
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
protected first() {
|
|
1932
|
+
this.moveCursor(this.findNextEnabled(0, 1));
|
|
1933
|
+
this.sync();
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
protected last() {
|
|
1937
|
+
this.moveCursor(this.findNextEnabled(this.options.length - 1, -1));
|
|
1938
|
+
this.sync();
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
protected override moveCursor(count: number) {
|
|
1942
|
+
this.cursor = count;
|
|
1943
|
+
this.changeValue(this.getSelectedValues());
|
|
1944
|
+
this.sync();
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
protected override onRender() {
|
|
1948
|
+
const spacing = Math.max(...this.options.map(option => option.label?.length || 0), 0) + 2;
|
|
1949
|
+
const startIndex = Math.max(
|
|
1950
|
+
Math.min(
|
|
1951
|
+
this.options.length - this.optionsPerPage,
|
|
1952
|
+
this.cursor - Math.floor(this.optionsPerPage / 2)
|
|
1953
|
+
),
|
|
1954
|
+
0
|
|
1955
|
+
);
|
|
1956
|
+
const endIndex = Math.min(startIndex + this.optionsPerPage, this.options.length);
|
|
1957
|
+
|
|
1958
|
+
let output = "";
|
|
1959
|
+
if (!this.isCompleted) {
|
|
1960
|
+
output += " \\\n";
|
|
1961
|
+
|
|
1962
|
+
for (let index = startIndex; index < endIndex; index++) {
|
|
1963
|
+
const option = this.options[index]!;
|
|
1964
|
+
const active = this.cursor === index;
|
|
1965
|
+
const selected = !!option.selected;
|
|
1966
|
+
const disabled = !!option.disabled;
|
|
1967
|
+
|
|
1968
|
+
const pointer = disabled
|
|
1969
|
+
? active
|
|
1970
|
+
? bold(textColors.prompt.input.disabled(">"))
|
|
1971
|
+
: index === startIndex
|
|
1972
|
+
? borderColors.app.divider.tertiary("↑")
|
|
1973
|
+
: index === endIndex - 1
|
|
1974
|
+
? borderColors.app.divider.tertiary("↓")
|
|
1975
|
+
: " "
|
|
1976
|
+
: active
|
|
1977
|
+
? bold(textColors.prompt.input.active(">"))
|
|
1978
|
+
: index === startIndex
|
|
1979
|
+
? borderColors.app.divider.tertiary("↑")
|
|
1980
|
+
: index === endIndex - 1
|
|
1981
|
+
? borderColors.app.divider.tertiary("↓")
|
|
1982
|
+
: " ";
|
|
1983
|
+
|
|
1984
|
+
const marker = isUnicodeSupported ? (selected ? "🗹" : "☐") : (selected ? "[x]" : "[ ]");
|
|
1985
|
+
const markerText = disabled
|
|
1986
|
+
? active
|
|
1987
|
+
? bold(textColors.prompt.input.disabled(marker))
|
|
1988
|
+
: textColors.prompt.input.disabled(marker)
|
|
1989
|
+
: active
|
|
1990
|
+
? bold(textColors.prompt.input.active(marker))
|
|
1991
|
+
: textColors.prompt.input.inactive(marker);
|
|
1992
|
+
|
|
1993
|
+
const labelText = disabled
|
|
1994
|
+
? active
|
|
1995
|
+
? bold(underline(textColors.prompt.input.disabled(option.label)))
|
|
1996
|
+
: strikethrough(textColors.prompt.input.disabled(option.label))
|
|
1997
|
+
: active
|
|
1998
|
+
? bold(underline(textColors.prompt.input.active(option.label)))
|
|
1999
|
+
: selected
|
|
2000
|
+
? textColors.prompt.input.active(option.label)
|
|
2001
|
+
: textColors.prompt.input.inactive(option.label);
|
|
2002
|
+
|
|
2003
|
+
output +=
|
|
2004
|
+
pointer +
|
|
2005
|
+
" " +
|
|
2006
|
+
markerText +
|
|
2007
|
+
" " +
|
|
2008
|
+
labelText +
|
|
2009
|
+
" " +
|
|
2010
|
+
" ".repeat(Math.max(0, spacing - option.label.length)) +
|
|
2011
|
+
(option.description && active
|
|
2012
|
+
? italic(textColors.prompt.description.active(option.description))
|
|
2013
|
+
: "") +
|
|
2014
|
+
" \\\n";
|
|
2015
|
+
}
|
|
2016
|
+
} else {
|
|
2017
|
+
const selected = this.options.filter(option => option.selected);
|
|
2018
|
+
this.displayValue =
|
|
2019
|
+
selected.length > 0
|
|
2020
|
+
? selected.map(option => option.label).join(", ")
|
|
2021
|
+
: "(none)";
|
|
2022
|
+
output += super.onRender();
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
return output;
|
|
2026
|
+
}`
|
|
2027
|
+
];
|
|
2028
|
+
}
|
|
2029
|
+
}),
|
|
2030
|
+
createComponent(Spacing, {}),
|
|
2031
|
+
createComponent(TSDoc, { heading: "A type definition for the configuration options to pass to the multi-select prompt, which extends the base PromptConfig with additional options specific to multi-select prompts." }),
|
|
2032
|
+
createComponent(TypeDeclaration, {
|
|
2033
|
+
"export": true,
|
|
2034
|
+
name: "MultiSelectConfig",
|
|
2035
|
+
children: code`PromptFactoryConfig<string[]> & MultiSelectPromptConfig; `
|
|
2036
|
+
}),
|
|
2037
|
+
createComponent(Spacing, {}),
|
|
2038
|
+
createComponent(TSDoc, {
|
|
2039
|
+
heading: "A function to create and run a multi-select prompt, which returns a promise that resolves with the submitted values or a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
2040
|
+
get children() {
|
|
2041
|
+
return [
|
|
2042
|
+
createComponent(TSDocExample, { children: `import { multiselect, isCancel } from "shell-shock:prompts";
|
|
2043
|
+
|
|
2044
|
+
async function run() {
|
|
2045
|
+
const tools = await multiselect({
|
|
2046
|
+
message: "Select tools",
|
|
2047
|
+
options: [
|
|
2048
|
+
{ value: "eslint", label: "ESLint" },
|
|
2049
|
+
{ value: "prettier", label: "Prettier" },
|
|
2050
|
+
{ value: "vitest", label: "Vitest", disabled: true }
|
|
2051
|
+
]
|
|
2052
|
+
});
|
|
2053
|
+
|
|
2054
|
+
if (isCancel(tools)) {
|
|
2055
|
+
console.log("Prompt was cancelled");
|
|
2056
|
+
return;
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
console.log("Selected tools:", tools.join(", "));
|
|
2060
|
+
}
|
|
2061
|
+
|
|
2062
|
+
run(); ` }),
|
|
2063
|
+
createComponent(Spacing, {}),
|
|
2064
|
+
createComponent(TSDocParam, {
|
|
2065
|
+
name: "config",
|
|
2066
|
+
children: `The configuration options to pass to the multi-select prompt`
|
|
2067
|
+
}),
|
|
2068
|
+
createComponent(TSDocReturns, { children: `A promise that resolves with the selected values or a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled` })
|
|
2069
|
+
];
|
|
2070
|
+
}
|
|
2071
|
+
}),
|
|
2072
|
+
createComponent(FunctionDeclaration, {
|
|
2073
|
+
name: "multiselect",
|
|
2074
|
+
"export": true,
|
|
2075
|
+
parameters: [{
|
|
2076
|
+
name: "config",
|
|
2077
|
+
type: "MultiSelectConfig"
|
|
2078
|
+
}],
|
|
2079
|
+
returnType: "Promise<string[] | symbol>",
|
|
2080
|
+
children: code`return new Promise<string[] | symbol>((response, reject) => {
|
|
2081
|
+
const prompt = new MultiSelectPrompt(config);
|
|
2082
|
+
|
|
2083
|
+
prompt.on("state", state => config.onState?.(state));
|
|
2084
|
+
prompt.on("submit", value => response(value));
|
|
2085
|
+
prompt.on("cancel", event => response(CANCEL_SYMBOL));
|
|
2086
|
+
});`
|
|
2087
|
+
})
|
|
2088
|
+
];
|
|
2089
|
+
}
|
|
2090
|
+
/**
|
|
2091
|
+
* A component that renders the declarations for the built-in numeric prompt, which allows users to input and select numeric values with various configuration options such as floating point support, precision, increment, and min/max values.
|
|
2092
|
+
*/
|
|
2093
|
+
function NumericPromptDeclarations() {
|
|
2094
|
+
return [
|
|
2095
|
+
createComponent(InterfaceDeclaration, {
|
|
2096
|
+
name: "NumberPromptConfig",
|
|
2097
|
+
"extends": "PromptConfig<number>",
|
|
2098
|
+
doc: "Configuration options for creating a numeric prompt",
|
|
2099
|
+
get children() {
|
|
2100
|
+
return [
|
|
2101
|
+
createComponent(InterfaceMember, {
|
|
2102
|
+
name: "isFloat",
|
|
2103
|
+
required: false,
|
|
2104
|
+
type: "boolean",
|
|
2105
|
+
doc: "Whether the prompt should accept floating point numbers"
|
|
2106
|
+
}),
|
|
2107
|
+
createComponent(Spacing, {}),
|
|
2108
|
+
createComponent(InterfaceMember, {
|
|
2109
|
+
name: "precision",
|
|
2110
|
+
required: false,
|
|
2111
|
+
type: "number",
|
|
2112
|
+
doc: "The number of decimal places to round the input to, defaults to 2"
|
|
2113
|
+
}),
|
|
2114
|
+
createComponent(Spacing, {}),
|
|
2115
|
+
createComponent(InterfaceMember, {
|
|
2116
|
+
name: "increment",
|
|
2117
|
+
required: false,
|
|
2118
|
+
type: "number",
|
|
2119
|
+
doc: "The increment value for the number prompt, defaults to 1"
|
|
2120
|
+
}),
|
|
2121
|
+
createComponent(Spacing, {}),
|
|
2122
|
+
createComponent(InterfaceMember, {
|
|
2123
|
+
name: "min",
|
|
2124
|
+
required: false,
|
|
2125
|
+
type: "number",
|
|
2126
|
+
doc: "The minimum value for the number prompt, defaults to -Infinity"
|
|
2127
|
+
}),
|
|
2128
|
+
createComponent(Spacing, {}),
|
|
2129
|
+
createComponent(InterfaceMember, {
|
|
2130
|
+
name: "max",
|
|
2131
|
+
required: false,
|
|
2132
|
+
type: "number",
|
|
2133
|
+
doc: "The maximum value for the number prompt, defaults to Infinity"
|
|
2134
|
+
})
|
|
2135
|
+
];
|
|
2136
|
+
}
|
|
2137
|
+
}),
|
|
2138
|
+
createComponent(Spacing, {}),
|
|
2139
|
+
createComponent(ClassDeclaration, {
|
|
2140
|
+
name: "NumberPrompt",
|
|
2141
|
+
doc: "A prompt for selecting a number input",
|
|
2142
|
+
"extends": "Prompt<number>",
|
|
2143
|
+
get children() {
|
|
2144
|
+
return [
|
|
2145
|
+
createComponent(ClassField, {
|
|
2146
|
+
name: "isInvalid",
|
|
2147
|
+
isPrivateMember: true,
|
|
2148
|
+
type: "boolean",
|
|
2149
|
+
children: code`false; `
|
|
2150
|
+
}),
|
|
2151
|
+
createComponent(Spacing, {}),
|
|
2152
|
+
createComponent(ClassField, {
|
|
2153
|
+
name: "initialValue",
|
|
2154
|
+
"protected": true,
|
|
2155
|
+
override: true,
|
|
2156
|
+
type: "number",
|
|
2157
|
+
children: code`0; `
|
|
2158
|
+
}),
|
|
2159
|
+
createIntrinsic("hbr", {}),
|
|
2160
|
+
createComponent(ClassField, {
|
|
2161
|
+
name: "defaultErrorMessage",
|
|
2162
|
+
"protected": true,
|
|
2163
|
+
override: true,
|
|
2164
|
+
type: "string",
|
|
2165
|
+
children: code`"A valid numeric value must be provided"; `
|
|
2166
|
+
}),
|
|
2167
|
+
createIntrinsic("hbr", {}),
|
|
2168
|
+
createComponent(ClassField, {
|
|
2169
|
+
name: "isFloat",
|
|
2170
|
+
"protected": true,
|
|
2171
|
+
type: "boolean",
|
|
2172
|
+
children: code`false; `
|
|
2173
|
+
}),
|
|
2174
|
+
createIntrinsic("hbr", {}),
|
|
2175
|
+
createComponent(ClassField, {
|
|
2176
|
+
name: "precision",
|
|
2177
|
+
"protected": true,
|
|
2178
|
+
type: "number",
|
|
2179
|
+
children: code`2; `
|
|
2180
|
+
}),
|
|
2181
|
+
createIntrinsic("hbr", {}),
|
|
2182
|
+
createComponent(ClassField, {
|
|
2183
|
+
name: "increment",
|
|
2184
|
+
"protected": true,
|
|
2185
|
+
type: "number",
|
|
2186
|
+
children: code`1; `
|
|
2187
|
+
}),
|
|
2188
|
+
createIntrinsic("hbr", {}),
|
|
2189
|
+
createComponent(ClassField, {
|
|
2190
|
+
name: "min",
|
|
2191
|
+
"protected": true,
|
|
2192
|
+
type: "number",
|
|
2193
|
+
children: code`Number.NEGATIVE_INFINITY; `
|
|
2194
|
+
}),
|
|
2195
|
+
createIntrinsic("hbr", {}),
|
|
2196
|
+
createComponent(ClassField, {
|
|
2197
|
+
name: "max",
|
|
2198
|
+
"protected": true,
|
|
2199
|
+
type: "number",
|
|
2200
|
+
children: code`Number.POSITIVE_INFINITY; `
|
|
2201
|
+
}),
|
|
2202
|
+
createComponent(Spacing, {}),
|
|
2203
|
+
code`constructor(config: NumberPromptConfig) {
|
|
514
2204
|
super(config);
|
|
515
2205
|
|
|
516
2206
|
if (config.initialValue) {
|
|
@@ -550,7 +2240,21 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
550
2240
|
this.cursor = 0;
|
|
551
2241
|
this.sync();
|
|
552
2242
|
this.last();
|
|
553
|
-
} `,
|
|
2243
|
+
} `,
|
|
2244
|
+
createComponent(Spacing, {}),
|
|
2245
|
+
createComponent(ClassMethod, {
|
|
2246
|
+
doc: "A method to handle key press events and determine the corresponding action",
|
|
2247
|
+
name: "onKeyPress",
|
|
2248
|
+
override: true,
|
|
2249
|
+
"protected": true,
|
|
2250
|
+
parameters: [{
|
|
2251
|
+
name: "char",
|
|
2252
|
+
type: "string"
|
|
2253
|
+
}, {
|
|
2254
|
+
name: "key",
|
|
2255
|
+
type: "Key"
|
|
2256
|
+
}],
|
|
2257
|
+
children: code`const action = this.getAction(key);
|
|
554
2258
|
if (action && typeof (this as any)[action] === "function") {
|
|
555
2259
|
return (this as any)[action]();
|
|
556
2260
|
}
|
|
@@ -578,10 +2282,45 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
578
2282
|
}
|
|
579
2283
|
|
|
580
2284
|
this.changeValue(value);
|
|
581
|
-
this.sync(); `
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
2285
|
+
this.sync(); `
|
|
2286
|
+
}),
|
|
2287
|
+
createComponent(Spacing, {}),
|
|
2288
|
+
createComponent(ClassMethod, {
|
|
2289
|
+
doc: "A method to handle changes in the prompt value",
|
|
2290
|
+
name: "onChange",
|
|
2291
|
+
override: true,
|
|
2292
|
+
"protected": true,
|
|
2293
|
+
parameters: [{
|
|
2294
|
+
name: "previousValue",
|
|
2295
|
+
type: "number"
|
|
2296
|
+
}],
|
|
2297
|
+
children: code`this.#isInvalid = false;
|
|
2298
|
+
this.cursor = this.displayValue.slice(0, this.cursor).length + 1; `
|
|
2299
|
+
}),
|
|
2300
|
+
createComponent(Spacing, {}),
|
|
2301
|
+
createComponent(ClassMethod, {
|
|
2302
|
+
doc: "A method to validate the prompt input",
|
|
2303
|
+
name: "checkValidations",
|
|
2304
|
+
override: true,
|
|
2305
|
+
async: true,
|
|
2306
|
+
"protected": true,
|
|
2307
|
+
children: code`await super.checkValidations(this.value);
|
|
2308
|
+
this.#isInvalid = this.isError; `
|
|
2309
|
+
}),
|
|
2310
|
+
createComponent(Spacing, {}),
|
|
2311
|
+
createComponent(ClassMethod, {
|
|
2312
|
+
doc: "A method to move the cursor to the end of the input",
|
|
2313
|
+
name: "next",
|
|
2314
|
+
"protected": true,
|
|
2315
|
+
children: code`this.changeValue(this.initialValue);
|
|
2316
|
+
this.sync(); `
|
|
2317
|
+
}),
|
|
2318
|
+
createComponent(Spacing, {}),
|
|
2319
|
+
createComponent(ClassMethod, {
|
|
2320
|
+
doc: "A method to move the cursor to the up",
|
|
2321
|
+
name: "up",
|
|
2322
|
+
"protected": true,
|
|
2323
|
+
children: code`let value = this.value;
|
|
585
2324
|
if (this.isPlaceholder) {
|
|
586
2325
|
value = this.min < 0 ? 0 : this.min;
|
|
587
2326
|
} else if (value >= this.max) {
|
|
@@ -591,7 +2330,14 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
591
2330
|
this.changeValue(value + this.increment);
|
|
592
2331
|
|
|
593
2332
|
this.sync();
|
|
594
|
-
this.last(); `
|
|
2333
|
+
this.last(); `
|
|
2334
|
+
}),
|
|
2335
|
+
createComponent(Spacing, {}),
|
|
2336
|
+
createComponent(ClassMethod, {
|
|
2337
|
+
doc: "A method to move the cursor to the down",
|
|
2338
|
+
name: "down",
|
|
2339
|
+
"protected": true,
|
|
2340
|
+
children: code`let value = this.value;
|
|
595
2341
|
if (this.isPlaceholder) {
|
|
596
2342
|
value = this.min < 0 ? 0 : this.min;
|
|
597
2343
|
} else if (value <= this.min) {
|
|
@@ -600,19 +2346,56 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
600
2346
|
|
|
601
2347
|
this.changeValue(value === this.min ? this.min : value - this.increment);
|
|
602
2348
|
this.sync();
|
|
603
|
-
this.last(); `
|
|
2349
|
+
this.last(); `
|
|
2350
|
+
}),
|
|
2351
|
+
createComponent(Spacing, {}),
|
|
2352
|
+
createComponent(ClassMethod, {
|
|
2353
|
+
doc: "A method to move the cursor to the left",
|
|
2354
|
+
name: "left",
|
|
2355
|
+
"protected": true,
|
|
2356
|
+
children: code`if (this.cursor <= 0) {
|
|
604
2357
|
return this.bell();
|
|
605
2358
|
}
|
|
606
2359
|
|
|
607
2360
|
this.moveCursor(-1);
|
|
608
|
-
this.sync(); `
|
|
2361
|
+
this.sync(); `
|
|
2362
|
+
}),
|
|
2363
|
+
createComponent(Spacing, {}),
|
|
2364
|
+
createComponent(ClassMethod, {
|
|
2365
|
+
doc: "A method to move the cursor to the right",
|
|
2366
|
+
name: "right",
|
|
2367
|
+
"protected": true,
|
|
2368
|
+
children: code`if (this.cursor >= this.displayValue.length) {
|
|
609
2369
|
return this.bell();
|
|
610
2370
|
}
|
|
611
2371
|
|
|
612
2372
|
this.moveCursor(1);
|
|
613
|
-
this.sync(); `
|
|
614
|
-
|
|
615
|
-
|
|
2373
|
+
this.sync(); `
|
|
2374
|
+
}),
|
|
2375
|
+
createComponent(Spacing, {}),
|
|
2376
|
+
createComponent(ClassMethod, {
|
|
2377
|
+
doc: "A method to move the cursor to the start",
|
|
2378
|
+
name: "first",
|
|
2379
|
+
"protected": true,
|
|
2380
|
+
children: code`this.cursor = 0;
|
|
2381
|
+
this.sync(); `
|
|
2382
|
+
}),
|
|
2383
|
+
createComponent(Spacing, {}),
|
|
2384
|
+
createComponent(ClassMethod, {
|
|
2385
|
+
doc: "A method to move the cursor to the end",
|
|
2386
|
+
name: "last",
|
|
2387
|
+
"protected": true,
|
|
2388
|
+
children: code`this.cursor = this.displayValue.length;
|
|
2389
|
+
this.sync(); `
|
|
2390
|
+
}),
|
|
2391
|
+
createComponent(Spacing, {}),
|
|
2392
|
+
createComponent(ClassMethod, {
|
|
2393
|
+
doc: "A method to render the prompt",
|
|
2394
|
+
name: "onRender",
|
|
2395
|
+
override: true,
|
|
2396
|
+
"protected": true,
|
|
2397
|
+
returnType: "string",
|
|
2398
|
+
children: code`return this.isPlaceholder
|
|
616
2399
|
? textColors.prompt.input.disabled(this.displayValue)
|
|
617
2400
|
: this.#isInvalid
|
|
618
2401
|
? textColors.prompt.input.error(this.displayValue)
|
|
@@ -620,7 +2403,24 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
620
2403
|
? textColors.prompt.input.submitted(this.displayValue)
|
|
621
2404
|
: this.isCancelled
|
|
622
2405
|
? textColors.prompt.input.cancelled(this.displayValue)
|
|
623
|
-
: bold(textColors.prompt.input.active(this.displayValue)); `
|
|
2406
|
+
: bold(textColors.prompt.input.active(this.displayValue)); `
|
|
2407
|
+
})
|
|
2408
|
+
];
|
|
2409
|
+
}
|
|
2410
|
+
}),
|
|
2411
|
+
createComponent(Spacing, {}),
|
|
2412
|
+
createComponent(TSDoc, { heading: "An object representing the configuration options for a numeric prompt." }),
|
|
2413
|
+
createComponent(TypeDeclaration, {
|
|
2414
|
+
name: "NumericConfig",
|
|
2415
|
+
"export": true,
|
|
2416
|
+
children: code`PromptFactoryConfig<number> & NumberPromptConfig; `
|
|
2417
|
+
}),
|
|
2418
|
+
createComponent(Spacing, {}),
|
|
2419
|
+
createComponent(TSDoc, {
|
|
2420
|
+
heading: "A function to create and run a numeric prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
2421
|
+
get children() {
|
|
2422
|
+
return [
|
|
2423
|
+
createComponent(TSDocExample, { children: `import { numeric, isCancel } from "shell-shock:prompts";
|
|
624
2424
|
|
|
625
2425
|
async function run() {
|
|
626
2426
|
const age = await numeric({
|
|
@@ -636,13 +2436,102 @@ async function run() {
|
|
|
636
2436
|
console.log("Your age is " + age + "!");
|
|
637
2437
|
}
|
|
638
2438
|
|
|
639
|
-
run(); `
|
|
2439
|
+
run(); ` }),
|
|
2440
|
+
createComponent(Spacing, {}),
|
|
2441
|
+
createComponent(TSDocParam, {
|
|
2442
|
+
name: "config",
|
|
2443
|
+
children: `The configuration options to pass to the numeric prompt, which extends the base PromptFactoryConfig with additional options specific to numeric prompts`
|
|
2444
|
+
}),
|
|
2445
|
+
createComponent(TSDocReturns, { children: `A promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled` })
|
|
2446
|
+
];
|
|
2447
|
+
}
|
|
2448
|
+
}),
|
|
2449
|
+
createComponent(FunctionDeclaration, {
|
|
2450
|
+
name: "numeric",
|
|
2451
|
+
"export": true,
|
|
2452
|
+
parameters: [{
|
|
2453
|
+
name: "config",
|
|
2454
|
+
type: "NumericConfig"
|
|
2455
|
+
}],
|
|
2456
|
+
returnType: "Promise<number | symbol>",
|
|
2457
|
+
children: code`return new Promise<number | symbol>((response, reject) => {
|
|
640
2458
|
const prompt = new NumberPrompt(config);
|
|
641
2459
|
|
|
642
2460
|
prompt.on("state", state => config.onState?.(state));
|
|
643
2461
|
prompt.on("submit", value => response(value));
|
|
644
2462
|
prompt.on("cancel", event => response(CANCEL_SYMBOL));
|
|
645
|
-
});`
|
|
2463
|
+
});`
|
|
2464
|
+
})
|
|
2465
|
+
];
|
|
2466
|
+
}
|
|
2467
|
+
/**
|
|
2468
|
+
* A component that renders the declarations for the built-in toggle prompt, which allows users to select a boolean value (true/false) with support for custom messages for the true and false states. This prompt type can be used for scenarios where the user needs to toggle a setting on or off, such as enabling or disabling a feature. The TogglePrompt class extends the base Prompt class and implements specific logic for handling boolean input and rendering interactions.
|
|
2469
|
+
*/
|
|
2470
|
+
function TogglePromptDeclarations() {
|
|
2471
|
+
return [
|
|
2472
|
+
createComponent(InterfaceDeclaration, {
|
|
2473
|
+
"export": true,
|
|
2474
|
+
name: "TogglePromptConfig",
|
|
2475
|
+
"extends": "PromptConfig<boolean>",
|
|
2476
|
+
doc: "Configuration options for creating a boolean toggle prompt",
|
|
2477
|
+
get children() {
|
|
2478
|
+
return [
|
|
2479
|
+
createComponent(InterfaceMember, {
|
|
2480
|
+
name: "trueMessage",
|
|
2481
|
+
required: false,
|
|
2482
|
+
type: "string",
|
|
2483
|
+
doc: "The message for the true state of the prompt"
|
|
2484
|
+
}),
|
|
2485
|
+
createComponent(Spacing, {}),
|
|
2486
|
+
createComponent(InterfaceMember, {
|
|
2487
|
+
name: "falseMessage",
|
|
2488
|
+
required: false,
|
|
2489
|
+
type: "string",
|
|
2490
|
+
doc: "The message for the false state of the prompt"
|
|
2491
|
+
}),
|
|
2492
|
+
createComponent(Spacing, {})
|
|
2493
|
+
];
|
|
2494
|
+
}
|
|
2495
|
+
}),
|
|
2496
|
+
createComponent(Spacing, {}),
|
|
2497
|
+
createComponent(ClassDeclaration, {
|
|
2498
|
+
"export": true,
|
|
2499
|
+
name: "TogglePrompt",
|
|
2500
|
+
doc: "A prompt for toggling a boolean input",
|
|
2501
|
+
"extends": "Prompt<boolean>",
|
|
2502
|
+
get children() {
|
|
2503
|
+
return [
|
|
2504
|
+
createComponent(ClassField, {
|
|
2505
|
+
name: "initialValue",
|
|
2506
|
+
"protected": true,
|
|
2507
|
+
override: true,
|
|
2508
|
+
type: "boolean",
|
|
2509
|
+
children: code`false; `
|
|
2510
|
+
}),
|
|
2511
|
+
createIntrinsic("hbr", {}),
|
|
2512
|
+
createComponent(ClassField, {
|
|
2513
|
+
name: "trueMessage",
|
|
2514
|
+
"protected": true,
|
|
2515
|
+
type: "string",
|
|
2516
|
+
children: code`"Yes"; `
|
|
2517
|
+
}),
|
|
2518
|
+
createIntrinsic("hbr", {}),
|
|
2519
|
+
createComponent(ClassField, {
|
|
2520
|
+
name: "falseMessage",
|
|
2521
|
+
"protected": true,
|
|
2522
|
+
type: "string",
|
|
2523
|
+
children: code`"No"; `
|
|
2524
|
+
}),
|
|
2525
|
+
createIntrinsic("hbr", {}),
|
|
2526
|
+
createComponent(ClassField, {
|
|
2527
|
+
name: "cursorHidden",
|
|
2528
|
+
"protected": true,
|
|
2529
|
+
override: true,
|
|
2530
|
+
type: "boolean",
|
|
2531
|
+
children: code`true; `
|
|
2532
|
+
}),
|
|
2533
|
+
createComponent(Spacing, {}),
|
|
2534
|
+
code`constructor(config: TogglePromptConfig) {
|
|
646
2535
|
super(config);
|
|
647
2536
|
|
|
648
2537
|
if (config.initialValue) {
|
|
@@ -657,17 +2546,45 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
657
2546
|
}
|
|
658
2547
|
|
|
659
2548
|
this.sync();
|
|
660
|
-
} `,
|
|
2549
|
+
} `,
|
|
2550
|
+
createComponent(Spacing, {}),
|
|
2551
|
+
createComponent(ClassMethod, {
|
|
2552
|
+
doc: "Update the toggle value to a checked state based on user input",
|
|
2553
|
+
name: "check",
|
|
2554
|
+
"protected": true,
|
|
2555
|
+
children: code`if (this.value === true) {
|
|
661
2556
|
return this.bell();
|
|
662
2557
|
}
|
|
663
2558
|
|
|
664
2559
|
this.changeValue(true);
|
|
665
|
-
this.sync(); `
|
|
2560
|
+
this.sync(); `
|
|
2561
|
+
}),
|
|
2562
|
+
createComponent(Spacing, {}),
|
|
2563
|
+
createComponent(ClassMethod, {
|
|
2564
|
+
doc: "Update the toggle value to an unchecked state based on user input",
|
|
2565
|
+
name: "uncheck",
|
|
2566
|
+
"protected": true,
|
|
2567
|
+
children: code`if (this.value === false) {
|
|
666
2568
|
return this.bell();
|
|
667
2569
|
}
|
|
668
2570
|
|
|
669
2571
|
this.changeValue(false);
|
|
670
|
-
this.sync(); `
|
|
2572
|
+
this.sync(); `
|
|
2573
|
+
}),
|
|
2574
|
+
createComponent(Spacing, {}),
|
|
2575
|
+
createComponent(ClassMethod, {
|
|
2576
|
+
doc: "A method to handle key press events and determine the corresponding action",
|
|
2577
|
+
name: "onKeyPress",
|
|
2578
|
+
override: true,
|
|
2579
|
+
"protected": true,
|
|
2580
|
+
parameters: [{
|
|
2581
|
+
name: "char",
|
|
2582
|
+
type: "string"
|
|
2583
|
+
}, {
|
|
2584
|
+
name: "key",
|
|
2585
|
+
type: "Key"
|
|
2586
|
+
}],
|
|
2587
|
+
children: code`const action = this.getAction(key);
|
|
671
2588
|
if (action && typeof (this as any)[action] === "function") {
|
|
672
2589
|
return (this as any)[action]();
|
|
673
2590
|
}
|
|
@@ -682,8 +2599,59 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
682
2599
|
return this.bell();
|
|
683
2600
|
}
|
|
684
2601
|
|
|
685
|
-
this.sync(); `
|
|
686
|
-
|
|
2602
|
+
this.sync(); `
|
|
2603
|
+
}),
|
|
2604
|
+
createComponent(Spacing, {}),
|
|
2605
|
+
createComponent(ClassMethod, {
|
|
2606
|
+
doc: "A method to remove the character backward of the cursor",
|
|
2607
|
+
name: "backspace",
|
|
2608
|
+
"protected": true,
|
|
2609
|
+
children: code`this.uncheck(); `
|
|
2610
|
+
}),
|
|
2611
|
+
createComponent(Spacing, {}),
|
|
2612
|
+
createComponent(ClassMethod, {
|
|
2613
|
+
doc: "A method to move the cursor to the left",
|
|
2614
|
+
name: "left",
|
|
2615
|
+
"protected": true,
|
|
2616
|
+
children: code`this.uncheck(); `
|
|
2617
|
+
}),
|
|
2618
|
+
createComponent(Spacing, {}),
|
|
2619
|
+
createComponent(ClassMethod, {
|
|
2620
|
+
doc: "A method to move the cursor to the right",
|
|
2621
|
+
name: "right",
|
|
2622
|
+
"protected": true,
|
|
2623
|
+
children: code`this.check(); `
|
|
2624
|
+
}),
|
|
2625
|
+
createComponent(Spacing, {}),
|
|
2626
|
+
createComponent(ClassMethod, {
|
|
2627
|
+
doc: "A method to move the cursor to down",
|
|
2628
|
+
name: "down",
|
|
2629
|
+
"protected": true,
|
|
2630
|
+
children: code`this.uncheck(); `
|
|
2631
|
+
}),
|
|
2632
|
+
createComponent(Spacing, {}),
|
|
2633
|
+
createComponent(ClassMethod, {
|
|
2634
|
+
doc: "A method to move the cursor to up",
|
|
2635
|
+
name: "up",
|
|
2636
|
+
"protected": true,
|
|
2637
|
+
children: code`this.check(); `
|
|
2638
|
+
}),
|
|
2639
|
+
createComponent(Spacing, {}),
|
|
2640
|
+
createComponent(ClassMethod, {
|
|
2641
|
+
doc: "A method to move to the next value",
|
|
2642
|
+
name: "next",
|
|
2643
|
+
"protected": true,
|
|
2644
|
+
children: code`this.changeValue(!this.value);
|
|
2645
|
+
this.sync(); `
|
|
2646
|
+
}),
|
|
2647
|
+
createComponent(Spacing, {}),
|
|
2648
|
+
createComponent(ClassMethod, {
|
|
2649
|
+
doc: "A method to render the prompt",
|
|
2650
|
+
name: "onRender",
|
|
2651
|
+
override: true,
|
|
2652
|
+
"protected": true,
|
|
2653
|
+
returnType: "string",
|
|
2654
|
+
children: code`return this.isSubmitted
|
|
687
2655
|
? textColors.prompt.input.submitted(this.value ? this.trueMessage : this.falseMessage)
|
|
688
2656
|
: this.isCancelled
|
|
689
2657
|
? textColors.prompt.input.cancelled(this.value ? this.trueMessage : this.falseMessage)
|
|
@@ -691,7 +2659,24 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
691
2659
|
this.value ? textColors.prompt.input.inactive(this.falseMessage) : underline(bold(textColors.prompt.input.active(this.falseMessage)))
|
|
692
2660
|
} \${borderColors.app.divider.tertiary("/")} \${
|
|
693
2661
|
this.value ? underline(bold(textColors.prompt.input.active(this.trueMessage))) : textColors.prompt.input.inactive(this.trueMessage)
|
|
694
|
-
}\`; `
|
|
2662
|
+
}\`; `
|
|
2663
|
+
})
|
|
2664
|
+
];
|
|
2665
|
+
}
|
|
2666
|
+
}),
|
|
2667
|
+
createComponent(Spacing, {}),
|
|
2668
|
+
createComponent(TSDoc, { heading: "An object representing the configuration options for a toggle prompt, which extends the base PromptFactoryConfig with additional options specific to the toggle prompt." }),
|
|
2669
|
+
createComponent(TypeDeclaration, {
|
|
2670
|
+
name: "ToggleConfig",
|
|
2671
|
+
"export": true,
|
|
2672
|
+
children: code`PromptFactoryConfig<boolean> & TogglePromptConfig; `
|
|
2673
|
+
}),
|
|
2674
|
+
createComponent(Spacing, {}),
|
|
2675
|
+
createComponent(TSDoc, {
|
|
2676
|
+
heading: "A function to create and run a toggle prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
2677
|
+
get children() {
|
|
2678
|
+
return [
|
|
2679
|
+
createComponent(TSDocExample, { children: `import { toggle, isCancel } from "shell-shock:prompts";
|
|
695
2680
|
|
|
696
2681
|
async function run() {
|
|
697
2682
|
const likesIceCream = await toggle({
|
|
@@ -705,13 +2690,161 @@ async function run() {
|
|
|
705
2690
|
console.log("You" + (likesIceCream ? " like ice cream" : " don't like ice cream") + "!");
|
|
706
2691
|
}
|
|
707
2692
|
|
|
708
|
-
run(); `
|
|
2693
|
+
run(); ` }),
|
|
2694
|
+
createComponent(Spacing, {}),
|
|
2695
|
+
createComponent(TSDocParam, {
|
|
2696
|
+
name: "config",
|
|
2697
|
+
children: `The configuration options to pass to the toggle prompt, which extends the base PromptFactoryConfig with additional options specific to the toggle prompt`
|
|
2698
|
+
}),
|
|
2699
|
+
createComponent(TSDocReturns, { children: `A promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled` })
|
|
2700
|
+
];
|
|
2701
|
+
}
|
|
2702
|
+
}),
|
|
2703
|
+
createComponent(FunctionDeclaration, {
|
|
2704
|
+
name: "toggle",
|
|
2705
|
+
"export": true,
|
|
2706
|
+
parameters: [{
|
|
2707
|
+
name: "config",
|
|
2708
|
+
type: "ToggleConfig"
|
|
2709
|
+
}],
|
|
2710
|
+
returnType: "Promise<boolean | symbol>",
|
|
2711
|
+
children: code`return new Promise<boolean | symbol>((response, reject) => {
|
|
709
2712
|
const prompt = new TogglePrompt(config);
|
|
710
2713
|
|
|
711
2714
|
prompt.on("state", state => config.onState?.(state));
|
|
712
2715
|
prompt.on("submit", value => response(value));
|
|
713
2716
|
prompt.on("cancel", event => response(CANCEL_SYMBOL));
|
|
714
|
-
});`
|
|
2717
|
+
});`
|
|
2718
|
+
})
|
|
2719
|
+
];
|
|
2720
|
+
}
|
|
2721
|
+
/**
|
|
2722
|
+
* A component that renders the declarations for the built-in confirm prompt, which allows users to select a boolean value (true/false) with support for custom messages for the true and false states. This prompt type can be used for scenarios where the user needs to toggle a setting on or off, such as enabling or disabling a feature. The ConfirmPrompt class extends the base Prompt class and implements specific logic for handling boolean input and rendering interactions.
|
|
2723
|
+
*/
|
|
2724
|
+
function ConfirmPromptDeclarations() {
|
|
2725
|
+
return [
|
|
2726
|
+
createComponent(InterfaceDeclaration, {
|
|
2727
|
+
"export": true,
|
|
2728
|
+
name: "ConfirmPromptConfig",
|
|
2729
|
+
"extends": "PromptConfig<boolean>",
|
|
2730
|
+
doc: "Configuration options for creating a boolean confirm prompt",
|
|
2731
|
+
get children() {
|
|
2732
|
+
return [
|
|
2733
|
+
createComponent(TSDoc, {
|
|
2734
|
+
heading: "The message for the \\`Yes\\` state of the prompt",
|
|
2735
|
+
get children() {
|
|
2736
|
+
return createComponent(TSDocDefaultValue, {
|
|
2737
|
+
type: "string",
|
|
2738
|
+
defaultValue: "Yes"
|
|
2739
|
+
});
|
|
2740
|
+
}
|
|
2741
|
+
}),
|
|
2742
|
+
createComponent(InterfaceMember, {
|
|
2743
|
+
name: "yesMessage",
|
|
2744
|
+
required: false,
|
|
2745
|
+
type: "string"
|
|
2746
|
+
}),
|
|
2747
|
+
createComponent(Spacing, {}),
|
|
2748
|
+
createComponent(TSDoc, {
|
|
2749
|
+
heading: "The \\`Yes\\` option when choosing between yes/no",
|
|
2750
|
+
get children() {
|
|
2751
|
+
return createComponent(TSDocDefaultValue, {
|
|
2752
|
+
type: "string",
|
|
2753
|
+
defaultValue: "1"
|
|
2754
|
+
});
|
|
2755
|
+
}
|
|
2756
|
+
}),
|
|
2757
|
+
createComponent(InterfaceMember, {
|
|
2758
|
+
name: "yesOption",
|
|
2759
|
+
required: false,
|
|
2760
|
+
type: "string"
|
|
2761
|
+
}),
|
|
2762
|
+
createComponent(Spacing, {}),
|
|
2763
|
+
createComponent(TSDoc, {
|
|
2764
|
+
heading: "The message for the \\`No\\` state of the prompt",
|
|
2765
|
+
get children() {
|
|
2766
|
+
return createComponent(TSDocDefaultValue, {
|
|
2767
|
+
type: "string",
|
|
2768
|
+
defaultValue: "(Y/n)"
|
|
2769
|
+
});
|
|
2770
|
+
}
|
|
2771
|
+
}),
|
|
2772
|
+
createComponent(InterfaceMember, {
|
|
2773
|
+
name: "noMessage",
|
|
2774
|
+
required: false,
|
|
2775
|
+
type: "string"
|
|
2776
|
+
}),
|
|
2777
|
+
createComponent(Spacing, {}),
|
|
2778
|
+
createComponent(TSDoc, {
|
|
2779
|
+
heading: "The \\`No\\` option when choosing between yes/no",
|
|
2780
|
+
get children() {
|
|
2781
|
+
return createComponent(TSDocDefaultValue, {
|
|
2782
|
+
type: "string",
|
|
2783
|
+
defaultValue: "(y/N)"
|
|
2784
|
+
});
|
|
2785
|
+
}
|
|
2786
|
+
}),
|
|
2787
|
+
createComponent(InterfaceMember, {
|
|
2788
|
+
name: "noOption",
|
|
2789
|
+
required: false,
|
|
2790
|
+
type: "string"
|
|
2791
|
+
})
|
|
2792
|
+
];
|
|
2793
|
+
}
|
|
2794
|
+
}),
|
|
2795
|
+
createComponent(Spacing, {}),
|
|
2796
|
+
createComponent(ClassDeclaration, {
|
|
2797
|
+
"export": true,
|
|
2798
|
+
name: "ConfirmPrompt",
|
|
2799
|
+
doc: "A prompt for confirming a boolean input",
|
|
2800
|
+
"extends": "Prompt<boolean>",
|
|
2801
|
+
get children() {
|
|
2802
|
+
return [
|
|
2803
|
+
createComponent(ClassField, {
|
|
2804
|
+
name: "initialValue",
|
|
2805
|
+
"protected": true,
|
|
2806
|
+
override: true,
|
|
2807
|
+
type: "boolean",
|
|
2808
|
+
children: code`false; `
|
|
2809
|
+
}),
|
|
2810
|
+
createIntrinsic("hbr", {}),
|
|
2811
|
+
createComponent(ClassField, {
|
|
2812
|
+
name: "yesMessage",
|
|
2813
|
+
"protected": true,
|
|
2814
|
+
type: "string",
|
|
2815
|
+
children: code`"Yes"; `
|
|
2816
|
+
}),
|
|
2817
|
+
createIntrinsic("hbr", {}),
|
|
2818
|
+
createComponent(ClassField, {
|
|
2819
|
+
name: "yesOption",
|
|
2820
|
+
"protected": true,
|
|
2821
|
+
type: "string",
|
|
2822
|
+
children: code`"(Y/n)"; `
|
|
2823
|
+
}),
|
|
2824
|
+
createIntrinsic("hbr", {}),
|
|
2825
|
+
createComponent(ClassField, {
|
|
2826
|
+
name: "noMessage",
|
|
2827
|
+
"protected": true,
|
|
2828
|
+
type: "string",
|
|
2829
|
+
children: code`"No"; `
|
|
2830
|
+
}),
|
|
2831
|
+
createIntrinsic("hbr", {}),
|
|
2832
|
+
createComponent(ClassField, {
|
|
2833
|
+
name: "noOption",
|
|
2834
|
+
"protected": true,
|
|
2835
|
+
type: "string",
|
|
2836
|
+
children: code`"(y/N)"; `
|
|
2837
|
+
}),
|
|
2838
|
+
createIntrinsic("hbr", {}),
|
|
2839
|
+
createComponent(ClassField, {
|
|
2840
|
+
name: "cursorHidden",
|
|
2841
|
+
"protected": true,
|
|
2842
|
+
override: true,
|
|
2843
|
+
type: "boolean",
|
|
2844
|
+
children: code`true; `
|
|
2845
|
+
}),
|
|
2846
|
+
createComponent(Spacing, {}),
|
|
2847
|
+
code`constructor(config: ConfirmPromptConfig) {
|
|
715
2848
|
super(config);
|
|
716
2849
|
|
|
717
2850
|
if (config.initialValue) {
|
|
@@ -732,7 +2865,21 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
732
2865
|
}
|
|
733
2866
|
|
|
734
2867
|
this.sync();
|
|
735
|
-
} `,
|
|
2868
|
+
} `,
|
|
2869
|
+
createComponent(Spacing, {}),
|
|
2870
|
+
createComponent(ClassMethod, {
|
|
2871
|
+
doc: "A method to handle key press events and determine the corresponding action",
|
|
2872
|
+
name: "onKeyPress",
|
|
2873
|
+
override: true,
|
|
2874
|
+
"protected": true,
|
|
2875
|
+
parameters: [{
|
|
2876
|
+
name: "char",
|
|
2877
|
+
type: "string"
|
|
2878
|
+
}, {
|
|
2879
|
+
name: "key",
|
|
2880
|
+
type: "Key"
|
|
2881
|
+
}],
|
|
2882
|
+
children: code`const action = this.getAction(key);
|
|
736
2883
|
if (action && typeof (this as any)[action] === "function") {
|
|
737
2884
|
return (this as any)[action]();
|
|
738
2885
|
}
|
|
@@ -745,11 +2892,37 @@ run(); `}),e(l,{}),e(b,{name:`config`,children:`The configuration options to pas
|
|
|
745
2892
|
return this.submit();
|
|
746
2893
|
} else {
|
|
747
2894
|
return this.bell();
|
|
748
|
-
} `
|
|
2895
|
+
} `
|
|
2896
|
+
}),
|
|
2897
|
+
createComponent(Spacing, {}),
|
|
2898
|
+
createComponent(ClassMethod, {
|
|
2899
|
+
doc: "A method to render the prompt",
|
|
2900
|
+
name: "onRender",
|
|
2901
|
+
override: true,
|
|
2902
|
+
"protected": true,
|
|
2903
|
+
returnType: "string",
|
|
2904
|
+
children: code`return this.isSubmitted
|
|
749
2905
|
? textColors.prompt.input.submitted(this.value ? this.yesMessage : this.noMessage)
|
|
750
2906
|
: this.isCancelled
|
|
751
2907
|
? textColors.prompt.input.cancelled(this.value ? this.yesMessage : this.noMessage)
|
|
752
|
-
: textColors.prompt.input.inactive(this.initialValue ? this.yesOption : this.noOption); `
|
|
2908
|
+
: textColors.prompt.input.inactive(this.initialValue ? this.yesOption : this.noOption); `
|
|
2909
|
+
})
|
|
2910
|
+
];
|
|
2911
|
+
}
|
|
2912
|
+
}),
|
|
2913
|
+
createComponent(Spacing, {}),
|
|
2914
|
+
createComponent(TSDoc, { heading: "An object representing the configuration options for a confirm prompt, which extends the base PromptFactoryConfig with additional options specific to the confirm prompt." }),
|
|
2915
|
+
createComponent(TypeDeclaration, {
|
|
2916
|
+
name: "ConfirmConfig",
|
|
2917
|
+
"export": true,
|
|
2918
|
+
children: code`PromptFactoryConfig<boolean> & ConfirmPromptConfig; `
|
|
2919
|
+
}),
|
|
2920
|
+
createComponent(Spacing, {}),
|
|
2921
|
+
createComponent(TSDoc, {
|
|
2922
|
+
heading: "A function to create and run a confirm prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
2923
|
+
get children() {
|
|
2924
|
+
return [
|
|
2925
|
+
createComponent(TSDocExample, { children: `import { confirm, isCancel } from "shell-shock:prompts";
|
|
753
2926
|
|
|
754
2927
|
async function run() {
|
|
755
2928
|
const likesIceCream = await confirm({
|
|
@@ -763,13 +2936,65 @@ async function run() {
|
|
|
763
2936
|
console.log("You" + (likesIceCream ? " like ice cream" : " don't like ice cream") + "!");
|
|
764
2937
|
}
|
|
765
2938
|
|
|
766
|
-
run(); `
|
|
2939
|
+
run(); ` }),
|
|
2940
|
+
createComponent(Spacing, {}),
|
|
2941
|
+
createComponent(TSDocParam, {
|
|
2942
|
+
name: "config",
|
|
2943
|
+
children: `The configuration options to pass to the confirm prompt, which extends the base PromptFactoryConfig with additional options specific to the confirm prompt`
|
|
2944
|
+
}),
|
|
2945
|
+
createComponent(TSDocReturns, { children: `A promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled` })
|
|
2946
|
+
];
|
|
2947
|
+
}
|
|
2948
|
+
}),
|
|
2949
|
+
createComponent(FunctionDeclaration, {
|
|
2950
|
+
name: "confirm",
|
|
2951
|
+
"export": true,
|
|
2952
|
+
parameters: [{
|
|
2953
|
+
name: "config",
|
|
2954
|
+
type: "ConfirmConfig"
|
|
2955
|
+
}],
|
|
2956
|
+
returnType: "Promise<boolean | symbol>",
|
|
2957
|
+
children: code`return new Promise<boolean | symbol>((response, reject) => {
|
|
767
2958
|
const prompt = new ConfirmPrompt(config);
|
|
768
2959
|
|
|
769
2960
|
prompt.on("state", state => config.onState?.(state));
|
|
770
2961
|
prompt.on("submit", value => response(value));
|
|
771
2962
|
prompt.on("cancel", event => response(CANCEL_SYMBOL));
|
|
772
|
-
}); `
|
|
2963
|
+
}); `
|
|
2964
|
+
})
|
|
2965
|
+
];
|
|
2966
|
+
}
|
|
2967
|
+
/**
|
|
2968
|
+
* Declarations for a password prompt that allows users to input and edit text, with support for cursor movement, deletion, and custom masking. This prompt type can be used for various text input scenarios, such as entering a password or any other string input. The PasswordPrompt class extends the base Prompt class and implements specific logic for handling password input and editing interactions.
|
|
2969
|
+
*/
|
|
2970
|
+
function PasswordPromptDeclaration() {
|
|
2971
|
+
return [
|
|
2972
|
+
createComponent(FunctionDeclaration, {
|
|
2973
|
+
"export": true,
|
|
2974
|
+
name: "passwordMask",
|
|
2975
|
+
doc: "A built-in prompt mask function that masks input with asterisks",
|
|
2976
|
+
parameters: [{
|
|
2977
|
+
name: "input",
|
|
2978
|
+
type: "string"
|
|
2979
|
+
}],
|
|
2980
|
+
returnType: "string",
|
|
2981
|
+
children: code`return "*".repeat(input.length); `
|
|
2982
|
+
}),
|
|
2983
|
+
createComponent(Spacing, {}),
|
|
2984
|
+
createComponent(TSDoc, { heading: "An object representing the configuration options for a password prompt, which extends the base PromptFactoryConfig with additional options specific to password prompts." }),
|
|
2985
|
+
createComponent(TypeDeclaration, {
|
|
2986
|
+
name: "PasswordConfig",
|
|
2987
|
+
"export": true,
|
|
2988
|
+
children: code`Omit<TextConfig, "mask" | "maskCompleted">; `
|
|
2989
|
+
}),
|
|
2990
|
+
createComponent(Spacing, {}),
|
|
2991
|
+
createComponent(TSDoc, {
|
|
2992
|
+
heading: "A function to create and run a password prompt, which returns a promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled.",
|
|
2993
|
+
get children() {
|
|
2994
|
+
return [
|
|
2995
|
+
createComponent(TSDocRemarks, { children: code`This function creates an instance of the TextPrompt class with the provided configuration options and a custom mask function to handle password input. It sets up event listeners for state updates, submission, and cancellation to handle the prompt interactions and return the appropriate results. The password prompt allows users to input text that is masked for privacy, making it suitable for scenarios like entering passwords or sensitive information.` }),
|
|
2996
|
+
createComponent(Spacing, {}),
|
|
2997
|
+
createComponent(TSDocExample, { children: `import { password, isCancel } from "shell-shock:prompts";
|
|
773
2998
|
|
|
774
2999
|
async function run() {
|
|
775
3000
|
const userPassword = await password({
|
|
@@ -783,18 +3008,63 @@ async function run() {
|
|
|
783
3008
|
console.log("You entered a password!");
|
|
784
3009
|
}
|
|
785
3010
|
|
|
786
|
-
run(); `
|
|
3011
|
+
run(); ` }),
|
|
3012
|
+
createComponent(Spacing, {}),
|
|
3013
|
+
createComponent(TSDocParam, {
|
|
3014
|
+
name: "config",
|
|
3015
|
+
children: `The configuration options to pass to the password prompt, which extends the base PromptConfig with additional options specific to password prompts`
|
|
3016
|
+
}),
|
|
3017
|
+
createComponent(TSDocReturns, { children: `A promise that resolves with the submitted value or rejects with a {@link CANCEL_SYMBOL | cancel symbol} if the prompt is cancelled` })
|
|
3018
|
+
];
|
|
3019
|
+
}
|
|
3020
|
+
}),
|
|
3021
|
+
createComponent(FunctionDeclaration, {
|
|
3022
|
+
name: "password",
|
|
3023
|
+
"export": true,
|
|
3024
|
+
parameters: [{
|
|
3025
|
+
name: "config",
|
|
3026
|
+
type: "PasswordConfig"
|
|
3027
|
+
}],
|
|
3028
|
+
returnType: "Promise<string | symbol>",
|
|
3029
|
+
children: code`return text({
|
|
787
3030
|
...config,
|
|
788
3031
|
mask: passwordMask,
|
|
789
3032
|
maskCompleted: () => "*******"
|
|
790
|
-
});`
|
|
3033
|
+
});`
|
|
3034
|
+
})
|
|
3035
|
+
];
|
|
3036
|
+
}
|
|
3037
|
+
function WaitForKeyPressDeclaration() {
|
|
3038
|
+
return [createComponent(TSDoc, {
|
|
3039
|
+
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.",
|
|
3040
|
+
get children() {
|
|
3041
|
+
return [
|
|
3042
|
+
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.` }),
|
|
3043
|
+
createComponent(Spacing, {}),
|
|
3044
|
+
createComponent(TSDocExample, { children: `import { waitForKeyPress } from "shell-shock:prompts";
|
|
791
3045
|
|
|
792
3046
|
async function run() {
|
|
793
3047
|
const result = await waitForKeyPress();
|
|
794
3048
|
console.log("A key was pressed!");
|
|
795
3049
|
}
|
|
796
3050
|
|
|
797
|
-
run(); `
|
|
3051
|
+
run(); ` }),
|
|
3052
|
+
createComponent(Spacing, {}),
|
|
3053
|
+
createComponent(TSDocParam, {
|
|
3054
|
+
name: "timeout",
|
|
3055
|
+
children: `The amount of time in milliseconds to wait before automatically resolving the prompt, defaults to 2 hours (7200000 ms)`
|
|
3056
|
+
}),
|
|
3057
|
+
createComponent(TSDocReturns, { children: `A promise that resolves when any key is pressed` })
|
|
3058
|
+
];
|
|
3059
|
+
}
|
|
3060
|
+
}), createComponent(FunctionDeclaration, {
|
|
3061
|
+
name: "waitForKeyPress",
|
|
3062
|
+
"export": true,
|
|
3063
|
+
parameters: [{
|
|
3064
|
+
name: "timeout",
|
|
3065
|
+
default: "7200000"
|
|
3066
|
+
}],
|
|
3067
|
+
children: code`process.stdin.setRawMode(true);
|
|
798
3068
|
return new Promise(resolve => process.stdin.once("data", () => {
|
|
799
3069
|
if (timeout >= 0) {
|
|
800
3070
|
setTimeout(() => {
|
|
@@ -805,5 +3075,89 @@ run(); `}),e(l,{}),e(b,{name:`timeout`,children:`The amount of time in milliseco
|
|
|
805
3075
|
|
|
806
3076
|
process.stdin.setRawMode(false);
|
|
807
3077
|
resolve(void 0);
|
|
808
|
-
})); `
|
|
3078
|
+
})); `
|
|
3079
|
+
})];
|
|
3080
|
+
}
|
|
3081
|
+
/**
|
|
3082
|
+
* A built-in prompts module for Shell Shock.
|
|
3083
|
+
*/
|
|
3084
|
+
function PromptsBuiltin(props) {
|
|
3085
|
+
const [{ children }, rest] = splitProps(props, ["children"]);
|
|
3086
|
+
return createComponent(BuiltinFile, mergeProps({
|
|
3087
|
+
id: "prompts",
|
|
3088
|
+
description: "A collection of prompts that allow for interactive input in command-line applications."
|
|
3089
|
+
}, rest, {
|
|
3090
|
+
get imports() {
|
|
3091
|
+
return defu(rest.imports ?? {}, {
|
|
3092
|
+
"node:events": "EventEmitter",
|
|
3093
|
+
"node:readline": [
|
|
3094
|
+
"Interface",
|
|
3095
|
+
"Key",
|
|
3096
|
+
"createInterface",
|
|
3097
|
+
"emitKeypressEvents"
|
|
3098
|
+
]
|
|
3099
|
+
});
|
|
3100
|
+
},
|
|
3101
|
+
get builtinImports() {
|
|
3102
|
+
return defu(rest.builtinImports ?? {}, {
|
|
3103
|
+
console: [
|
|
3104
|
+
"erase",
|
|
3105
|
+
"beep",
|
|
3106
|
+
"cursor",
|
|
3107
|
+
"textColors",
|
|
3108
|
+
"borderColors",
|
|
3109
|
+
"bold",
|
|
3110
|
+
"italic",
|
|
3111
|
+
"underline",
|
|
3112
|
+
"strikethrough",
|
|
3113
|
+
"clear",
|
|
3114
|
+
"stripAnsi",
|
|
3115
|
+
"splitText",
|
|
3116
|
+
"error"
|
|
3117
|
+
],
|
|
3118
|
+
utils: ["isUnicodeSupported"],
|
|
3119
|
+
env: [
|
|
3120
|
+
"env",
|
|
3121
|
+
"isCI",
|
|
3122
|
+
"isTest",
|
|
3123
|
+
"isWindows",
|
|
3124
|
+
"isDevelopment",
|
|
3125
|
+
"isDebug"
|
|
3126
|
+
]
|
|
3127
|
+
});
|
|
3128
|
+
},
|
|
3129
|
+
get children() {
|
|
3130
|
+
return [
|
|
3131
|
+
createComponent(Spacing, {}),
|
|
3132
|
+
createComponent(BasePromptDeclarations, {}),
|
|
3133
|
+
createComponent(Spacing, {}),
|
|
3134
|
+
createComponent(TextPromptDeclarations, {}),
|
|
3135
|
+
createComponent(Spacing, {}),
|
|
3136
|
+
createComponent(SelectPromptDeclarations, {}),
|
|
3137
|
+
createComponent(Spacing, {}),
|
|
3138
|
+
createComponent(MultiSelectPromptDeclarations, {}),
|
|
3139
|
+
createComponent(Spacing, {}),
|
|
3140
|
+
createComponent(NumericPromptDeclarations, {}),
|
|
3141
|
+
createComponent(Spacing, {}),
|
|
3142
|
+
createComponent(TogglePromptDeclarations, {}),
|
|
3143
|
+
createComponent(Spacing, {}),
|
|
3144
|
+
createComponent(PasswordPromptDeclaration, {}),
|
|
3145
|
+
createComponent(Spacing, {}),
|
|
3146
|
+
createComponent(ConfirmPromptDeclarations, {}),
|
|
3147
|
+
createComponent(Spacing, {}),
|
|
3148
|
+
createComponent(WaitForKeyPressDeclaration, {}),
|
|
3149
|
+
createComponent(Spacing, {}),
|
|
3150
|
+
createComponent(Show, {
|
|
3151
|
+
get when() {
|
|
3152
|
+
return Boolean(children);
|
|
3153
|
+
},
|
|
3154
|
+
children
|
|
3155
|
+
})
|
|
3156
|
+
];
|
|
3157
|
+
}
|
|
3158
|
+
}));
|
|
3159
|
+
}
|
|
3160
|
+
|
|
3161
|
+
//#endregion
|
|
3162
|
+
export { BasePromptDeclarations, ConfirmPromptDeclarations, MultiSelectPromptDeclarations, NumericPromptDeclarations, PasswordPromptDeclaration, PromptsBuiltin, SelectPromptDeclarations, TextPromptDeclarations, TogglePromptDeclarations, WaitForKeyPressDeclaration };
|
|
809
3163
|
//# sourceMappingURL=prompts-builtin.mjs.map
|