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