inquirerer 4.1.2 → 4.2.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.
@@ -0,0 +1,371 @@
1
+ /**
2
+ * Engine-based prompt implementations
3
+ *
4
+ * These are internal implementations of the standard prompts (list, autocomplete, checkbox)
5
+ * using the UIEngine for rendering and event handling.
6
+ */
7
+ import { blue } from 'yanse';
8
+ import { UIEngine } from './engine';
9
+ import { Key } from './types';
10
+ /**
11
+ * Render the prompt header lines
12
+ */
13
+ export function renderPromptHeader(promptMessage, input) {
14
+ const inputLine = `> ${input}`;
15
+ return [promptMessage + inputLine];
16
+ }
17
+ /**
18
+ * Fuzzy match function (matches existing prompt.ts behavior)
19
+ */
20
+ function fuzzyMatch(option, input) {
21
+ if (!input || !input.trim().length)
22
+ return true;
23
+ const length = input.length;
24
+ let position = 0;
25
+ for (let i = 0; i < option.length; i++) {
26
+ if (option[i] === input[position]) {
27
+ position++;
28
+ if (position === length) {
29
+ return true;
30
+ }
31
+ }
32
+ }
33
+ return false;
34
+ }
35
+ /**
36
+ * Filter options based on input (matches existing prompt.ts behavior)
37
+ */
38
+ export function filterOptions(options, input) {
39
+ const normalizedInput = input.toLowerCase();
40
+ return options
41
+ .filter(option => fuzzyMatch(option.name.toLowerCase(), normalizedInput))
42
+ .sort((a, b) => {
43
+ if (a.name < b.name)
44
+ return -1;
45
+ if (a.name > b.name)
46
+ return 1;
47
+ return 0;
48
+ });
49
+ }
50
+ function listRender(state) {
51
+ const lines = [];
52
+ // Header
53
+ lines.push(state.promptMessage);
54
+ // Options
55
+ const endIndex = Math.min(state.startIndex + state.maxLines, state.options.length);
56
+ for (let i = state.startIndex; i < endIndex; i++) {
57
+ const option = state.options[i];
58
+ if (!option) {
59
+ lines.push('No options');
60
+ }
61
+ else if (i === state.selectedIndex) {
62
+ lines.push(blue('> ' + option.name));
63
+ }
64
+ else {
65
+ lines.push(' ' + option.name);
66
+ }
67
+ }
68
+ return lines;
69
+ }
70
+ function listOnEvent(event, state) {
71
+ const { options, maxLines } = state;
72
+ let { selectedIndex, startIndex } = state;
73
+ if (event.type === 'key') {
74
+ switch (event.key) {
75
+ case Key.UP:
76
+ selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
77
+ if (selectedIndex < startIndex) {
78
+ startIndex = selectedIndex;
79
+ }
80
+ else if (selectedIndex === options.length - 1) {
81
+ startIndex = Math.max(0, options.length - maxLines);
82
+ }
83
+ break;
84
+ case Key.DOWN:
85
+ selectedIndex = (selectedIndex + 1) % options.length;
86
+ if (selectedIndex >= startIndex + maxLines) {
87
+ startIndex = selectedIndex - maxLines + 1;
88
+ }
89
+ else if (selectedIndex === 0) {
90
+ startIndex = 0;
91
+ }
92
+ break;
93
+ case Key.ENTER:
94
+ return {
95
+ state: { ...state, selectedIndex, startIndex },
96
+ done: true,
97
+ value: options[selectedIndex]?.value
98
+ };
99
+ }
100
+ }
101
+ return { state: { ...state, selectedIndex, startIndex } };
102
+ }
103
+ export async function listPromptEngine(config) {
104
+ const engine = new UIEngine({
105
+ keypress: config.keypress,
106
+ ownsKeypress: false,
107
+ input: config.input,
108
+ output: config.output,
109
+ noTty: config.noTty,
110
+ clearScreenOnStart: true,
111
+ });
112
+ const initialState = {
113
+ options: config.options,
114
+ selectedIndex: 0,
115
+ startIndex: 0,
116
+ maxLines: config.maxLines,
117
+ promptMessage: config.promptMessage,
118
+ };
119
+ return engine.run({
120
+ initialState,
121
+ render: listRender,
122
+ onEvent: listOnEvent,
123
+ });
124
+ }
125
+ function autocompleteRender(state) {
126
+ const lines = [];
127
+ // Header with input
128
+ lines.push(state.promptMessage + `> ${state.input}`);
129
+ // Filtered options
130
+ const endIndex = Math.min(state.startIndex + state.maxLines, state.filteredOptions.length);
131
+ for (let i = state.startIndex; i < endIndex; i++) {
132
+ const option = state.filteredOptions[i];
133
+ if (!option) {
134
+ lines.push('No options');
135
+ }
136
+ else if (i === state.selectedIndex) {
137
+ lines.push(blue('> ' + option.name));
138
+ }
139
+ else {
140
+ lines.push(' ' + option.name);
141
+ }
142
+ }
143
+ return lines;
144
+ }
145
+ function autocompleteOnEvent(event, state) {
146
+ const { options, maxLines } = state;
147
+ let { input, filteredOptions, selectedIndex, startIndex } = state;
148
+ const updateFiltered = () => {
149
+ filteredOptions = filterOptions(options, input);
150
+ if (selectedIndex < startIndex) {
151
+ startIndex = selectedIndex;
152
+ }
153
+ else if (selectedIndex >= startIndex + maxLines) {
154
+ startIndex = selectedIndex - maxLines + 1;
155
+ }
156
+ if (selectedIndex >= filteredOptions.length) {
157
+ selectedIndex = Math.max(filteredOptions.length - 1, 0);
158
+ }
159
+ };
160
+ if (event.type === 'key') {
161
+ switch (event.key) {
162
+ case Key.UP:
163
+ if (filteredOptions.length === 0)
164
+ break;
165
+ selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : filteredOptions.length - 1;
166
+ if (selectedIndex < startIndex) {
167
+ startIndex = selectedIndex;
168
+ }
169
+ else if (selectedIndex === filteredOptions.length - 1) {
170
+ startIndex = Math.max(0, filteredOptions.length - maxLines);
171
+ }
172
+ break;
173
+ case Key.DOWN:
174
+ if (filteredOptions.length === 0)
175
+ break;
176
+ selectedIndex = (selectedIndex + 1) % filteredOptions.length;
177
+ if (selectedIndex >= startIndex + maxLines) {
178
+ startIndex = selectedIndex - maxLines + 1;
179
+ }
180
+ else if (selectedIndex === 0) {
181
+ startIndex = 0;
182
+ }
183
+ break;
184
+ case Key.BACKSPACE:
185
+ input = input.slice(0, -1);
186
+ updateFiltered();
187
+ break;
188
+ case Key.SPACE:
189
+ // In autocomplete, space is part of input (unlike checkbox)
190
+ input += ' ';
191
+ updateFiltered();
192
+ break;
193
+ case Key.ENTER:
194
+ return {
195
+ state: { ...state, input, filteredOptions, selectedIndex, startIndex },
196
+ done: true,
197
+ value: filteredOptions[selectedIndex]?.value || input
198
+ };
199
+ }
200
+ }
201
+ else if (event.type === 'char') {
202
+ input += event.char;
203
+ updateFiltered();
204
+ }
205
+ return { state: { ...state, input, filteredOptions, selectedIndex, startIndex } };
206
+ }
207
+ export async function autocompletePromptEngine(config) {
208
+ const engine = new UIEngine({
209
+ keypress: config.keypress,
210
+ ownsKeypress: false,
211
+ input: config.input,
212
+ output: config.output,
213
+ noTty: config.noTty,
214
+ clearScreenOnStart: true,
215
+ });
216
+ const initialState = {
217
+ options: config.options,
218
+ filteredOptions: config.options,
219
+ input: '',
220
+ selectedIndex: 0,
221
+ startIndex: 0,
222
+ maxLines: config.maxLines,
223
+ promptMessage: config.promptMessage,
224
+ };
225
+ return engine.run({
226
+ initialState,
227
+ render: autocompleteRender,
228
+ onEvent: autocompleteOnEvent,
229
+ });
230
+ }
231
+ function checkboxRender(state) {
232
+ const lines = [];
233
+ // Header with input
234
+ lines.push(state.promptMessage + `> ${state.input}`);
235
+ // Filtered options with checkboxes
236
+ const endIndex = Math.min(state.startIndex + state.maxLines, state.filteredOptions.length);
237
+ for (let i = state.startIndex; i < endIndex; i++) {
238
+ const option = state.filteredOptions[i];
239
+ const isSelected = state.selectedIndex === i;
240
+ const marker = isSelected ? '>' : ' ';
241
+ // Find original index to get selection state
242
+ const originalIndex = state.options.findIndex(o => o.name === option.name);
243
+ if (originalIndex >= 0) {
244
+ const isChecked = state.selections[originalIndex] ? '◉' : '○';
245
+ const line = `${marker} ${isChecked} ${option.name}`;
246
+ lines.push(isSelected ? blue(line) : line);
247
+ }
248
+ else {
249
+ lines.push('No options');
250
+ }
251
+ }
252
+ return lines;
253
+ }
254
+ function checkboxOnEvent(event, state) {
255
+ const { options, maxLines, returnFullResults } = state;
256
+ let { input, filteredOptions, selections, selectedIndex, startIndex } = state;
257
+ const updateFiltered = () => {
258
+ filteredOptions = filterOptions(options, input);
259
+ // Clamp selectedIndex and startIndex when filtered results change
260
+ if (filteredOptions.length === 0) {
261
+ selectedIndex = 0;
262
+ startIndex = 0;
263
+ }
264
+ else {
265
+ if (selectedIndex >= filteredOptions.length) {
266
+ selectedIndex = filteredOptions.length - 1;
267
+ }
268
+ if (startIndex > Math.max(0, filteredOptions.length - maxLines)) {
269
+ startIndex = Math.max(0, filteredOptions.length - maxLines);
270
+ }
271
+ }
272
+ };
273
+ if (event.type === 'key') {
274
+ switch (event.key) {
275
+ case Key.UP:
276
+ if (filteredOptions.length === 0)
277
+ break;
278
+ selectedIndex = selectedIndex > 0 ? selectedIndex - 1 : filteredOptions.length - 1;
279
+ if (selectedIndex < startIndex) {
280
+ startIndex = selectedIndex;
281
+ }
282
+ else if (selectedIndex === filteredOptions.length - 1) {
283
+ startIndex = Math.max(0, filteredOptions.length - maxLines);
284
+ }
285
+ break;
286
+ case Key.DOWN:
287
+ if (filteredOptions.length === 0)
288
+ break;
289
+ selectedIndex = (selectedIndex + 1) % filteredOptions.length;
290
+ if (selectedIndex >= startIndex + maxLines) {
291
+ startIndex = selectedIndex - maxLines + 1;
292
+ }
293
+ else if (selectedIndex === 0) {
294
+ startIndex = 0;
295
+ }
296
+ break;
297
+ case Key.BACKSPACE:
298
+ input = input.slice(0, -1);
299
+ updateFiltered();
300
+ break;
301
+ case Key.SPACE:
302
+ // Toggle selection
303
+ if (filteredOptions.length > 0 && filteredOptions[selectedIndex]) {
304
+ const originalIndex = options.indexOf(filteredOptions[selectedIndex]);
305
+ if (originalIndex >= 0) {
306
+ selections = [...selections];
307
+ selections[originalIndex] = !selections[originalIndex];
308
+ }
309
+ }
310
+ break;
311
+ case Key.ENTER:
312
+ const result = [];
313
+ if (returnFullResults) {
314
+ options.forEach((option, index) => {
315
+ result.push({
316
+ name: option.name,
317
+ value: option.value,
318
+ selected: selections[index]
319
+ });
320
+ });
321
+ }
322
+ else {
323
+ options.forEach((option, index) => {
324
+ if (selections[index]) {
325
+ result.push({
326
+ name: option.name,
327
+ value: option.value,
328
+ selected: true
329
+ });
330
+ }
331
+ });
332
+ }
333
+ return {
334
+ state: { ...state, input, filteredOptions, selections, selectedIndex, startIndex },
335
+ done: true,
336
+ value: result
337
+ };
338
+ }
339
+ }
340
+ else if (event.type === 'char') {
341
+ input += event.char;
342
+ updateFiltered();
343
+ }
344
+ return { state: { ...state, input, filteredOptions, selections, selectedIndex, startIndex } };
345
+ }
346
+ export async function checkboxPromptEngine(config) {
347
+ const engine = new UIEngine({
348
+ keypress: config.keypress,
349
+ ownsKeypress: false,
350
+ input: config.input,
351
+ output: config.output,
352
+ noTty: config.noTty,
353
+ clearScreenOnStart: true,
354
+ });
355
+ const initialState = {
356
+ options: config.options,
357
+ filteredOptions: config.options,
358
+ selections: config.defaultSelections,
359
+ input: '',
360
+ selectedIndex: 0,
361
+ startIndex: 0,
362
+ maxLines: config.maxLines,
363
+ promptMessage: config.promptMessage,
364
+ returnFullResults: config.returnFullResults,
365
+ };
366
+ return engine.run({
367
+ initialState,
368
+ render: checkboxRender,
369
+ onEvent: checkboxOnEvent,
370
+ });
371
+ }
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Spinner Component
3
+ *
4
+ * Animated loading spinner for async operations
5
+ */
6
+ import { UIEngine } from './engine';
7
+ import { green, red, yellow, cyan } from 'yanse';
8
+ /**
9
+ * Default spinner frames (dots style)
10
+ */
11
+ const DEFAULT_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
12
+ /**
13
+ * Alternative spinner styles
14
+ */
15
+ export const SPINNER_STYLES = {
16
+ dots: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
17
+ line: ['-', '\\', '|', '/'],
18
+ arc: ['◜', '◠', '◝', '◞', '◡', '◟'],
19
+ circle: ['◐', '◓', '◑', '◒'],
20
+ square: ['◰', '◳', '◲', '◱'],
21
+ bounce: ['⠁', '⠂', '⠄', '⠂'],
22
+ arrow: ['←', '↖', '↑', '↗', '→', '↘', '↓', '↙'],
23
+ clock: ['🕐', '🕑', '🕒', '🕓', '🕔', '🕕', '🕖', '🕗', '🕘', '🕙', '🕚', '🕛'],
24
+ moon: ['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'],
25
+ dots2: ['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'],
26
+ };
27
+ /**
28
+ * Create a spinner that can be controlled externally
29
+ */
30
+ export class Spinner {
31
+ engine;
32
+ frames;
33
+ interval;
34
+ state;
35
+ tickTimer = null;
36
+ output;
37
+ lastLineCount = 0;
38
+ isRunning = false;
39
+ constructor(config, output = process.stdout) {
40
+ this.engine = new UIEngine({ output });
41
+ this.frames = config.frames ?? DEFAULT_FRAMES;
42
+ this.interval = config.interval ?? 80;
43
+ this.output = output;
44
+ this.state = {
45
+ frame: 0,
46
+ text: config.text,
47
+ status: 'spinning',
48
+ };
49
+ }
50
+ /**
51
+ * Start the spinner
52
+ */
53
+ start() {
54
+ if (this.isRunning)
55
+ return this;
56
+ this.isRunning = true;
57
+ // Hide cursor
58
+ this.output.write('\x1B[?25l');
59
+ // Initial render
60
+ this.render();
61
+ // Start animation
62
+ this.tickTimer = setInterval(() => {
63
+ this.state.frame = (this.state.frame + 1) % this.frames.length;
64
+ this.render();
65
+ }, this.interval);
66
+ return this;
67
+ }
68
+ /**
69
+ * Update the spinner text
70
+ */
71
+ text(text) {
72
+ this.state.text = text;
73
+ if (this.isRunning) {
74
+ this.render();
75
+ }
76
+ return this;
77
+ }
78
+ /**
79
+ * Stop with success
80
+ */
81
+ succeed(text) {
82
+ return this.stop('success', text);
83
+ }
84
+ /**
85
+ * Stop with error
86
+ */
87
+ fail(text) {
88
+ return this.stop('error', text);
89
+ }
90
+ /**
91
+ * Stop with warning
92
+ */
93
+ warn(text) {
94
+ return this.stop('warning', text);
95
+ }
96
+ /**
97
+ * Stop with info
98
+ */
99
+ info(text) {
100
+ return this.stop('info', text);
101
+ }
102
+ /**
103
+ * Stop the spinner
104
+ */
105
+ stop(status = 'success', text) {
106
+ if (!this.isRunning)
107
+ return this;
108
+ // Clear timer
109
+ if (this.tickTimer) {
110
+ clearInterval(this.tickTimer);
111
+ this.tickTimer = null;
112
+ }
113
+ this.state.status = status;
114
+ if (text) {
115
+ this.state.finalText = text;
116
+ }
117
+ // Final render
118
+ this.render();
119
+ // Show cursor and newline
120
+ this.output.write('\x1B[?25h\n');
121
+ this.isRunning = false;
122
+ return this;
123
+ }
124
+ /**
125
+ * Render the current state
126
+ */
127
+ render() {
128
+ const { frame, text, status, finalText } = this.state;
129
+ let icon;
130
+ let displayText = finalText ?? text;
131
+ switch (status) {
132
+ case 'success':
133
+ icon = green('✔');
134
+ displayText = green(displayText);
135
+ break;
136
+ case 'error':
137
+ icon = red('✖');
138
+ displayText = red(displayText);
139
+ break;
140
+ case 'warning':
141
+ icon = yellow('⚠');
142
+ displayText = yellow(displayText);
143
+ break;
144
+ case 'info':
145
+ icon = cyan('ℹ');
146
+ displayText = cyan(displayText);
147
+ break;
148
+ default:
149
+ icon = cyan(this.frames[frame]);
150
+ }
151
+ // Clear line and write
152
+ this.output.write('\r\x1B[2K' + icon + ' ' + displayText);
153
+ }
154
+ }
155
+ /**
156
+ * Create and start a spinner
157
+ */
158
+ export function createSpinner(text, options) {
159
+ return new Spinner({ text, ...options });
160
+ }
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Streaming Text Component
3
+ *
4
+ * For displaying streaming output like AI chat responses
5
+ */
6
+ import { cyan } from 'yanse';
7
+ /**
8
+ * Streaming text display for AI-style output
9
+ */
10
+ export class StreamingText {
11
+ prefix;
12
+ showCursor;
13
+ state;
14
+ output;
15
+ isRunning = false;
16
+ cursorTimer = null;
17
+ cursorVisible = true;
18
+ constructor(config = {}, output = process.stdout) {
19
+ this.prefix = config.prefix ?? '';
20
+ this.showCursor = config.showCursor ?? true;
21
+ this.output = output;
22
+ this.state = {
23
+ lines: [],
24
+ currentLine: '',
25
+ showCursor: this.showCursor,
26
+ isComplete: false,
27
+ };
28
+ }
29
+ /**
30
+ * Start the stream display
31
+ */
32
+ start() {
33
+ if (this.isRunning)
34
+ return this;
35
+ this.isRunning = true;
36
+ // Hide terminal cursor
37
+ this.output.write('\x1B[?25l');
38
+ // Start cursor blink if enabled
39
+ if (this.showCursor) {
40
+ this.cursorTimer = setInterval(() => {
41
+ this.cursorVisible = !this.cursorVisible;
42
+ this.render();
43
+ }, 530);
44
+ }
45
+ // Initial render
46
+ this.render();
47
+ return this;
48
+ }
49
+ /**
50
+ * Append text to the stream
51
+ */
52
+ append(text) {
53
+ for (const char of text) {
54
+ if (char === '\n') {
55
+ this.state.lines.push(this.state.currentLine);
56
+ this.state.currentLine = '';
57
+ }
58
+ else {
59
+ this.state.currentLine += char;
60
+ }
61
+ }
62
+ if (this.isRunning) {
63
+ this.render();
64
+ }
65
+ return this;
66
+ }
67
+ /**
68
+ * Append a complete line
69
+ */
70
+ appendLine(line) {
71
+ if (this.state.currentLine) {
72
+ this.state.lines.push(this.state.currentLine + line);
73
+ this.state.currentLine = '';
74
+ }
75
+ else {
76
+ this.state.lines.push(line);
77
+ }
78
+ if (this.isRunning) {
79
+ this.render();
80
+ }
81
+ return this;
82
+ }
83
+ /**
84
+ * Clear all content
85
+ */
86
+ clear() {
87
+ this.state.lines = [];
88
+ this.state.currentLine = '';
89
+ if (this.isRunning) {
90
+ this.render();
91
+ }
92
+ return this;
93
+ }
94
+ /**
95
+ * Mark stream as complete
96
+ */
97
+ done() {
98
+ this.state.isComplete = true;
99
+ // Stop cursor blink
100
+ if (this.cursorTimer) {
101
+ clearInterval(this.cursorTimer);
102
+ this.cursorTimer = null;
103
+ }
104
+ // Final render without cursor
105
+ this.cursorVisible = false;
106
+ this.render();
107
+ // Show terminal cursor and newline
108
+ this.output.write('\x1B[?25h\n');
109
+ this.isRunning = false;
110
+ return this;
111
+ }
112
+ /**
113
+ * Get the current content
114
+ */
115
+ getContent() {
116
+ const allLines = [...this.state.lines];
117
+ if (this.state.currentLine) {
118
+ allLines.push(this.state.currentLine);
119
+ }
120
+ return allLines.join('\n');
121
+ }
122
+ /**
123
+ * Render the current state
124
+ */
125
+ render() {
126
+ const { lines, currentLine, isComplete } = this.state;
127
+ // Build output
128
+ const allLines = [...lines];
129
+ // Add current line with optional cursor
130
+ let lastLine = currentLine;
131
+ if (this.showCursor && !isComplete && this.cursorVisible) {
132
+ lastLine += cyan('▋');
133
+ }
134
+ else if (this.showCursor && !isComplete) {
135
+ lastLine += ' ';
136
+ }
137
+ if (lastLine || allLines.length === 0) {
138
+ allLines.push(lastLine);
139
+ }
140
+ // Add prefix to each line
141
+ const prefixedLines = allLines.map((line, i) => {
142
+ if (i === 0 && this.prefix) {
143
+ return this.prefix + line;
144
+ }
145
+ return (this.prefix ? ' '.repeat(this.prefix.length) : '') + line;
146
+ });
147
+ // Clear previous output and write new
148
+ // Move cursor to start of output area
149
+ const totalLines = prefixedLines.length;
150
+ // For simplicity, just clear and rewrite
151
+ // In a more sophisticated implementation, we'd track line count
152
+ this.output.write('\r\x1B[2K');
153
+ // Write all lines
154
+ for (let i = 0; i < prefixedLines.length; i++) {
155
+ if (i > 0) {
156
+ this.output.write('\n\x1B[2K');
157
+ }
158
+ this.output.write(prefixedLines[i]);
159
+ }
160
+ // Move cursor back to end of last line
161
+ if (prefixedLines.length > 1) {
162
+ this.output.write(`\x1B[${prefixedLines.length - 1}A`);
163
+ this.output.write(`\x1B[${prefixedLines[0].length}G`);
164
+ }
165
+ }
166
+ }
167
+ /**
168
+ * Create a streaming text display
169
+ */
170
+ export function createStream(options) {
171
+ return new StreamingText(options);
172
+ }