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.
- package/README.md +205 -0
- package/esm/index.js +1 -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/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/esm/ui/types.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event-driven UI Engine Types
|
|
3
|
+
*
|
|
4
|
+
* This module provides types for building custom interactive terminal UIs
|
|
5
|
+
* with support for key events, timers, and async updates.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Normalized key events - abstracts raw escape sequences
|
|
9
|
+
*/
|
|
10
|
+
export var Key;
|
|
11
|
+
(function (Key) {
|
|
12
|
+
Key["UP"] = "UP";
|
|
13
|
+
Key["DOWN"] = "DOWN";
|
|
14
|
+
Key["LEFT"] = "LEFT";
|
|
15
|
+
Key["RIGHT"] = "RIGHT";
|
|
16
|
+
Key["ENTER"] = "ENTER";
|
|
17
|
+
Key["SPACE"] = "SPACE";
|
|
18
|
+
Key["BACKSPACE"] = "BACKSPACE";
|
|
19
|
+
Key["ESCAPE"] = "ESCAPE";
|
|
20
|
+
Key["TAB"] = "TAB";
|
|
21
|
+
Key["CTRL_C"] = "CTRL_C";
|
|
22
|
+
})(Key || (Key = {}));
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive Upgrade UI Component
|
|
3
|
+
*
|
|
4
|
+
* pnpm-style interactive dependency upgrade interface
|
|
5
|
+
*/
|
|
6
|
+
import { UIEngine } from './engine';
|
|
7
|
+
import { Key } from './types';
|
|
8
|
+
import { cyan, green, dim, white } from 'yanse';
|
|
9
|
+
/**
|
|
10
|
+
* Render a single package row
|
|
11
|
+
*/
|
|
12
|
+
function renderPackageRow(pkg, selection, isSelected, isVersionMode, versionIndex, versionOptions) {
|
|
13
|
+
const checkbox = selection.selected ? green('◉') : dim('○');
|
|
14
|
+
const cursor = isSelected ? cyan('❯') : ' ';
|
|
15
|
+
const name = pkg.name.padEnd(30);
|
|
16
|
+
const current = dim(pkg.current.padEnd(12));
|
|
17
|
+
const arrow = dim('→');
|
|
18
|
+
let target;
|
|
19
|
+
if (isSelected && isVersionMode) {
|
|
20
|
+
// Show version selector
|
|
21
|
+
const versions = versionOptions.map((v, i) => i === versionIndex ? cyan(`[${v}]`) : dim(v)).join(' ');
|
|
22
|
+
target = versions;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
target = selection.selected
|
|
26
|
+
? green(selection.targetVersion.padEnd(12))
|
|
27
|
+
: dim(selection.targetVersion.padEnd(12));
|
|
28
|
+
}
|
|
29
|
+
const type = dim(`(${pkg.type.replace('Dependencies', '')})`);
|
|
30
|
+
return `${cursor} ${checkbox} ${name} ${current} ${arrow} ${target} ${type}`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create the upgrade UI
|
|
34
|
+
*/
|
|
35
|
+
export async function interactiveUpgrade(engine, config) {
|
|
36
|
+
const { packages, maxLines = 10 } = config;
|
|
37
|
+
// Initialize selections
|
|
38
|
+
const selections = new Map();
|
|
39
|
+
packages.forEach(pkg => {
|
|
40
|
+
selections.set(pkg.name, {
|
|
41
|
+
selected: false,
|
|
42
|
+
targetVersion: pkg.latest,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
const initialState = {
|
|
46
|
+
packages,
|
|
47
|
+
selections,
|
|
48
|
+
selectedIndex: 0,
|
|
49
|
+
startIndex: 0,
|
|
50
|
+
maxLines,
|
|
51
|
+
filter: '',
|
|
52
|
+
mode: 'select',
|
|
53
|
+
versionOptions: [],
|
|
54
|
+
versionIndex: 0,
|
|
55
|
+
};
|
|
56
|
+
const result = await engine.run({
|
|
57
|
+
initialState,
|
|
58
|
+
hideCursor: true,
|
|
59
|
+
render: (state) => {
|
|
60
|
+
const lines = [];
|
|
61
|
+
// Header
|
|
62
|
+
lines.push(white('Interactive Dependency Upgrade'));
|
|
63
|
+
lines.push(dim('Use ↑↓ to navigate, SPACE to select, → to change version, ENTER to confirm'));
|
|
64
|
+
lines.push('');
|
|
65
|
+
// Column headers
|
|
66
|
+
const header = ` ${dim(' Package'.padEnd(32))} ${dim('Current'.padEnd(12))} ${dim('Target'.padEnd(12))} ${dim('Type')}`;
|
|
67
|
+
lines.push(header);
|
|
68
|
+
lines.push(dim('─'.repeat(80)));
|
|
69
|
+
// Filter packages if filter is set
|
|
70
|
+
let filteredPackages = state.packages;
|
|
71
|
+
if (state.filter) {
|
|
72
|
+
filteredPackages = state.packages.filter(p => p.name.toLowerCase().includes(state.filter.toLowerCase()));
|
|
73
|
+
}
|
|
74
|
+
// Calculate visible range
|
|
75
|
+
const endIndex = Math.min(state.startIndex + state.maxLines, filteredPackages.length);
|
|
76
|
+
// Render visible packages
|
|
77
|
+
for (let i = state.startIndex; i < endIndex; i++) {
|
|
78
|
+
const pkg = filteredPackages[i];
|
|
79
|
+
const selection = state.selections.get(pkg.name);
|
|
80
|
+
const isSelected = i === state.selectedIndex;
|
|
81
|
+
const isVersionMode = state.mode === 'version' && isSelected;
|
|
82
|
+
lines.push(renderPackageRow(pkg, selection, isSelected, isVersionMode, state.versionIndex, state.versionOptions));
|
|
83
|
+
}
|
|
84
|
+
// Padding if fewer packages than maxLines
|
|
85
|
+
for (let i = filteredPackages.length; i < state.maxLines; i++) {
|
|
86
|
+
lines.push('');
|
|
87
|
+
}
|
|
88
|
+
lines.push(dim('─'.repeat(80)));
|
|
89
|
+
// Summary
|
|
90
|
+
const selectedCount = Array.from(state.selections.values()).filter(s => s.selected).length;
|
|
91
|
+
lines.push(`${cyan(selectedCount.toString())} packages selected for upgrade`);
|
|
92
|
+
// Filter indicator
|
|
93
|
+
if (state.filter) {
|
|
94
|
+
lines.push(dim(`Filter: ${state.filter}`));
|
|
95
|
+
}
|
|
96
|
+
return lines;
|
|
97
|
+
},
|
|
98
|
+
onEvent: (event, state) => {
|
|
99
|
+
// Get filtered packages for navigation
|
|
100
|
+
let filteredPackages = state.packages;
|
|
101
|
+
if (state.filter) {
|
|
102
|
+
filteredPackages = state.packages.filter(p => p.name.toLowerCase().includes(state.filter.toLowerCase()));
|
|
103
|
+
}
|
|
104
|
+
if (event.type === 'key') {
|
|
105
|
+
switch (event.key) {
|
|
106
|
+
case Key.UP: {
|
|
107
|
+
if (filteredPackages.length === 0)
|
|
108
|
+
return { state };
|
|
109
|
+
let newIndex = state.selectedIndex - 1;
|
|
110
|
+
let newStartIndex = state.startIndex;
|
|
111
|
+
if (newIndex < 0) {
|
|
112
|
+
newIndex = filteredPackages.length - 1;
|
|
113
|
+
newStartIndex = Math.max(0, filteredPackages.length - state.maxLines);
|
|
114
|
+
}
|
|
115
|
+
else if (newIndex < state.startIndex) {
|
|
116
|
+
newStartIndex = newIndex;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
state: {
|
|
120
|
+
...state,
|
|
121
|
+
selectedIndex: newIndex,
|
|
122
|
+
startIndex: newStartIndex,
|
|
123
|
+
mode: 'select',
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
case Key.DOWN: {
|
|
128
|
+
if (filteredPackages.length === 0)
|
|
129
|
+
return { state };
|
|
130
|
+
let newIndex = (state.selectedIndex + 1) % filteredPackages.length;
|
|
131
|
+
let newStartIndex = state.startIndex;
|
|
132
|
+
if (newIndex === 0) {
|
|
133
|
+
newStartIndex = 0;
|
|
134
|
+
}
|
|
135
|
+
else if (newIndex >= state.startIndex + state.maxLines) {
|
|
136
|
+
newStartIndex = newIndex - state.maxLines + 1;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
state: {
|
|
140
|
+
...state,
|
|
141
|
+
selectedIndex: newIndex,
|
|
142
|
+
startIndex: newStartIndex,
|
|
143
|
+
mode: 'select',
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
case Key.SPACE: {
|
|
148
|
+
if (filteredPackages.length === 0)
|
|
149
|
+
return { state };
|
|
150
|
+
const pkg = filteredPackages[state.selectedIndex];
|
|
151
|
+
const newSelections = new Map(state.selections);
|
|
152
|
+
const current = newSelections.get(pkg.name);
|
|
153
|
+
newSelections.set(pkg.name, {
|
|
154
|
+
...current,
|
|
155
|
+
selected: !current.selected,
|
|
156
|
+
});
|
|
157
|
+
return {
|
|
158
|
+
state: {
|
|
159
|
+
...state,
|
|
160
|
+
selections: newSelections,
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
case Key.RIGHT: {
|
|
165
|
+
if (filteredPackages.length === 0)
|
|
166
|
+
return { state };
|
|
167
|
+
const pkg = filteredPackages[state.selectedIndex];
|
|
168
|
+
// Generate version options (simplified - in real use, these would come from npm)
|
|
169
|
+
const versionOptions = [
|
|
170
|
+
pkg.latest,
|
|
171
|
+
`^${pkg.latest}`,
|
|
172
|
+
`~${pkg.latest}`,
|
|
173
|
+
pkg.current,
|
|
174
|
+
];
|
|
175
|
+
return {
|
|
176
|
+
state: {
|
|
177
|
+
...state,
|
|
178
|
+
mode: 'version',
|
|
179
|
+
versionOptions,
|
|
180
|
+
versionIndex: 0,
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
case Key.LEFT: {
|
|
185
|
+
if (state.mode === 'version') {
|
|
186
|
+
const newIndex = (state.versionIndex - 1 + state.versionOptions.length) % state.versionOptions.length;
|
|
187
|
+
return {
|
|
188
|
+
state: {
|
|
189
|
+
...state,
|
|
190
|
+
versionIndex: newIndex,
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return { state };
|
|
195
|
+
}
|
|
196
|
+
case Key.ENTER: {
|
|
197
|
+
if (state.mode === 'version') {
|
|
198
|
+
// Apply selected version
|
|
199
|
+
const pkg = filteredPackages[state.selectedIndex];
|
|
200
|
+
const newSelections = new Map(state.selections);
|
|
201
|
+
const current = newSelections.get(pkg.name);
|
|
202
|
+
newSelections.set(pkg.name, {
|
|
203
|
+
...current,
|
|
204
|
+
selected: true,
|
|
205
|
+
targetVersion: state.versionOptions[state.versionIndex],
|
|
206
|
+
});
|
|
207
|
+
return {
|
|
208
|
+
state: {
|
|
209
|
+
...state,
|
|
210
|
+
selections: newSelections,
|
|
211
|
+
mode: 'select',
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
// Confirm and return results
|
|
216
|
+
const updates = [];
|
|
217
|
+
state.selections.forEach((selection, name) => {
|
|
218
|
+
if (selection.selected) {
|
|
219
|
+
const pkg = state.packages.find(p => p.name === name);
|
|
220
|
+
updates.push({
|
|
221
|
+
name,
|
|
222
|
+
from: pkg.current,
|
|
223
|
+
to: selection.targetVersion,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return {
|
|
228
|
+
state,
|
|
229
|
+
done: true,
|
|
230
|
+
value: { updates },
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
case Key.ESCAPE: {
|
|
234
|
+
if (state.mode === 'version') {
|
|
235
|
+
return {
|
|
236
|
+
state: {
|
|
237
|
+
...state,
|
|
238
|
+
mode: 'select',
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
// Cancel
|
|
243
|
+
return {
|
|
244
|
+
state,
|
|
245
|
+
done: true,
|
|
246
|
+
value: { updates: [] },
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
case Key.BACKSPACE: {
|
|
250
|
+
if (state.filter.length > 0) {
|
|
251
|
+
return {
|
|
252
|
+
state: {
|
|
253
|
+
...state,
|
|
254
|
+
filter: state.filter.slice(0, -1),
|
|
255
|
+
selectedIndex: 0,
|
|
256
|
+
startIndex: 0,
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return { state };
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// Handle character input for filtering
|
|
265
|
+
if (event.type === 'char') {
|
|
266
|
+
return {
|
|
267
|
+
state: {
|
|
268
|
+
...state,
|
|
269
|
+
filter: state.filter + event.char,
|
|
270
|
+
selectedIndex: 0,
|
|
271
|
+
startIndex: 0,
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
return { state };
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
return result ?? { updates: [] };
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Standalone upgrade prompt
|
|
282
|
+
*/
|
|
283
|
+
export async function upgradePrompt(packages, maxLines) {
|
|
284
|
+
const engine = new UIEngine();
|
|
285
|
+
try {
|
|
286
|
+
return await interactiveUpgrade(engine, { packages, maxLines });
|
|
287
|
+
}
|
|
288
|
+
finally {
|
|
289
|
+
engine.destroy();
|
|
290
|
+
}
|
|
291
|
+
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
|
|
6
6
|
"main": "index.js",
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"prepublishOnly": "npm run build",
|
|
26
26
|
"build": "makage build",
|
|
27
27
|
"dev": "ts-node dev/index",
|
|
28
|
+
"dev:spinner": "ts-node dev/demo-spinner",
|
|
29
|
+
"dev:chat": "ts-node dev/demo-chat",
|
|
30
|
+
"dev:upgrade": "ts-node dev/demo-upgrade",
|
|
31
|
+
"dev:prompts": "ts-node dev/demo-prompts-engine",
|
|
28
32
|
"test": "jest",
|
|
29
33
|
"test:watch": "jest --watch"
|
|
30
34
|
},
|
|
@@ -41,5 +45,5 @@
|
|
|
41
45
|
"clean-ansi": "0.1.7",
|
|
42
46
|
"makage": "0.1.8"
|
|
43
47
|
},
|
|
44
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "89afff9da8425e38d1c8b75b2021833581a90307"
|
|
45
49
|
}
|
package/ui/engine.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event-driven UI Engine
|
|
3
|
+
*
|
|
4
|
+
* A flexible engine for building custom interactive terminal UIs
|
|
5
|
+
* with support for key events, timers, and async updates.
|
|
6
|
+
*/
|
|
7
|
+
import { Readable, Writable } from 'stream';
|
|
8
|
+
import { TerminalKeypress } from '../keypress';
|
|
9
|
+
import { UIEvent, UIScreenConfig } from './types';
|
|
10
|
+
/**
|
|
11
|
+
* ANSI escape codes for terminal control
|
|
12
|
+
*/
|
|
13
|
+
declare const ANSI: {
|
|
14
|
+
clearScreen: string;
|
|
15
|
+
hideCursor: string;
|
|
16
|
+
showCursor: string;
|
|
17
|
+
cursorUp: (n: number) => string;
|
|
18
|
+
cursorDown: (n: number) => string;
|
|
19
|
+
cursorTo: (x: number, y: number) => string;
|
|
20
|
+
clearLine: string;
|
|
21
|
+
clearToEnd: string;
|
|
22
|
+
saveCursor: string;
|
|
23
|
+
restoreCursor: string;
|
|
24
|
+
};
|
|
25
|
+
export interface UIEngineOptions {
|
|
26
|
+
input?: Readable;
|
|
27
|
+
output?: Writable;
|
|
28
|
+
noTty?: boolean;
|
|
29
|
+
/** Existing keypress instance to reuse (avoids multiple listeners on stdin) */
|
|
30
|
+
keypress?: TerminalKeypress;
|
|
31
|
+
/** If true, engine owns the keypress and will destroy it on cleanup. Default: true if keypress not provided */
|
|
32
|
+
ownsKeypress?: boolean;
|
|
33
|
+
/** If true, clear the entire screen on start instead of just overwriting lines */
|
|
34
|
+
clearScreenOnStart?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare class UIEngine {
|
|
37
|
+
private input;
|
|
38
|
+
private output;
|
|
39
|
+
private noTty;
|
|
40
|
+
private keypress;
|
|
41
|
+
private ownsKeypress;
|
|
42
|
+
private clearScreenOnStart;
|
|
43
|
+
private tickTimer;
|
|
44
|
+
private lastLineCount;
|
|
45
|
+
constructor(options?: UIEngineOptions);
|
|
46
|
+
/**
|
|
47
|
+
* Write to output
|
|
48
|
+
*/
|
|
49
|
+
private write;
|
|
50
|
+
/**
|
|
51
|
+
* Clear the screen
|
|
52
|
+
*/
|
|
53
|
+
private clearScreen;
|
|
54
|
+
/**
|
|
55
|
+
* Hide the cursor
|
|
56
|
+
*/
|
|
57
|
+
private hideCursor;
|
|
58
|
+
/**
|
|
59
|
+
* Show the cursor
|
|
60
|
+
*/
|
|
61
|
+
private showCursor;
|
|
62
|
+
/**
|
|
63
|
+
* Render lines to the terminal, clearing previous output
|
|
64
|
+
*/
|
|
65
|
+
private render;
|
|
66
|
+
/**
|
|
67
|
+
* Run a custom UI screen
|
|
68
|
+
*/
|
|
69
|
+
run<S, V = unknown, D = unknown>(config: UIScreenConfig<S, V, D>): Promise<V | undefined>;
|
|
70
|
+
/**
|
|
71
|
+
* Dispatch an external event (for async updates)
|
|
72
|
+
*/
|
|
73
|
+
dispatch<S, V, D>(event: UIEvent<D>): void;
|
|
74
|
+
/**
|
|
75
|
+
* Cleanup resources
|
|
76
|
+
*/
|
|
77
|
+
destroy(): void;
|
|
78
|
+
}
|
|
79
|
+
export { ANSI };
|
package/ui/engine.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Event-driven UI Engine
|
|
4
|
+
*
|
|
5
|
+
* A flexible engine for building custom interactive terminal UIs
|
|
6
|
+
* with support for key events, timers, and async updates.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ANSI = exports.UIEngine = void 0;
|
|
10
|
+
const keypress_1 = require("../keypress");
|
|
11
|
+
const types_1 = require("./types");
|
|
12
|
+
/**
|
|
13
|
+
* Maps raw key codes to normalized Key enum
|
|
14
|
+
*/
|
|
15
|
+
const KEY_MAP = {
|
|
16
|
+
[keypress_1.KEY_CODES.UP_ARROW]: types_1.Key.UP,
|
|
17
|
+
[keypress_1.KEY_CODES.DOWN_ARROW]: types_1.Key.DOWN,
|
|
18
|
+
[keypress_1.KEY_CODES.LEFT_ARROW]: types_1.Key.LEFT,
|
|
19
|
+
[keypress_1.KEY_CODES.RIGHT_ARROW]: types_1.Key.RIGHT,
|
|
20
|
+
[keypress_1.KEY_CODES.ENTER]: types_1.Key.ENTER,
|
|
21
|
+
[keypress_1.KEY_CODES.SPACE]: types_1.Key.SPACE,
|
|
22
|
+
[keypress_1.KEY_CODES.BACKSPACE]: types_1.Key.BACKSPACE,
|
|
23
|
+
[keypress_1.KEY_CODES.BACKSPACE_LEGACY]: types_1.Key.BACKSPACE,
|
|
24
|
+
[keypress_1.KEY_CODES.CTRL_C]: types_1.Key.CTRL_C,
|
|
25
|
+
'\u001b': types_1.Key.ESCAPE,
|
|
26
|
+
'\t': types_1.Key.TAB,
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* ANSI escape codes for terminal control
|
|
30
|
+
*/
|
|
31
|
+
const ANSI = {
|
|
32
|
+
clearScreen: '\x1Bc',
|
|
33
|
+
hideCursor: '\x1B[?25l',
|
|
34
|
+
showCursor: '\x1B[?25h',
|
|
35
|
+
cursorUp: (n) => `\x1B[${n}A`,
|
|
36
|
+
cursorDown: (n) => `\x1B[${n}B`,
|
|
37
|
+
cursorTo: (x, y) => `\x1B[${y};${x}H`,
|
|
38
|
+
clearLine: '\x1B[2K',
|
|
39
|
+
clearToEnd: '\x1B[0J',
|
|
40
|
+
saveCursor: '\x1B[s',
|
|
41
|
+
restoreCursor: '\x1B[u',
|
|
42
|
+
};
|
|
43
|
+
exports.ANSI = ANSI;
|
|
44
|
+
class UIEngine {
|
|
45
|
+
input;
|
|
46
|
+
output;
|
|
47
|
+
noTty;
|
|
48
|
+
keypress = null;
|
|
49
|
+
ownsKeypress;
|
|
50
|
+
clearScreenOnStart;
|
|
51
|
+
tickTimer = null;
|
|
52
|
+
lastLineCount = 0;
|
|
53
|
+
constructor(options = {}) {
|
|
54
|
+
this.input = options.input ?? process.stdin;
|
|
55
|
+
this.output = options.output ?? process.stdout;
|
|
56
|
+
this.noTty = options.noTty ?? false;
|
|
57
|
+
this.keypress = options.keypress ?? null;
|
|
58
|
+
this.ownsKeypress = options.ownsKeypress ?? (options.keypress === undefined);
|
|
59
|
+
this.clearScreenOnStart = options.clearScreenOnStart ?? false;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Write to output
|
|
63
|
+
*/
|
|
64
|
+
write(text) {
|
|
65
|
+
this.output.write(text);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Clear the screen
|
|
69
|
+
*/
|
|
70
|
+
clearScreen() {
|
|
71
|
+
this.write(ANSI.clearScreen);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Hide the cursor
|
|
75
|
+
*/
|
|
76
|
+
hideCursor() {
|
|
77
|
+
this.write(ANSI.hideCursor);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Show the cursor
|
|
81
|
+
*/
|
|
82
|
+
showCursor() {
|
|
83
|
+
this.write(ANSI.showCursor);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Render lines to the terminal, clearing previous output
|
|
87
|
+
*/
|
|
88
|
+
render(lines) {
|
|
89
|
+
// Move cursor up to overwrite previous output
|
|
90
|
+
if (this.lastLineCount > 0) {
|
|
91
|
+
this.write(ANSI.cursorUp(this.lastLineCount));
|
|
92
|
+
}
|
|
93
|
+
// Clear and write each line
|
|
94
|
+
for (const line of lines) {
|
|
95
|
+
this.write(ANSI.clearLine + line + '\n');
|
|
96
|
+
}
|
|
97
|
+
// Clear any remaining lines from previous render
|
|
98
|
+
if (lines.length < this.lastLineCount) {
|
|
99
|
+
for (let i = 0; i < this.lastLineCount - lines.length; i++) {
|
|
100
|
+
this.write(ANSI.clearLine + '\n');
|
|
101
|
+
}
|
|
102
|
+
// Move cursor back up
|
|
103
|
+
this.write(ANSI.cursorUp(this.lastLineCount - lines.length));
|
|
104
|
+
}
|
|
105
|
+
this.lastLineCount = lines.length;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Run a custom UI screen
|
|
109
|
+
*/
|
|
110
|
+
async run(config) {
|
|
111
|
+
if (this.noTty) {
|
|
112
|
+
// In non-TTY mode, just return undefined
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
let state = config.initialState;
|
|
116
|
+
let resolved = false;
|
|
117
|
+
let result;
|
|
118
|
+
const createdKeypress = !this.keypress;
|
|
119
|
+
// Setup keypress handler - reuse existing or create new
|
|
120
|
+
if (!this.keypress) {
|
|
121
|
+
this.keypress = new keypress_1.TerminalKeypress(this.noTty, this.input);
|
|
122
|
+
}
|
|
123
|
+
this.keypress.resume();
|
|
124
|
+
// Clear screen on start if requested (matches legacy prompt behavior)
|
|
125
|
+
if (this.clearScreenOnStart) {
|
|
126
|
+
this.clearScreen();
|
|
127
|
+
}
|
|
128
|
+
// Hide cursor if requested
|
|
129
|
+
if (config.hideCursor) {
|
|
130
|
+
this.hideCursor();
|
|
131
|
+
}
|
|
132
|
+
// Call onStart hook
|
|
133
|
+
config.onStart?.(state);
|
|
134
|
+
// Initial render
|
|
135
|
+
const lines = config.render(state);
|
|
136
|
+
this.render(lines);
|
|
137
|
+
// Setup tick timer if interval specified
|
|
138
|
+
if (config.tickInterval && config.tickInterval > 0) {
|
|
139
|
+
this.tickTimer = setInterval(() => {
|
|
140
|
+
if (resolved)
|
|
141
|
+
return;
|
|
142
|
+
const event = { type: 'tick' };
|
|
143
|
+
const eventResult = config.onEvent(event, state);
|
|
144
|
+
state = eventResult.state;
|
|
145
|
+
this.render(config.render(state));
|
|
146
|
+
if (eventResult.done) {
|
|
147
|
+
resolved = true;
|
|
148
|
+
result = eventResult.value;
|
|
149
|
+
}
|
|
150
|
+
}, config.tickInterval);
|
|
151
|
+
}
|
|
152
|
+
return new Promise((resolve) => {
|
|
153
|
+
const handleEvent = (event) => {
|
|
154
|
+
if (resolved)
|
|
155
|
+
return;
|
|
156
|
+
const eventResult = config.onEvent(event, state);
|
|
157
|
+
state = eventResult.state;
|
|
158
|
+
this.render(config.render(state));
|
|
159
|
+
if (eventResult.done) {
|
|
160
|
+
resolved = true;
|
|
161
|
+
result = eventResult.value;
|
|
162
|
+
cleanup();
|
|
163
|
+
resolve(result);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const cleanup = () => {
|
|
167
|
+
// Clear tick timer
|
|
168
|
+
if (this.tickTimer) {
|
|
169
|
+
clearInterval(this.tickTimer);
|
|
170
|
+
this.tickTimer = null;
|
|
171
|
+
}
|
|
172
|
+
// Cleanup keypress - pause clears handlers, which is what we want
|
|
173
|
+
// Only destroy if we created it and own it
|
|
174
|
+
if (this.keypress) {
|
|
175
|
+
this.keypress.pause();
|
|
176
|
+
if (createdKeypress && this.ownsKeypress) {
|
|
177
|
+
this.keypress.destroy();
|
|
178
|
+
this.keypress = null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// Show cursor
|
|
182
|
+
if (config.hideCursor) {
|
|
183
|
+
this.showCursor();
|
|
184
|
+
}
|
|
185
|
+
// Call onExit hook
|
|
186
|
+
config.onExit?.(state, result);
|
|
187
|
+
};
|
|
188
|
+
// Register key handlers
|
|
189
|
+
// Handle special keys - but let TerminalKeypress handle CTRL+C exit
|
|
190
|
+
Object.entries(KEY_MAP).forEach(([code, key]) => {
|
|
191
|
+
this.keypress.on(code, () => {
|
|
192
|
+
// For CTRL+C, just cleanup - TerminalKeypress will call process.exit
|
|
193
|
+
if (key === types_1.Key.CTRL_C) {
|
|
194
|
+
cleanup();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
handleEvent({ type: 'key', key });
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
// Handle alphanumeric characters (lowercase only to match legacy behavior)
|
|
201
|
+
'abcdefghijklmnopqrstuvwxyz0123456789'.split('').forEach(char => {
|
|
202
|
+
this.keypress.on(char, () => {
|
|
203
|
+
handleEvent({ type: 'char', char });
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
// Handle punctuation and special characters
|
|
207
|
+
'!@#$%^&*()_+-=[]{}|;:\'",.<>?/`~'.split('').forEach(char => {
|
|
208
|
+
this.keypress.on(char, () => {
|
|
209
|
+
handleEvent({ type: 'char', char });
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Dispatch an external event (for async updates)
|
|
216
|
+
*/
|
|
217
|
+
dispatch(event) {
|
|
218
|
+
// This would be used for external event dispatch
|
|
219
|
+
// Implementation depends on how we want to handle async updates
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Cleanup resources
|
|
223
|
+
*/
|
|
224
|
+
destroy() {
|
|
225
|
+
if (this.tickTimer) {
|
|
226
|
+
clearInterval(this.tickTimer);
|
|
227
|
+
this.tickTimer = null;
|
|
228
|
+
}
|
|
229
|
+
if (this.keypress) {
|
|
230
|
+
this.keypress.destroy();
|
|
231
|
+
this.keypress = null;
|
|
232
|
+
}
|
|
233
|
+
this.showCursor();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
exports.UIEngine = UIEngine;
|
package/ui/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UI Module
|
|
3
|
+
*
|
|
4
|
+
* Event-driven UI components for building rich terminal interfaces
|
|
5
|
+
*/
|
|
6
|
+
export { UIEngine, UIEngineOptions, ANSI } from './engine';
|
|
7
|
+
export { Key, UIEvent, EventResult, UIScreenConfig, SpinnerConfig, ProgressConfig, StreamConfig, PackageInfo, UpgradeSelection, } from './types';
|
|
8
|
+
export { Spinner, createSpinner, SPINNER_STYLES } from './spinner';
|
|
9
|
+
export { ProgressBar, createProgress } from './progress';
|
|
10
|
+
export { StreamingText, createStream } from './stream';
|
|
11
|
+
export { interactiveUpgrade, upgradePrompt } from './upgrade';
|
|
12
|
+
export { listPromptEngine, autocompletePromptEngine, checkboxPromptEngine, filterOptions, renderPromptHeader, ListPromptConfig, AutocompletePromptConfig, CheckboxPromptConfig, } from './prompts';
|