semola 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +17 -5
  2. package/dist/lib/api/index.cjs +1 -537
  3. package/dist/lib/api/index.d.cts +1 -271
  4. package/dist/lib/api/index.d.mts +80 -84
  5. package/dist/lib/api/index.mjs +1 -535
  6. package/dist/lib/cache/index.cjs +1 -99
  7. package/dist/lib/cache/index.d.cts +1 -27
  8. package/dist/lib/cache/index.mjs +1 -98
  9. package/dist/lib/cron/index.cjs +1 -735
  10. package/dist/lib/cron/index.d.cts +1 -146
  11. package/dist/lib/cron/index.d.mts +99 -36
  12. package/dist/lib/cron/index.mjs +1 -726
  13. package/dist/lib/errors/index.cjs +1 -30
  14. package/dist/lib/errors/index.d.cts +1 -13
  15. package/dist/lib/errors/index.mjs +1 -26
  16. package/dist/lib/i18n/index.cjs +1 -42
  17. package/dist/lib/i18n/index.d.cts +1 -28
  18. package/dist/lib/i18n/index.mjs +1 -41
  19. package/dist/lib/logging/index.cjs +1 -388
  20. package/dist/lib/logging/index.d.cts +1 -108
  21. package/dist/lib/logging/index.mjs +1 -374
  22. package/dist/lib/orm/index.cjs +1 -1642
  23. package/dist/lib/orm/index.d.cts +1 -402
  24. package/dist/lib/orm/index.d.mts +4 -2
  25. package/dist/lib/orm/index.mjs +1 -1630
  26. package/dist/lib/policy/index.cjs +1 -285
  27. package/dist/lib/policy/index.d.cts +1 -77
  28. package/dist/lib/policy/index.mjs +1 -265
  29. package/dist/lib/prompts/index.cjs +6 -769
  30. package/dist/lib/prompts/index.d.cts +1 -75
  31. package/dist/lib/prompts/index.mjs +6 -762
  32. package/dist/lib/pubsub/index.cjs +1 -141
  33. package/dist/lib/pubsub/index.d.cts +1 -26
  34. package/dist/lib/pubsub/index.mjs +1 -140
  35. package/dist/lib/queue/index.cjs +1 -212
  36. package/dist/lib/queue/index.d.cts +1 -81
  37. package/dist/lib/queue/index.mjs +1 -209
  38. package/dist/lib/workflow/index.cjs +1 -537
  39. package/dist/lib/workflow/index.d.cts +1 -150
  40. package/dist/lib/workflow/index.mjs +1 -527
  41. package/dist/rolldown-runtime-CMqjfN_6.cjs +1 -0
  42. package/package.json +7 -5
  43. package/dist/chunk-CKQMccvm.cjs +0 -28
@@ -1,762 +1,6 @@
1
- import { mightThrow, mightThrowSync } from "../errors/index.mjs";
2
- import { styleText } from "node:util";
3
- //#region src/lib/prompts/errors.ts
4
- var PromptIOError = class extends Error {
5
- constructor(message) {
6
- super(message);
7
- this.name = "PromptIOError";
8
- }
9
- };
10
- var PromptEnvironmentError = class extends Error {
11
- constructor(message) {
12
- super(message);
13
- this.name = "PromptEnvironmentError";
14
- }
15
- };
16
- var PromptCancelledError = class extends Error {
17
- constructor(message) {
18
- super(message);
19
- this.name = "PromptCancelledError";
20
- }
21
- };
22
- //#endregion
23
- //#region src/lib/prompts/core/keys.ts
24
- const ESC = "\x1B";
25
- const CSI = "\x1B[";
26
- const ESC_BACKSPACE = `${ESC}\u007F`;
27
- const CHAR_KEY_MAP = new Map(Object.values({
28
- enterCr: {
29
- value: "\r",
30
- name: "enter"
31
- },
32
- enterLf: {
33
- value: "\n",
34
- name: "enter"
35
- },
36
- ctrlA: {
37
- value: "",
38
- name: "ctrl_a"
39
- },
40
- ctrlC: {
41
- value: "",
42
- name: "ctrl_c"
43
- },
44
- ctrlBackspace: {
45
- value: "",
46
- name: "ctrl_backspace"
47
- },
48
- backspace: {
49
- value: "",
50
- name: "backspace"
51
- },
52
- tab: {
53
- value: " ",
54
- name: "tab"
55
- },
56
- space: {
57
- value: " ",
58
- name: "space"
59
- }
60
- }).map((entry) => [entry.value, { name: entry.name }]));
61
- const KNOWN_SEQUENCES = [
62
- {
63
- sequence: "\x1B[1;2D",
64
- key: { name: "shift_left" }
65
- },
66
- {
67
- sequence: "\x1B[1;2C",
68
- key: { name: "shift_right" }
69
- },
70
- {
71
- sequence: "\x1B[1;5D",
72
- key: { name: "ctrl_left" }
73
- },
74
- {
75
- sequence: "\x1B[1;5C",
76
- key: { name: "ctrl_right" }
77
- },
78
- {
79
- sequence: "\x1B[1;6D",
80
- key: { name: "shift_ctrl_left" }
81
- },
82
- {
83
- sequence: "\x1B[1;6C",
84
- key: { name: "shift_ctrl_right" }
85
- },
86
- {
87
- sequence: "\x1B[3~",
88
- key: { name: "delete" }
89
- },
90
- {
91
- sequence: "\x1B[H",
92
- key: { name: "home" }
93
- },
94
- {
95
- sequence: "\x1B[F",
96
- key: { name: "end" }
97
- },
98
- {
99
- sequence: "\x1B[A",
100
- key: { name: "up" }
101
- },
102
- {
103
- sequence: "\x1B[B",
104
- key: { name: "down" }
105
- },
106
- {
107
- sequence: "\x1B[C",
108
- key: { name: "right" }
109
- },
110
- {
111
- sequence: "\x1B[D",
112
- key: { name: "left" }
113
- }
114
- ];
115
- const isControlChar = (char) => {
116
- return char <= "" || char === "";
117
- };
118
- const isCsiFinalByte = (char) => {
119
- if (!char) return false;
120
- if (char >= "A" && char <= "Z") return true;
121
- if (char >= "a" && char <= "z") return true;
122
- if (char === "~") return true;
123
- return false;
124
- };
125
- const readCsiLength = (remaining) => {
126
- if (!remaining.startsWith(CSI)) return null;
127
- let index = 2;
128
- while (index < remaining.length) {
129
- if (isCsiFinalByte(remaining[index] ?? "")) return {
130
- length: index + 1,
131
- incomplete: false
132
- };
133
- index += 1;
134
- }
135
- return {
136
- length: 0,
137
- incomplete: true
138
- };
139
- };
140
- const parseKeys = (chunk) => {
141
- const keys = [];
142
- let cursor = 0;
143
- while (cursor < chunk.length) {
144
- const remaining = chunk.slice(cursor);
145
- if (remaining.startsWith(ESC_BACKSPACE)) {
146
- keys.push({ name: "ctrl_backspace" });
147
- cursor += 2;
148
- continue;
149
- }
150
- const knownSequence = KNOWN_SEQUENCES.find((entry) => remaining.startsWith(entry.sequence));
151
- if (knownSequence) {
152
- keys.push(knownSequence.key);
153
- cursor += knownSequence.sequence.length;
154
- continue;
155
- }
156
- const csiLength = readCsiLength(remaining);
157
- if (csiLength) {
158
- if (csiLength.incomplete) return {
159
- keys,
160
- remaining: chunk.slice(cursor)
161
- };
162
- cursor += csiLength.length;
163
- continue;
164
- }
165
- const char = chunk[cursor] ?? "";
166
- switch (char) {
167
- case ESC:
168
- if (cursor === chunk.length - 1) keys.push({ name: "escape" });
169
- break;
170
- default: {
171
- const key = CHAR_KEY_MAP.get(char);
172
- if (key) keys.push(key);
173
- else if (!isControlChar(char)) keys.push({
174
- name: "character",
175
- value: char
176
- });
177
- }
178
- }
179
- cursor += 1;
180
- }
181
- return {
182
- keys,
183
- remaining: ""
184
- };
185
- };
186
- //#endregion
187
- //#region src/lib/prompts/core/runtime.ts
188
- const HIDE_CURSOR = "\x1B[?25l";
189
- const SHOW_CURSOR = "\x1B[?25h";
190
- const countLines = (text) => {
191
- let count = 1;
192
- for (let i = 0; i < text.length; i++) if (text[i] === "\n") count += 1;
193
- return count;
194
- };
195
- var NodePromptRuntime = class {
196
- stdin;
197
- stdout;
198
- queue = [];
199
- waiters = [];
200
- initialized = false;
201
- previousLines = 0;
202
- buffer = "";
203
- constructor(stdin = process.stdin, stdout = process.stdout) {
204
- this.stdin = stdin;
205
- this.stdout = stdout;
206
- }
207
- isInteractive() {
208
- return Boolean(this.stdin.isTTY && this.stdout.isTTY && typeof this.stdin.setRawMode === "function");
209
- }
210
- init() {
211
- if (!this.isInteractive()) throw new PromptEnvironmentError("Interactive prompts require a TTY with raw mode support");
212
- if (this.initialized) return;
213
- const [initError] = mightThrowSync(() => {
214
- this.stdin.setRawMode(true);
215
- this.stdin.setEncoding("utf8");
216
- this.stdin.resume();
217
- this.stdin.on("data", this.onData);
218
- this.stdout.write(HIDE_CURSOR);
219
- });
220
- if (initError) throw new PromptIOError("Unable to initialize prompt runtime");
221
- this.initialized = true;
222
- }
223
- readKey() {
224
- const queued = this.queue.shift();
225
- if (queued) return Promise.resolve(queued);
226
- return new Promise((resolve, reject) => {
227
- this.waiters.push({
228
- resolve,
229
- reject
230
- });
231
- });
232
- }
233
- render(frame) {
234
- const [renderError] = mightThrowSync(() => {
235
- if (this.previousLines > 1) this.stdout.write(`\u001B[${this.previousLines - 1}A`);
236
- this.stdout.write("\r\x1B[J");
237
- this.stdout.write(frame);
238
- this.previousLines = countLines(frame);
239
- });
240
- if (renderError) throw new PromptIOError("Unable to render prompt frame");
241
- }
242
- done(frame) {
243
- const [doneError] = mightThrowSync(() => {
244
- if (this.previousLines > 1) this.stdout.write(`\u001B[${this.previousLines - 1}A`);
245
- this.stdout.write("\r\x1B[J");
246
- this.stdout.write(`${frame}\n`);
247
- this.previousLines = 0;
248
- });
249
- if (doneError) throw new PromptIOError("Unable to finalize prompt frame");
250
- }
251
- close() {
252
- if (!this.initialized) return;
253
- this.initialized = false;
254
- const [closeError] = mightThrowSync(() => {
255
- this.stdin.off("data", this.onData);
256
- this.stdin.setRawMode(false);
257
- this.stdin.pause?.();
258
- this.stdout.write(SHOW_CURSOR);
259
- });
260
- if (closeError) throw new PromptIOError("Unable to close prompt runtime");
261
- }
262
- interrupt() {
263
- this.close();
264
- process.exit(0);
265
- }
266
- onData = (chunk) => {
267
- const value = typeof chunk === "string" ? chunk : chunk.toString("utf8");
268
- const combined = `${this.buffer}${value}`;
269
- this.buffer = "";
270
- const [parseError, result] = mightThrowSync(() => parseKeys(combined));
271
- if (parseError || !result) {
272
- for (const waiter of this.waiters.splice(0)) waiter.reject(new PromptIOError("Failed to parse input"));
273
- return;
274
- }
275
- this.buffer = result.remaining;
276
- for (const key of result.keys) {
277
- const waiter = this.waiters.shift();
278
- if (waiter) waiter.resolve(key);
279
- else this.queue.push(key);
280
- }
281
- };
282
- };
283
- const createNodePromptRuntime = () => {
284
- return new NodePromptRuntime();
285
- };
286
- //#endregion
287
- //#region src/lib/prompts/core/session.ts
288
- const CANCEL_MESSAGE = "Interrupted, bye!";
289
- const resolveOutput = async (options, value) => {
290
- if (!options.transform) return value;
291
- const [transformError, transformed] = await mightThrow(Promise.resolve().then(() => options.transform?.(value)));
292
- if (transformError || transformed === null || transformed === void 0) throw new PromptIOError("Prompt transform callback failed unexpectedly");
293
- return transformed;
294
- };
295
- const runValidation = async (options, value) => {
296
- if (!options.validate) return null;
297
- const [validationError, validationMessage] = await mightThrow(Promise.resolve().then(() => options.validate?.(value)));
298
- if (validationError) throw new PromptIOError("Prompt validate callback failed unexpectedly");
299
- if (typeof validationMessage === "string" && validationMessage.length > 0) return validationMessage;
300
- return null;
301
- };
302
- const isCancelKey = (key) => {
303
- if (key.name === "ctrl_c") return true;
304
- if (key.name === "escape") return true;
305
- return false;
306
- };
307
- const runPromptSession = async (params) => {
308
- params.runtime.init();
309
- let state = params.initialState;
310
- let errorMessage = null;
311
- const closeAndDone = (message) => {
312
- params.runtime.close();
313
- params.runtime.done(message);
314
- };
315
- const [sessionError, finalValue] = await mightThrow((async () => {
316
- while (true) {
317
- params.runtime.render(params.render({
318
- options: params.options,
319
- state,
320
- errorMessage
321
- }));
322
- const key = await params.runtime.readKey();
323
- if (!key) throw new PromptIOError("Unable to read prompt input");
324
- if (isCancelKey(key)) {
325
- params.runtime.close();
326
- params.runtime.done(`✖ ${CANCEL_MESSAGE}`);
327
- if (params.runtime.interrupt) params.runtime.interrupt(CANCEL_MESSAGE);
328
- throw new PromptCancelledError(CANCEL_MESSAGE);
329
- }
330
- if (key.name !== "enter") {
331
- state = params.onKey(state, key);
332
- errorMessage = null;
333
- continue;
334
- }
335
- const submitResult = params.onSubmit(state);
336
- if ("errorMessage" in submitResult) {
337
- errorMessage = submitResult.errorMessage;
338
- continue;
339
- }
340
- const rawValue = submitResult.value;
341
- const validationErrorMessage = await runValidation(params.options, rawValue);
342
- if (validationErrorMessage) {
343
- errorMessage = validationErrorMessage;
344
- continue;
345
- }
346
- const outputValue = await resolveOutput(params.options, rawValue);
347
- if (outputValue === null) throw new PromptIOError("Unable to transform prompt value");
348
- closeAndDone(params.complete({
349
- options: params.options,
350
- state,
351
- value: outputValue
352
- }));
353
- return outputValue;
354
- }
355
- })());
356
- if (sessionError) {
357
- params.runtime.close();
358
- throw sessionError;
359
- }
360
- return finalValue;
361
- };
362
- //#endregion
363
- //#region src/lib/prompts/index.ts
364
- const pointer = (active) => {
365
- return active ? paint("cyan", "❯") : " ";
366
- };
367
- const addErrorLine = (content, errorMessage) => {
368
- if (!errorMessage) return content;
369
- return `${content}\n${errorMark()} ${paint("red", errorMessage)}`;
370
- };
371
- const insertAt = (value, at, character) => {
372
- return `${value.slice(0, at)}${character}${value.slice(at)}`;
373
- };
374
- const INVERSE = "\x1B[7m";
375
- const RESET = "\x1B[0m";
376
- const WHITESPACE = /\s/;
377
- const NUMBER_CHAR = /[0-9.-]/;
378
- const CURSOR_BLOCK = `${INVERSE} ${RESET}`;
379
- const paint = (color, text) => {
380
- return styleText(color, text, { validateStream: false });
381
- };
382
- const questionMark = () => paint("cyan", "?");
383
- const successMark = () => paint("green", "✔");
384
- const errorMark = () => paint("red", "✖");
385
- const renderQuestionLine = (message, content) => {
386
- return `${questionMark()} ${paint("cyan", message)} ${content}`;
387
- };
388
- const renderSuccessLine = (message, content) => {
389
- return `${successMark()} ${message} ${content}`;
390
- };
391
- const createTextState = (value) => {
392
- return {
393
- value,
394
- cursor: value.length,
395
- selectionAnchor: null
396
- };
397
- };
398
- const submitTextValue = (state, options) => {
399
- const value = state.value.length > 0 ? state.value : options.defaultValue ?? "";
400
- if (options.required && value.trim().length === 0) return { errorMessage: options.requiredMessage ?? "A value is required" };
401
- return { value };
402
- };
403
- const getSelectionRange = (state) => {
404
- if (state.selectionAnchor === null || state.selectionAnchor === state.cursor) return null;
405
- return {
406
- start: Math.min(state.selectionAnchor, state.cursor),
407
- end: Math.max(state.selectionAnchor, state.cursor)
408
- };
409
- };
410
- const moveCursor = (state, cursor, keepSelection) => {
411
- if (!keepSelection) return {
412
- ...state,
413
- cursor,
414
- selectionAnchor: null
415
- };
416
- return {
417
- ...state,
418
- cursor,
419
- selectionAnchor: state.selectionAnchor ?? state.cursor
420
- };
421
- };
422
- const deleteRange = (state, start, end) => {
423
- return {
424
- value: `${state.value.slice(0, start)}${state.value.slice(end)}`,
425
- cursor: start,
426
- selectionAnchor: null
427
- };
428
- };
429
- const deleteSelectedRange = (state) => {
430
- const selection = getSelectionRange(state);
431
- if (!selection) return state;
432
- return deleteRange(state, selection.start, selection.end);
433
- };
434
- const findWordStart = (value, cursor) => {
435
- let index = cursor;
436
- while (index > 0 && WHITESPACE.test(value[index - 1] ?? "")) index -= 1;
437
- while (index > 0 && !WHITESPACE.test(value[index - 1] ?? "")) index -= 1;
438
- return index;
439
- };
440
- const findWordEnd = (value, cursor) => {
441
- let index = cursor;
442
- while (index < value.length && WHITESPACE.test(value[index] ?? "")) index += 1;
443
- while (index < value.length && !WHITESPACE.test(value[index] ?? "")) index += 1;
444
- return index;
445
- };
446
- const renderTextSelection = (state, mask) => {
447
- const selection = getSelectionRange(state);
448
- const content = mask ? mask.repeat(state.value.length) : state.value;
449
- const boundedCursor = Math.max(0, Math.min(state.cursor, content.length));
450
- if (!selection) {
451
- if (content.length === 0) return CURSOR_BLOCK;
452
- if (boundedCursor === content.length) return `${content}${CURSOR_BLOCK}`;
453
- const focusedChar = content[boundedCursor] ?? " ";
454
- return `${content.slice(0, boundedCursor)}${INVERSE}${focusedChar}${RESET}${content.slice(boundedCursor + 1)}`;
455
- }
456
- const selected = content.slice(selection.start, selection.end);
457
- return `${content.slice(0, selection.start)}${INVERSE}${selected}${RESET}${content.slice(selection.end)}`;
458
- };
459
- const textOnKey = (state, key) => {
460
- switch (key.name) {
461
- case "ctrl_a": return {
462
- ...state,
463
- cursor: state.value.length,
464
- selectionAnchor: 0
465
- };
466
- case "home": return moveCursor(state, 0, false);
467
- case "end": return moveCursor(state, state.value.length, false);
468
- case "shift_ctrl_left": return moveCursor(state, findWordStart(state.value, state.cursor), true);
469
- case "shift_ctrl_right": return moveCursor(state, findWordEnd(state.value, state.cursor), true);
470
- case "left": return moveCursor(state, Math.max(state.cursor - 1, 0), false);
471
- case "shift_left": return moveCursor(state, Math.max(state.cursor - 1, 0), true);
472
- case "right": return moveCursor(state, Math.min(state.cursor + 1, state.value.length), false);
473
- case "ctrl_left": return moveCursor(state, findWordStart(state.value, state.cursor), false);
474
- case "ctrl_right": return moveCursor(state, findWordEnd(state.value, state.cursor), false);
475
- case "shift_right": return moveCursor(state, Math.min(state.cursor + 1, state.value.length), true);
476
- case "backspace": {
477
- const selectionDeleted = deleteSelectedRange(state);
478
- if (selectionDeleted !== state) return selectionDeleted;
479
- if (state.cursor === 0) return state;
480
- const nextCursor = state.cursor - 1;
481
- return {
482
- value: `${state.value.slice(0, nextCursor)}${state.value.slice(state.cursor)}`,
483
- cursor: nextCursor,
484
- selectionAnchor: null
485
- };
486
- }
487
- case "ctrl_backspace": {
488
- const selectionDeleted = deleteSelectedRange(state);
489
- if (selectionDeleted !== state) return selectionDeleted;
490
- if (state.cursor === 0) return state;
491
- return deleteRange(state, findWordStart(state.value, state.cursor), state.cursor);
492
- }
493
- case "delete": {
494
- const selectionDeleted = deleteSelectedRange(state);
495
- if (selectionDeleted !== state) return selectionDeleted;
496
- return {
497
- ...state,
498
- value: `${state.value.slice(0, state.cursor)}${state.value.slice(state.cursor + 1)}`
499
- };
500
- }
501
- case "character":
502
- case "space": {
503
- const collapsed = deleteSelectedRange(state);
504
- const text = key.name === "space" ? " " : key.value ?? "";
505
- return {
506
- value: insertAt(collapsed.value, collapsed.cursor, text),
507
- cursor: collapsed.cursor + 1,
508
- selectionAnchor: null
509
- };
510
- }
511
- default: return state;
512
- }
513
- };
514
- const input = async (options, runtime) => {
515
- return runPromptSession({
516
- runtime: runtime ?? createNodePromptRuntime(),
517
- options,
518
- initialState: createTextState(options.defaultValue ?? ""),
519
- render: ({ options: currentOptions, state, errorMessage }) => {
520
- let text = renderTextSelection(state);
521
- if (state.value.length === 0 && currentOptions.placeholder) text = `${CURSOR_BLOCK}${paint("dim", currentOptions.placeholder)}`;
522
- return addErrorLine(renderQuestionLine(currentOptions.message, text), errorMessage);
523
- },
524
- complete: ({ options: currentOptions, value }) => {
525
- return renderSuccessLine(currentOptions.message, value);
526
- },
527
- onKey: (state, key) => {
528
- return textOnKey(state, key);
529
- },
530
- onSubmit: (state) => submitTextValue(state, options)
531
- });
532
- };
533
- const password = async (options, runtime) => {
534
- const promptRuntime = runtime ?? createNodePromptRuntime();
535
- const initialValue = options.defaultValue ?? "";
536
- const mask = options.mask;
537
- return runPromptSession({
538
- runtime: promptRuntime,
539
- options,
540
- initialState: createTextState(initialValue),
541
- render: ({ options: currentOptions, state, errorMessage }) => {
542
- let visible;
543
- if (state.value.length === 0 && currentOptions.placeholder) visible = `${CURSOR_BLOCK}${paint("dim", currentOptions.placeholder)}`;
544
- else if (mask !== void 0) visible = renderTextSelection(state, mask);
545
- else visible = CURSOR_BLOCK;
546
- return addErrorLine(renderQuestionLine(currentOptions.message, visible), errorMessage);
547
- },
548
- complete: ({ options: currentOptions, value }) => {
549
- return renderSuccessLine(currentOptions.message, mask !== void 0 ? mask.repeat(value.length) : "");
550
- },
551
- onKey: (state, key) => {
552
- return textOnKey(state, key);
553
- },
554
- onSubmit: (state) => submitTextValue(state, options)
555
- });
556
- };
557
- const confirm = async (options, runtime) => {
558
- return runPromptSession({
559
- runtime: runtime ?? createNodePromptRuntime(),
560
- options,
561
- initialState: { value: options.defaultValue ?? false },
562
- render: ({ options: currentOptions, state, errorMessage }) => {
563
- const yesLabel = currentOptions.activeLabel ?? "Yes";
564
- const noLabel = currentOptions.inactiveLabel ?? "No";
565
- const rendered = state.value ? yesLabel : noLabel;
566
- return addErrorLine([renderQuestionLine(currentOptions.message, `(${rendered})`), paint("dim", " (y/n to select, ← yes / → no, space to toggle)")].join("\n"), errorMessage);
567
- },
568
- complete: ({ options: currentOptions, value }) => {
569
- const label = value ? currentOptions.activeLabel ?? "Yes" : currentOptions.inactiveLabel ?? "No";
570
- return renderSuccessLine(currentOptions.message, label);
571
- },
572
- onKey: (state, key) => {
573
- switch (key.name) {
574
- case "left": return { value: true };
575
- case "right": return { value: false };
576
- case "space": return { value: !state.value };
577
- case "character": {
578
- const lowered = (key.value ?? "").toLowerCase();
579
- if (lowered === "y") return { value: true };
580
- if (lowered === "n") return { value: false };
581
- return state;
582
- }
583
- default: return state;
584
- }
585
- },
586
- onSubmit: (state) => {
587
- return { value: state.value };
588
- }
589
- });
590
- };
591
- const number = async (options, runtime) => {
592
- return runPromptSession({
593
- runtime: runtime ?? createNodePromptRuntime(),
594
- options,
595
- initialState: createTextState(options.defaultValue === void 0 ? "" : String(options.defaultValue)),
596
- render: ({ options: currentOptions, state, errorMessage }) => {
597
- return addErrorLine(renderQuestionLine(currentOptions.message, renderTextSelection(state)), errorMessage);
598
- },
599
- complete: ({ options: currentOptions, value }) => {
600
- return renderSuccessLine(currentOptions.message, String(value));
601
- },
602
- onKey: (state, key) => {
603
- const next = textOnKey(state, key);
604
- if (!(key.name === "character" || key.name === "space")) return next;
605
- const value = key.name === "space" ? " " : key.value ?? "";
606
- if (!NUMBER_CHAR.test(value)) return state;
607
- if (value === "." || value === "-") {
608
- const collapsed = deleteSelectedRange(state);
609
- if (value === "." && collapsed.value.includes(".")) return state;
610
- if (value === "-") {
611
- if (collapsed.cursor !== 0) return state;
612
- if (collapsed.value.includes("-")) return state;
613
- }
614
- }
615
- return next;
616
- },
617
- onSubmit: (state) => {
618
- const raw = state.value.trim();
619
- if (raw.length === 0) return { errorMessage: options.requiredMessage ?? "A number is required" };
620
- const parsed = Number(raw);
621
- if (!Number.isFinite(parsed)) return { errorMessage: options.invalidMessage ?? "Please enter a valid number" };
622
- if (options.min !== void 0 && parsed < options.min) return { errorMessage: options.minMessage ?? `Number must be greater than or equal to ${options.min}` };
623
- if (options.max !== void 0 && parsed > options.max) return { errorMessage: options.maxMessage ?? `Number must be lower than or equal to ${options.max}` };
624
- return { value: parsed };
625
- }
626
- });
627
- };
628
- const findFirstEnabledIndex = (choices) => {
629
- const firstEnabledIndex = choices.findIndex((choice) => !choice.disabled);
630
- if (firstEnabledIndex >= 0) return firstEnabledIndex;
631
- return 0;
632
- };
633
- const findNextEnabledIndex = (choices, cursor, direction) => {
634
- const total = choices.length;
635
- let currentCursor = cursor;
636
- for (let offset = 0; offset < total; offset++) {
637
- const nextCursor = (currentCursor + direction + total) % total;
638
- const nextChoice = choices[nextCursor];
639
- if (nextChoice && !nextChoice.disabled) return nextCursor;
640
- currentCursor = nextCursor;
641
- }
642
- return currentCursor;
643
- };
644
- const findDefaultIndex = (choices, defaultValue) => {
645
- if (defaultValue === void 0) return findFirstEnabledIndex(choices);
646
- const index = choices.findIndex((choice) => choice.value === defaultValue && !choice.disabled);
647
- if (index >= 0) return index;
648
- return findFirstEnabledIndex(choices);
649
- };
650
- const select = async (options, runtime) => {
651
- return runPromptSession({
652
- runtime: runtime ?? createNodePromptRuntime(),
653
- options,
654
- initialState: { cursor: findDefaultIndex(options.choices, options.defaultValue) },
655
- render: ({ options: currentOptions, state, errorMessage }) => {
656
- const lines = [renderQuestionLine(currentOptions.message, "")];
657
- for (let index = 0; index < currentOptions.choices.length; index++) {
658
- const choice = currentOptions.choices[index];
659
- if (!choice) continue;
660
- const active = state.cursor === index;
661
- const label = choice.label ?? choice.value;
662
- const hint = choice.hint ? paint("dim", ` (${choice.hint})`) : "";
663
- const disabled = choice.disabled ? paint("yellow", " [disabled]") : "";
664
- lines.push(`${pointer(active)} ${label}${hint}${disabled}`);
665
- }
666
- return addErrorLine(lines.join("\n"), errorMessage);
667
- },
668
- complete: ({ options: currentOptions, value }) => {
669
- const selected = currentOptions.choices.find((choice) => choice.value === value);
670
- return renderSuccessLine(currentOptions.message, selected?.label ?? value);
671
- },
672
- onKey: (state, key) => {
673
- switch (key.name) {
674
- case "up": return { cursor: findNextEnabledIndex(options.choices, state.cursor, -1) };
675
- case "down": return { cursor: findNextEnabledIndex(options.choices, state.cursor, 1) };
676
- default: return state;
677
- }
678
- },
679
- onSubmit: (state) => {
680
- const selectedChoice = options.choices[state.cursor];
681
- if (!selectedChoice) return { errorMessage: "Please select an option" };
682
- if (selectedChoice.disabled) return { errorMessage: "Selected option is disabled" };
683
- return { value: selectedChoice.value };
684
- }
685
- });
686
- };
687
- const multiselect = async (options, runtime) => {
688
- return runPromptSession({
689
- runtime: runtime ?? createNodePromptRuntime(),
690
- options,
691
- initialState: {
692
- cursor: findFirstEnabledIndex(options.choices),
693
- selected: new Set(options.defaultValue ?? [])
694
- },
695
- render: ({ options: currentOptions, state, errorMessage }) => {
696
- const lines = [renderQuestionLine(currentOptions.message, "")];
697
- for (let index = 0; index < currentOptions.choices.length; index++) {
698
- const choice = currentOptions.choices[index];
699
- if (!choice) continue;
700
- const active = state.cursor === index;
701
- const checked = state.selected.has(choice.value) ? "◉" : "◯";
702
- const disabled = choice.disabled ? paint("yellow", " [disabled]") : "";
703
- const label = choice.label ?? choice.value;
704
- const hint = choice.hint ? paint("dim", ` (${choice.hint})`) : "";
705
- lines.push(`${pointer(active)} ${checked} ${label}${hint}${disabled}`);
706
- }
707
- lines.push(paint("dim", " (space to toggle, a to toggle all, enter to submit)"));
708
- return addErrorLine(lines.join("\n"), errorMessage);
709
- },
710
- complete: ({ options: currentOptions, value }) => {
711
- const labelMap = new Map(currentOptions.choices.map((choice) => [choice.value, choice.label]));
712
- const labels = value.map((v) => labelMap.get(v) ?? v);
713
- return renderSuccessLine(currentOptions.message, labels.join(", "));
714
- },
715
- onKey: (state, key) => {
716
- switch (key.name) {
717
- case "up": return {
718
- ...state,
719
- cursor: findNextEnabledIndex(options.choices, state.cursor, -1)
720
- };
721
- case "down": return {
722
- ...state,
723
- cursor: findNextEnabledIndex(options.choices, state.cursor, 1)
724
- };
725
- case "character":
726
- if ((key.value ?? "").toLowerCase() === "a") {
727
- const enabledValues = options.choices.filter((choice) => !choice.disabled).map((choice) => choice.value);
728
- if (!(enabledValues.length > 0)) return state;
729
- const areAllEnabledSelected = enabledValues.every((value) => state.selected.has(value));
730
- const nextSelection = new Set(state.selected);
731
- if (areAllEnabledSelected) for (const value of enabledValues) nextSelection.delete(value);
732
- else for (const value of enabledValues) nextSelection.add(value);
733
- return {
734
- ...state,
735
- selected: nextSelection
736
- };
737
- }
738
- return state;
739
- case "space": {
740
- const currentChoice = options.choices[state.cursor];
741
- if (!currentChoice || currentChoice.disabled) return state;
742
- const nextSelection = new Set(state.selected);
743
- if (nextSelection.has(currentChoice.value)) nextSelection.delete(currentChoice.value);
744
- else nextSelection.add(currentChoice.value);
745
- return {
746
- ...state,
747
- selected: nextSelection
748
- };
749
- }
750
- default: return state;
751
- }
752
- },
753
- onSubmit: (state) => {
754
- const values = options.choices.filter((choice) => state.selected.has(choice.value)).map((choice) => choice.value);
755
- if (options.min !== void 0 && values.length < options.min) return { errorMessage: `Please select at least ${options.min} options` };
756
- if (options.max !== void 0 && values.length > options.max) return { errorMessage: `Please select at most ${options.max} options` };
757
- return { value: values };
758
- }
759
- });
760
- };
761
- //#endregion
762
- export { confirm, input, multiselect, number, password, select };
1
+ import{mightThrow as e,mightThrowSync as t}from"../errors/index.mjs";import{styleText as n}from"node:util";var r=class extends Error{constructor(e){super(e),this.name=`PromptIOError`}},i=class extends Error{constructor(e){super(e),this.name=`PromptEnvironmentError`}},a=class extends Error{constructor(e){super(e),this.name=`PromptCancelledError`}};const o=new Map(Object.values({enterCr:{value:`\r`,name:`enter`},enterLf:{value:`
2
+ `,name:`enter`},ctrlA:{value:``,name:`ctrl_a`},ctrlC:{value:``,name:`ctrl_c`},ctrlBackspace:{value:``,name:`ctrl_backspace`},backspace:{value:``,name:`backspace`},tab:{value:` `,name:`tab`},space:{value:` `,name:`space`}}).map(e=>[e.value,{name:e.name}])),s=[{sequence:`\x1B[1;2D`,key:{name:`shift_left`}},{sequence:`\x1B[1;2C`,key:{name:`shift_right`}},{sequence:`\x1B[1;5D`,key:{name:`ctrl_left`}},{sequence:`\x1B[1;5C`,key:{name:`ctrl_right`}},{sequence:`\x1B[1;6D`,key:{name:`shift_ctrl_left`}},{sequence:`\x1B[1;6C`,key:{name:`shift_ctrl_right`}},{sequence:`\x1B[3~`,key:{name:`delete`}},{sequence:`\x1B[H`,key:{name:`home`}},{sequence:`\x1B[F`,key:{name:`end`}},{sequence:`\x1B[A`,key:{name:`up`}},{sequence:`\x1B[B`,key:{name:`down`}},{sequence:`\x1B[C`,key:{name:`right`}},{sequence:`\x1B[D`,key:{name:`left`}}],c=e=>e<=``||e===``,l=e=>e?e>=`A`&&e<=`Z`||e>=`a`&&e<=`z`||e===`~`:!1,u=e=>{if(!e.startsWith(`\x1B[`))return null;let t=2;for(;t<e.length;){let n=e[t]??``;if(l(n))return{length:t+1,incomplete:!1};t+=1}return{length:0,incomplete:!0}},d=e=>{let t=[],n=0;for(;n<e.length;){let r=e.slice(n);if(r.startsWith(`\x1B`)){t.push({name:`ctrl_backspace`}),n+=2;continue}let i=s.find(e=>r.startsWith(e.sequence));if(i){t.push(i.key),n+=i.sequence.length;continue}let a=u(r);if(a){if(a.incomplete)return{keys:t,remaining:e.slice(n)};n+=a.length;continue}let l=e[n]??``;switch(l){case`\x1B`:n===e.length-1&&t.push({name:`escape`});break;default:{let e=o.get(l);e?t.push(e):c(l)||t.push({name:`character`,value:l})}}n+=1}return{keys:t,remaining:``}},f=e=>{let t=1;for(let n=0;n<e.length;n++)e[n]===`
3
+ `&&(t+=1);return t};var p=class{stdin;stdout;queue=[];waiters=[];initialized=!1;previousLines=0;buffer=``;constructor(e=process.stdin,t=process.stdout){this.stdin=e,this.stdout=t}isInteractive(){return!!(this.stdin.isTTY&&this.stdout.isTTY&&typeof this.stdin.setRawMode==`function`)}init(){if(!this.isInteractive())throw new i(`Interactive prompts require a TTY with raw mode support`);if(this.initialized)return;let[e]=t(()=>{this.stdin.setRawMode(!0),this.stdin.setEncoding(`utf8`),this.stdin.resume(),this.stdin.on(`data`,this.onData),this.stdout.write(`\x1B[?25l`)});if(e)throw new r(`Unable to initialize prompt runtime`);this.initialized=!0}readKey(){let e=this.queue.shift();return e?Promise.resolve(e):new Promise((e,t)=>{this.waiters.push({resolve:e,reject:t})})}render(e){let[n]=t(()=>{this.previousLines>1&&this.stdout.write(`\u001B[${this.previousLines-1}A`),this.stdout.write(`\r\x1B[J`),this.stdout.write(e),this.previousLines=f(e)});if(n)throw new r(`Unable to render prompt frame`)}done(e){let[n]=t(()=>{this.previousLines>1&&this.stdout.write(`\u001B[${this.previousLines-1}A`),this.stdout.write(`\r\x1B[J`),this.stdout.write(`${e}\n`),this.previousLines=0});if(n)throw new r(`Unable to finalize prompt frame`)}close(){if(!this.initialized)return;this.initialized=!1;let[e]=t(()=>{this.stdin.off(`data`,this.onData),this.stdin.setRawMode(!1),this.stdin.pause?.(),this.stdout.write(`\x1B[?25h`)});if(e)throw new r(`Unable to close prompt runtime`)}interrupt(){this.close(),process.exit(0)}onData=e=>{let n=typeof e==`string`?e:e.toString(`utf8`),i=`${this.buffer}${n}`;this.buffer=``;let[a,o]=t(()=>d(i));if(a||!o){for(let e of this.waiters.splice(0))e.reject(new r(`Failed to parse input`));return}this.buffer=o.remaining;for(let e of o.keys){let t=this.waiters.shift();t?t.resolve(e):this.queue.push(e)}}};const m=()=>new p,h=`Interrupted, bye!`,g=async(t,n)=>{if(!t.transform)return n;let[i,a]=await e(Promise.resolve().then(()=>t.transform?.(n)));if(i||a==null)throw new r(`Prompt transform callback failed unexpectedly`);return a},_=async(t,n)=>{if(!t.validate)return null;let[i,a]=await e(Promise.resolve().then(()=>t.validate?.(n)));if(i)throw new r(`Prompt validate callback failed unexpectedly`);return typeof a==`string`&&a.length>0?a:null},v=e=>e.name===`ctrl_c`||e.name===`escape`,y=async t=>{t.runtime.init();let n=t.initialState,i=null,o=e=>{t.runtime.close(),t.runtime.done(e)},[s,c]=await e((async()=>{for(;;){t.runtime.render(t.render({options:t.options,state:n,errorMessage:i}));let e=await t.runtime.readKey();if(!e)throw new r(`Unable to read prompt input`);if(v(e))throw t.runtime.close(),t.runtime.done(`✖ ${h}`),t.runtime.interrupt&&t.runtime.interrupt(h),new a(h);if(e.name!==`enter`){n=t.onKey(n,e),i=null;continue}let s=t.onSubmit(n);if(`errorMessage`in s){i=s.errorMessage;continue}let c=s.value,l=await _(t.options,c);if(l){i=l;continue}let u=await g(t.options,c);if(u===null)throw new r(`Unable to transform prompt value`);return o(t.complete({options:t.options,state:n,value:u})),u}})());if(s)throw t.runtime.close(),s;return c},b=e=>e?O(`cyan`,`❯`):` `,x=(e,t)=>t?`${e}\n${j()} ${O(`red`,t)}`:e,S=(e,t,n)=>`${e.slice(0,t)}${n}${e.slice(t)}`,C=`\x1B[7m`,w=`\x1B[0m`,T=/\s/,E=/[0-9.-]/,D=`${C} ${w}`,O=(e,t)=>n(e,t,{validateStream:!1}),k=()=>O(`cyan`,`?`),A=()=>O(`green`,`✔`),j=()=>O(`red`,`✖`),M=(e,t)=>`${k()} ${O(`cyan`,e)} ${t}`,N=(e,t)=>`${A()} ${e} ${t}`,P=e=>({value:e,cursor:e.length,selectionAnchor:null}),F=(e,t)=>{let n=e.value.length>0?e.value:t.defaultValue??``;return t.required&&n.trim().length===0?{errorMessage:t.requiredMessage??`A value is required`}:{value:n}},I=e=>e.selectionAnchor===null||e.selectionAnchor===e.cursor?null:{start:Math.min(e.selectionAnchor,e.cursor),end:Math.max(e.selectionAnchor,e.cursor)},L=(e,t,n)=>n?{...e,cursor:t,selectionAnchor:e.selectionAnchor??e.cursor}:{...e,cursor:t,selectionAnchor:null},R=(e,t,n)=>({value:`${e.value.slice(0,t)}${e.value.slice(n)}`,cursor:t,selectionAnchor:null}),z=e=>{let t=I(e);return t?R(e,t.start,t.end):e},B=(e,t)=>{let n=t;for(;n>0&&T.test(e[n-1]??``);)--n;for(;n>0&&!T.test(e[n-1]??``);)--n;return n},V=(e,t)=>{let n=t;for(;n<e.length&&T.test(e[n]??``);)n+=1;for(;n<e.length&&!T.test(e[n]??``);)n+=1;return n},H=(e,t)=>{let n=I(e),r=t?t.repeat(e.value.length):e.value,i=Math.max(0,Math.min(e.cursor,r.length));if(!n){if(r.length===0)return D;if(i===r.length)return`${r}${D}`;let e=r[i]??` `;return`${r.slice(0,i)}${C}${e}${w}${r.slice(i+1)}`}let a=r.slice(n.start,n.end);return`${r.slice(0,n.start)}${C}${a}${w}${r.slice(n.end)}`},U=(e,t)=>{switch(t.name){case`ctrl_a`:return{...e,cursor:e.value.length,selectionAnchor:0};case`home`:return L(e,0,!1);case`end`:return L(e,e.value.length,!1);case`shift_ctrl_left`:return L(e,B(e.value,e.cursor),!0);case`shift_ctrl_right`:return L(e,V(e.value,e.cursor),!0);case`left`:return L(e,Math.max(e.cursor-1,0),!1);case`shift_left`:return L(e,Math.max(e.cursor-1,0),!0);case`right`:return L(e,Math.min(e.cursor+1,e.value.length),!1);case`ctrl_left`:return L(e,B(e.value,e.cursor),!1);case`ctrl_right`:return L(e,V(e.value,e.cursor),!1);case`shift_right`:return L(e,Math.min(e.cursor+1,e.value.length),!0);case`backspace`:{let t=z(e);if(t!==e)return t;if(e.cursor===0)return e;let n=e.cursor-1;return{value:`${e.value.slice(0,n)}${e.value.slice(e.cursor)}`,cursor:n,selectionAnchor:null}}case`ctrl_backspace`:{let t=z(e);if(t!==e)return t;if(e.cursor===0)return e;let n=B(e.value,e.cursor);return R(e,n,e.cursor)}case`delete`:{let t=z(e);return t===e?{...e,value:`${e.value.slice(0,e.cursor)}${e.value.slice(e.cursor+1)}`}:t}case`character`:case`space`:{let n=z(e),r=t.name===`space`?` `:t.value??``;return{value:S(n.value,n.cursor,r),cursor:n.cursor+1,selectionAnchor:null}}default:return e}},W=async(e,t)=>{let n=t??m(),r=e.defaultValue??``;return y({runtime:n,options:e,initialState:P(r),render:({options:e,state:t,errorMessage:n})=>{let r=H(t);return t.value.length===0&&e.placeholder&&(r=`${D}${O(`dim`,e.placeholder)}`),x(M(e.message,r),n)},complete:({options:e,value:t})=>N(e.message,t),onKey:(e,t)=>U(e,t),onSubmit:t=>F(t,e)})},G=async(e,t)=>{let n=t??m(),r=e.defaultValue??``,i=e.mask;return y({runtime:n,options:e,initialState:P(r),render:({options:e,state:t,errorMessage:n})=>{let r;return r=t.value.length===0&&e.placeholder?`${D}${O(`dim`,e.placeholder)}`:i===void 0?D:H(t,i),x(M(e.message,r),n)},complete:({options:e,value:t})=>N(e.message,i===void 0?``:i.repeat(t.length)),onKey:(e,t)=>U(e,t),onSubmit:t=>F(t,e)})},K=async(e,t)=>y({runtime:t??m(),options:e,initialState:{value:e.defaultValue??!1},render:({options:e,state:t,errorMessage:n})=>{let r=e.activeLabel??`Yes`,i=e.inactiveLabel??`No`,a=t.value?r:i;return x([M(e.message,`(${a})`),O(`dim`,` (y/n to select, ← yes / → no, space to toggle)`)].join(`
4
+ `),n)},complete:({options:e,value:t})=>{let n=t?e.activeLabel??`Yes`:e.inactiveLabel??`No`;return N(e.message,n)},onKey:(e,t)=>{switch(t.name){case`left`:return{value:!0};case`right`:return{value:!1};case`space`:return{value:!e.value};case`character`:{let n=(t.value??``).toLowerCase();return n===`y`?{value:!0}:n===`n`?{value:!1}:e}default:return e}},onSubmit:e=>({value:e.value})}),q=async(e,t)=>{let n=t??m(),r=e.defaultValue===void 0?``:String(e.defaultValue);return y({runtime:n,options:e,initialState:P(r),render:({options:e,state:t,errorMessage:n})=>x(M(e.message,H(t)),n),complete:({options:e,value:t})=>N(e.message,String(t)),onKey:(e,t)=>{let n=U(e,t);if(!(t.name===`character`||t.name===`space`))return n;let r=t.name===`space`?` `:t.value??``;if(!E.test(r))return e;if(r===`.`||r===`-`){let t=z(e);if(r===`.`&&t.value.includes(`.`)||r===`-`&&(t.cursor!==0||t.value.includes(`-`)))return e}return n},onSubmit:t=>{let n=t.value.trim();if(n.length===0)return{errorMessage:e.requiredMessage??`A number is required`};let r=Number(n);return Number.isFinite(r)?e.min!==void 0&&r<e.min?{errorMessage:e.minMessage??`Number must be greater than or equal to ${e.min}`}:e.max!==void 0&&r>e.max?{errorMessage:e.maxMessage??`Number must be lower than or equal to ${e.max}`}:{value:r}:{errorMessage:e.invalidMessage??`Please enter a valid number`}}})},J=e=>{let t=e.findIndex(e=>!e.disabled);return t>=0?t:0},Y=(e,t,n)=>{let r=e.length,i=t;for(let t=0;t<r;t++){let t=(i+n+r)%r,a=e[t];if(a&&!a.disabled)return t;i=t}return i},X=(e,t)=>{if(t===void 0)return J(e);let n=e.findIndex(e=>e.value===t&&!e.disabled);return n>=0?n:J(e)},Z=async(e,t)=>y({runtime:t??m(),options:e,initialState:{cursor:X(e.choices,e.defaultValue)},render:({options:e,state:t,errorMessage:n})=>{let r=[M(e.message,``)];for(let n=0;n<e.choices.length;n++){let i=e.choices[n];if(!i)continue;let a=t.cursor===n,o=i.label??i.value,s=i.hint?O(`dim`,` (${i.hint})`):``,c=i.disabled?O(`yellow`,` [disabled]`):``;r.push(`${b(a)} ${o}${s}${c}`)}return x(r.join(`
5
+ `),n)},complete:({options:e,value:t})=>{let n=e.choices.find(e=>e.value===t);return N(e.message,n?.label??t)},onKey:(t,n)=>{switch(n.name){case`up`:return{cursor:Y(e.choices,t.cursor,-1)};case`down`:return{cursor:Y(e.choices,t.cursor,1)};default:return t}},onSubmit:t=>{let n=e.choices[t.cursor];return n?n.disabled?{errorMessage:`Selected option is disabled`}:{value:n.value}:{errorMessage:`Please select an option`}}}),Q=async(e,t)=>y({runtime:t??m(),options:e,initialState:{cursor:J(e.choices),selected:new Set(e.defaultValue??[])},render:({options:e,state:t,errorMessage:n})=>{let r=[M(e.message,``)];for(let n=0;n<e.choices.length;n++){let i=e.choices[n];if(!i)continue;let a=t.cursor===n,o=t.selected.has(i.value)?`◉`:`◯`,s=i.disabled?O(`yellow`,` [disabled]`):``,c=i.label??i.value,l=i.hint?O(`dim`,` (${i.hint})`):``;r.push(`${b(a)} ${o} ${c}${l}${s}`)}return r.push(O(`dim`,` (space to toggle, a to toggle all, enter to submit)`)),x(r.join(`
6
+ `),n)},complete:({options:e,value:t})=>{let n=new Map(e.choices.map(e=>[e.value,e.label])),r=t.map(e=>n.get(e)??e);return N(e.message,r.join(`, `))},onKey:(t,n)=>{switch(n.name){case`up`:return{...t,cursor:Y(e.choices,t.cursor,-1)};case`down`:return{...t,cursor:Y(e.choices,t.cursor,1)};case`character`:if((n.value??``).toLowerCase()===`a`){let n=e.choices.filter(e=>!e.disabled).map(e=>e.value);if(!(n.length>0))return t;let r=n.every(e=>t.selected.has(e)),i=new Set(t.selected);if(r)for(let e of n)i.delete(e);else for(let e of n)i.add(e);return{...t,selected:i}}return t;case`space`:{let n=e.choices[t.cursor];if(!n||n.disabled)return t;let r=new Set(t.selected);return r.has(n.value)?r.delete(n.value):r.add(n.value),{...t,selected:r}}default:return t}},onSubmit:t=>{let n=e.choices.filter(e=>t.selected.has(e.value)).map(e=>e.value);return e.min!==void 0&&n.length<e.min?{errorMessage:`Please select at least ${e.min} options`}:e.max!==void 0&&n.length>e.max?{errorMessage:`Please select at most ${e.max} options`}:{value:n}}});export{K as confirm,W as input,Q as multiselect,q as number,G as password,Z as select};