@hyperfrontend/questions 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,13 +1,340 @@
1
1
  /**
2
- * Terminal prompting library with composable, functional API for text, select, confirm, and multiselect prompts.
3
- *
4
- * @module @hyperfrontend/questions
5
- */
6
- export type { Choice, ConfirmConfig, MultiselectConfig, PromptCancelledOutcome, PromptConfig, PromptFunction, PromptOutcome, PromptSubmittedOutcome, SelectConfig, TextConfig, } from './types';
7
- export { confirm } from './prompts/confirm';
8
- export { multiselect } from './prompts/multiselect';
9
- export { select } from './prompts/select';
10
- export { text } from './prompts/text';
11
- export { style } from './render';
12
- export { PromptResult } from './types';
13
- //# sourceMappingURL=index.d.ts.map
2
+ * Result state of a prompt interaction.
3
+ */
4
+ declare const PromptResult: Readonly<{
5
+ /** User submitted a value */
6
+ readonly Submitted: "submitted";
7
+ /** User cancelled the prompt (Ctrl+C) */
8
+ readonly Cancelled: "cancelled";
9
+ }>;
10
+ /** Result state of a prompt interaction. */
11
+ type PromptResult = (typeof PromptResult)[keyof typeof PromptResult];
12
+ /**
13
+ * Base configuration shared by all prompts.
14
+ */
15
+ interface PromptConfig {
16
+ /** The question message to display */
17
+ readonly message: string;
18
+ /** Stream to read input from (defaults to stdin) */
19
+ readonly input?: NodeJS.ReadStream;
20
+ /** Stream to write output to (defaults to stdout) */
21
+ readonly output?: NodeJS.WriteStream;
22
+ }
23
+ /**
24
+ * Outcome when user submits a value.
25
+ */
26
+ interface PromptSubmittedOutcome<T> {
27
+ /** Submission result indicator */
28
+ readonly result: typeof PromptResult.Submitted;
29
+ /** The submitted value */
30
+ readonly value: T;
31
+ }
32
+ /**
33
+ * Outcome when user cancels the prompt.
34
+ */
35
+ interface PromptCancelledOutcome {
36
+ /** Cancellation result indicator */
37
+ readonly result: typeof PromptResult.Cancelled;
38
+ /** No value on cancellation */
39
+ readonly value: undefined;
40
+ }
41
+ /**
42
+ * Outcome of a prompt interaction.
43
+ */
44
+ type PromptOutcome<T> = PromptSubmittedOutcome<T> | PromptCancelledOutcome;
45
+ /**
46
+ * A prompt function that asks a question and returns a value.
47
+ */
48
+ type PromptFunction<TConfig extends PromptConfig, TValue> = (config: TConfig) => Promise<PromptOutcome<TValue>>;
49
+ /**
50
+ * Choice item for select/multiselect prompts.
51
+ */
52
+ interface Choice<T = string> {
53
+ /** Display label */
54
+ readonly label: string;
55
+ /** Value returned when selected */
56
+ readonly value: T;
57
+ /** Optional hint text shown after label */
58
+ readonly hint?: string;
59
+ /** Whether this choice is disabled */
60
+ readonly disabled?: boolean;
61
+ }
62
+ /**
63
+ * Configuration for text input prompts.
64
+ */
65
+ interface TextConfig extends PromptConfig {
66
+ /** Default value if user submits empty input */
67
+ readonly initial?: string;
68
+ /** Validator function - return error message string to reject, undefined to accept */
69
+ readonly validate?: (value: string) => string | undefined;
70
+ /** Transform display of input (e.g., for password masking) */
71
+ readonly format?: (value: string) => string;
72
+ /** Dynamic message recomputed on every keystroke; overrides `message` when present */
73
+ readonly renderMessage?: (value: string) => string;
74
+ }
75
+ /**
76
+ * Configuration for confirmation prompts.
77
+ */
78
+ interface ConfirmConfig extends PromptConfig {
79
+ /** Default value if user just presses enter */
80
+ readonly initial?: boolean;
81
+ }
82
+ /**
83
+ * Configuration for single-select prompts.
84
+ */
85
+ interface SelectConfig<T = string> extends PromptConfig {
86
+ /** Available choices */
87
+ readonly choices: ReadonlyArray<Choice<T>>;
88
+ /** Index of initially selected choice */
89
+ readonly initial?: number;
90
+ /** Maximum number of visible choices (enables scrolling) */
91
+ readonly maxVisible?: number;
92
+ /** Enable type-to-filter */
93
+ readonly searchable?: boolean;
94
+ }
95
+ /**
96
+ * Configuration for multi-select prompts.
97
+ */
98
+ interface MultiselectConfig<T = string> extends PromptConfig {
99
+ /** Available choices */
100
+ readonly choices: ReadonlyArray<Choice<T>>;
101
+ /** Indices of initially selected choices */
102
+ readonly initial?: ReadonlyArray<number>;
103
+ /** Maximum number of visible choices (enables scrolling) */
104
+ readonly maxVisible?: number;
105
+ /** Minimum required selections */
106
+ readonly min?: number;
107
+ /** Maximum allowed selections */
108
+ readonly max?: number;
109
+ /** Enable type-to-filter */
110
+ readonly searchable?: boolean;
111
+ }
112
+
113
+ /**
114
+ * Prompts for yes/no confirmation.
115
+ *
116
+ * Pure functional prompt that asks a yes/no question and returns a boolean.
117
+ * Supports default values and responds to y/Y/n/N keys. A pasted
118
+ * `y`/`yes`/`n`/`no` (trimmed, case-insensitive) is accepted; any other
119
+ * paste is ignored. The prompt repaints on terminal resize.
120
+ *
121
+ * @param config - Confirm prompt configuration
122
+ * @returns Promise resolving to boolean value or cancellation
123
+ *
124
+ * @example Basic confirmation
125
+ * ```typescript
126
+ * const outcome = await confirm({ message: 'Continue?' })
127
+ * if (outcome.result === 'submitted' && outcome.value) {
128
+ * console.log('Proceeding...')
129
+ * }
130
+ * ```
131
+ *
132
+ * @example With default value
133
+ * ```typescript
134
+ * const outcome = await confirm({
135
+ * message: 'Enable feature?',
136
+ * initial: true, // Default to yes
137
+ * })
138
+ * ```
139
+ */
140
+ declare function confirm(config: ConfirmConfig): Promise<PromptOutcome<boolean>>;
141
+
142
+ /**
143
+ * Prompts for multiple selections from a list of choices.
144
+ *
145
+ * Pure functional prompt with arrow key navigation, space to toggle,
146
+ * scrolling support, min/max constraints, and optional type-to-filter
147
+ * search. In searchable mode, pasted text appends its first line to the
148
+ * filter query; pasting never toggles or submits. The prompt repaints on
149
+ * terminal resize, preserving cursor, selection, and scroll state.
150
+ *
151
+ * @param config - Multiselect prompt configuration
152
+ * @returns Promise resolving to array of selected values or cancellation
153
+ *
154
+ * @example Basic multiselect
155
+ * ```typescript
156
+ * const outcome = await multiselect({
157
+ * message: 'Select toppings:',
158
+ * choices: [
159
+ * { label: 'Cheese', value: 'cheese' },
160
+ * { label: 'Pepperoni', value: 'pepperoni' },
161
+ * { label: 'Mushrooms', value: 'mushrooms' },
162
+ * ],
163
+ * })
164
+ * if (outcome.result === 'submitted') {
165
+ * console.log(`You selected: ${outcome.value.join(', ')}`)
166
+ * }
167
+ * ```
168
+ *
169
+ * @example With search and constraints
170
+ * ```typescript
171
+ * const outcome = await multiselect({
172
+ * message: 'Select features:',
173
+ * choices: features.map((f) => ({ label: f.name, value: f.id })),
174
+ * searchable: true,
175
+ * min: 1,
176
+ * max: 5,
177
+ * })
178
+ * ```
179
+ *
180
+ * @example Pre-selected values
181
+ * ```typescript
182
+ * const outcome = await multiselect({
183
+ * message: 'Select permissions:',
184
+ * choices: permissions,
185
+ * initial: [0, 2], // First and third choices pre-selected
186
+ * })
187
+ * ```
188
+ */
189
+ declare function multiselect<T = string>(config: MultiselectConfig<T>): Promise<PromptOutcome<ReadonlyArray<T>>>;
190
+
191
+ /**
192
+ * Prompts for single selection from a list of choices.
193
+ *
194
+ * Pure functional prompt with arrow key navigation, scrolling support,
195
+ * optional disabled choices, and optional type-to-filter search. In
196
+ * searchable mode, pasted text appends its first line to the filter query.
197
+ * The prompt repaints on terminal resize, preserving cursor and scroll
198
+ * state.
199
+ *
200
+ * @param config - Select prompt configuration
201
+ * @returns Promise resolving to selected value or cancellation
202
+ *
203
+ * @example Basic select
204
+ * ```typescript
205
+ * const outcome = await select({
206
+ * message: 'Choose a color:',
207
+ * choices: [
208
+ * { label: 'Red', value: 'red' },
209
+ * { label: 'Green', value: 'green' },
210
+ * { label: 'Blue', value: 'blue' },
211
+ * ],
212
+ * })
213
+ * if (outcome.result === 'submitted') {
214
+ * console.log(`You chose: ${outcome.value}`)
215
+ * }
216
+ * ```
217
+ *
218
+ * @example With hints and disabled options
219
+ * ```typescript
220
+ * const outcome = await select({
221
+ * message: 'Select plan:',
222
+ * choices: [
223
+ * { label: 'Free', value: 'free', hint: '$0/month' },
224
+ * { label: 'Pro', value: 'pro', hint: '$10/month' },
225
+ * { label: 'Enterprise', value: 'enterprise', disabled: true },
226
+ * ],
227
+ * initial: 1, // Start on Pro
228
+ * })
229
+ * ```
230
+ *
231
+ * @example With search
232
+ * ```typescript
233
+ * const outcome = await select({
234
+ * message: 'Pick a project:',
235
+ * choices: projects.map((p) => ({ label: p.name, value: p.id })),
236
+ * searchable: true,
237
+ * })
238
+ * ```
239
+ */
240
+ declare function select<T = string>(config: SelectConfig<T>): Promise<PromptOutcome<T>>;
241
+
242
+ /**
243
+ * Prompts for text input with optional validation.
244
+ *
245
+ * Pure functional prompt that reads text from the user with support for
246
+ * default values, input validation, and display formatting. Pasted text is
247
+ * sanitized (newlines collapse to spaces, control characters are removed)
248
+ * and inserted at the cursor without ever auto-submitting. The prompt
249
+ * repaints on terminal resize, preserving value, cursor, and any
250
+ * validation error.
251
+ *
252
+ * @param config - Text prompt configuration
253
+ * @returns Promise resolving to submitted value or cancellation
254
+ *
255
+ * @example Basic text input
256
+ * ```typescript
257
+ * const outcome = await text({ message: 'What is your name?' })
258
+ * if (outcome.result === 'submitted') {
259
+ * console.log(`Hello, ${outcome.value}!`)
260
+ * }
261
+ * ```
262
+ *
263
+ * @example With validation
264
+ * ```typescript
265
+ * const outcome = await text({
266
+ * message: 'Enter email:',
267
+ * validate: (value) => {
268
+ * if (!value.includes('@')) return 'Must be a valid email'
269
+ * return undefined
270
+ * },
271
+ * })
272
+ * ```
273
+ *
274
+ * @example Password input with masking
275
+ * ```typescript
276
+ * const outcome = await text({
277
+ * message: 'Password:',
278
+ * format: (value) => '*'.repeat(value.length),
279
+ * })
280
+ * ```
281
+ */
282
+ declare function text(config: TextConfig): Promise<PromptOutcome<string>>;
283
+
284
+ /**
285
+ * Style functions for ANSI text formatting.
286
+ */
287
+ declare const style: Readonly<{
288
+ /**
289
+ * Applies bold styling to text.
290
+ *
291
+ * @param text - Text to style
292
+ * @returns Text wrapped in bold ANSI codes
293
+ */
294
+ bold: (text: string) => string;
295
+ /**
296
+ * Applies dim styling to text.
297
+ *
298
+ * @param text - Text to style
299
+ * @returns Text wrapped in dim ANSI codes
300
+ */
301
+ dim: (text: string) => string;
302
+ /**
303
+ * Applies cyan color to text.
304
+ *
305
+ * @param text - Text to style
306
+ * @returns Text wrapped in cyan ANSI codes
307
+ */
308
+ cyan: (text: string) => string;
309
+ /**
310
+ * Applies green color to text.
311
+ *
312
+ * @param text - Text to style
313
+ * @returns Text wrapped in green ANSI codes
314
+ */
315
+ green: (text: string) => string;
316
+ /**
317
+ * Applies yellow color to text.
318
+ *
319
+ * @param text - Text to style
320
+ * @returns Text wrapped in yellow ANSI codes
321
+ */
322
+ yellow: (text: string) => string;
323
+ /**
324
+ * Applies red color to text.
325
+ *
326
+ * @param text - Text to style
327
+ * @returns Text wrapped in red ANSI codes
328
+ */
329
+ red: (text: string) => string;
330
+ /**
331
+ * Applies gray color to text.
332
+ *
333
+ * @param text - Text to style
334
+ * @returns Text wrapped in gray ANSI codes
335
+ */
336
+ gray: (text: string) => string;
337
+ }>;
338
+
339
+ export { PromptResult, confirm, multiselect, select, style, text };
340
+ export type { Choice, ConfirmConfig, MultiselectConfig, PromptCancelledOutcome, PromptConfig, PromptFunction, PromptOutcome, PromptSubmittedOutcome, SelectConfig, TextConfig };