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/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';
package/ui/index.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /**
3
+ * UI Module
4
+ *
5
+ * Event-driven UI components for building rich terminal interfaces
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.renderPromptHeader = exports.filterOptions = exports.checkboxPromptEngine = exports.autocompletePromptEngine = exports.listPromptEngine = exports.upgradePrompt = exports.interactiveUpgrade = exports.createStream = exports.StreamingText = exports.createProgress = exports.ProgressBar = exports.SPINNER_STYLES = exports.createSpinner = exports.Spinner = exports.Key = exports.ANSI = exports.UIEngine = void 0;
9
+ // Core engine
10
+ var engine_1 = require("./engine");
11
+ Object.defineProperty(exports, "UIEngine", { enumerable: true, get: function () { return engine_1.UIEngine; } });
12
+ Object.defineProperty(exports, "ANSI", { enumerable: true, get: function () { return engine_1.ANSI; } });
13
+ // Types
14
+ var types_1 = require("./types");
15
+ Object.defineProperty(exports, "Key", { enumerable: true, get: function () { return types_1.Key; } });
16
+ // Components
17
+ var spinner_1 = require("./spinner");
18
+ Object.defineProperty(exports, "Spinner", { enumerable: true, get: function () { return spinner_1.Spinner; } });
19
+ Object.defineProperty(exports, "createSpinner", { enumerable: true, get: function () { return spinner_1.createSpinner; } });
20
+ Object.defineProperty(exports, "SPINNER_STYLES", { enumerable: true, get: function () { return spinner_1.SPINNER_STYLES; } });
21
+ var progress_1 = require("./progress");
22
+ Object.defineProperty(exports, "ProgressBar", { enumerable: true, get: function () { return progress_1.ProgressBar; } });
23
+ Object.defineProperty(exports, "createProgress", { enumerable: true, get: function () { return progress_1.createProgress; } });
24
+ var stream_1 = require("./stream");
25
+ Object.defineProperty(exports, "StreamingText", { enumerable: true, get: function () { return stream_1.StreamingText; } });
26
+ Object.defineProperty(exports, "createStream", { enumerable: true, get: function () { return stream_1.createStream; } });
27
+ var upgrade_1 = require("./upgrade");
28
+ Object.defineProperty(exports, "interactiveUpgrade", { enumerable: true, get: function () { return upgrade_1.interactiveUpgrade; } });
29
+ Object.defineProperty(exports, "upgradePrompt", { enumerable: true, get: function () { return upgrade_1.upgradePrompt; } });
30
+ // Engine-based prompt implementations (internal use)
31
+ var prompts_1 = require("./prompts");
32
+ Object.defineProperty(exports, "listPromptEngine", { enumerable: true, get: function () { return prompts_1.listPromptEngine; } });
33
+ Object.defineProperty(exports, "autocompletePromptEngine", { enumerable: true, get: function () { return prompts_1.autocompletePromptEngine; } });
34
+ Object.defineProperty(exports, "checkboxPromptEngine", { enumerable: true, get: function () { return prompts_1.checkboxPromptEngine; } });
35
+ Object.defineProperty(exports, "filterOptions", { enumerable: true, get: function () { return prompts_1.filterOptions; } });
36
+ Object.defineProperty(exports, "renderPromptHeader", { enumerable: true, get: function () { return prompts_1.renderPromptHeader; } });
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Progress Bar Component
3
+ *
4
+ * Visual progress indicator for operations with known completion
5
+ */
6
+ import { Writable } from 'stream';
7
+ import { ProgressConfig } from './types';
8
+ /**
9
+ * Progress bar that can be controlled externally
10
+ */
11
+ export declare class ProgressBar {
12
+ private width;
13
+ private showPercentage;
14
+ private state;
15
+ private output;
16
+ private isRunning;
17
+ constructor(config: ProgressConfig, output?: Writable);
18
+ /**
19
+ * Start the progress bar
20
+ */
21
+ start(): this;
22
+ /**
23
+ * Update progress (0-1)
24
+ */
25
+ update(value: number, text?: string): this;
26
+ /**
27
+ * Increment progress
28
+ */
29
+ increment(amount?: number): this;
30
+ /**
31
+ * Complete the progress bar
32
+ */
33
+ complete(text?: string): this;
34
+ /**
35
+ * Mark as error
36
+ */
37
+ error(text?: string): this;
38
+ /**
39
+ * Render the current state
40
+ */
41
+ private render;
42
+ }
43
+ /**
44
+ * Create and start a progress bar
45
+ */
46
+ export declare function createProgress(text: string, options?: Partial<ProgressConfig>): ProgressBar;
package/ui/progress.js ADDED
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ /**
3
+ * Progress Bar Component
4
+ *
5
+ * Visual progress indicator for operations with known completion
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.ProgressBar = void 0;
9
+ exports.createProgress = createProgress;
10
+ const yanse_1 = require("yanse");
11
+ /**
12
+ * Progress bar that can be controlled externally
13
+ */
14
+ class ProgressBar {
15
+ width;
16
+ showPercentage;
17
+ state;
18
+ output;
19
+ isRunning = false;
20
+ constructor(config, output = process.stdout) {
21
+ this.width = config.width ?? 40;
22
+ this.showPercentage = config.showPercentage ?? true;
23
+ this.output = output;
24
+ this.state = {
25
+ value: 0,
26
+ text: config.text,
27
+ status: 'active',
28
+ };
29
+ }
30
+ /**
31
+ * Start the progress bar
32
+ */
33
+ start() {
34
+ if (this.isRunning)
35
+ return this;
36
+ this.isRunning = true;
37
+ // Hide cursor
38
+ this.output.write('\x1B[?25l');
39
+ // Initial render
40
+ this.render();
41
+ return this;
42
+ }
43
+ /**
44
+ * Update progress (0-1)
45
+ */
46
+ update(value, text) {
47
+ this.state.value = Math.max(0, Math.min(1, value));
48
+ if (text) {
49
+ this.state.text = text;
50
+ }
51
+ if (this.isRunning) {
52
+ this.render();
53
+ }
54
+ return this;
55
+ }
56
+ /**
57
+ * Increment progress
58
+ */
59
+ increment(amount = 0.1) {
60
+ return this.update(this.state.value + amount);
61
+ }
62
+ /**
63
+ * Complete the progress bar
64
+ */
65
+ complete(text) {
66
+ this.state.value = 1;
67
+ this.state.status = 'complete';
68
+ if (text) {
69
+ this.state.text = text;
70
+ }
71
+ this.render();
72
+ // Show cursor and newline
73
+ this.output.write('\x1B[?25h\n');
74
+ this.isRunning = false;
75
+ return this;
76
+ }
77
+ /**
78
+ * Mark as error
79
+ */
80
+ error(text) {
81
+ this.state.status = 'error';
82
+ if (text) {
83
+ this.state.text = text;
84
+ }
85
+ this.render();
86
+ // Show cursor and newline
87
+ this.output.write('\x1B[?25h\n');
88
+ this.isRunning = false;
89
+ return this;
90
+ }
91
+ /**
92
+ * Render the current state
93
+ */
94
+ render() {
95
+ const { value, text, status } = this.state;
96
+ const filled = Math.round(value * this.width);
97
+ const empty = this.width - filled;
98
+ let bar;
99
+ let icon;
100
+ switch (status) {
101
+ case 'complete':
102
+ bar = (0, yanse_1.green)('█'.repeat(this.width));
103
+ icon = (0, yanse_1.green)('✔');
104
+ break;
105
+ case 'error':
106
+ bar = (0, yanse_1.dim)('█'.repeat(filled) + '░'.repeat(empty));
107
+ icon = '\x1B[31m✖\x1B[0m'; // red
108
+ break;
109
+ default:
110
+ bar = (0, yanse_1.cyan)('█'.repeat(filled)) + (0, yanse_1.dim)('░'.repeat(empty));
111
+ icon = (0, yanse_1.cyan)('◐');
112
+ }
113
+ let line = `${icon} ${text} [${bar}]`;
114
+ if (this.showPercentage) {
115
+ const percent = Math.round(value * 100);
116
+ line += ` ${(0, yanse_1.white)(percent.toString().padStart(3))}%`;
117
+ }
118
+ // Clear line and write
119
+ this.output.write('\r\x1B[2K' + line);
120
+ }
121
+ }
122
+ exports.ProgressBar = ProgressBar;
123
+ /**
124
+ * Create and start a progress bar
125
+ */
126
+ function createProgress(text, options) {
127
+ return new ProgressBar({ text, ...options });
128
+ }
@@ -0,0 +1,49 @@
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 { TerminalKeypress } from '../keypress';
8
+ import { OptionValue } from '../question';
9
+ import { Readable, Writable } from 'stream';
10
+ /**
11
+ * Render the prompt header lines
12
+ */
13
+ export declare function renderPromptHeader(promptMessage: string, input: string): string[];
14
+ /**
15
+ * Filter options based on input (matches existing prompt.ts behavior)
16
+ */
17
+ export declare function filterOptions(options: OptionValue[], input: string): OptionValue[];
18
+ export interface ListPromptConfig {
19
+ options: OptionValue[];
20
+ promptMessage: string;
21
+ maxLines: number;
22
+ keypress: TerminalKeypress;
23
+ input: Readable;
24
+ output: Writable;
25
+ noTty: boolean;
26
+ }
27
+ export declare function listPromptEngine(config: ListPromptConfig): Promise<any>;
28
+ export interface AutocompletePromptConfig {
29
+ options: OptionValue[];
30
+ promptMessage: string;
31
+ maxLines: number;
32
+ keypress: TerminalKeypress;
33
+ input: Readable;
34
+ output: Writable;
35
+ noTty: boolean;
36
+ }
37
+ export declare function autocompletePromptEngine(config: AutocompletePromptConfig): Promise<any>;
38
+ export interface CheckboxPromptConfig {
39
+ options: OptionValue[];
40
+ defaultSelections: boolean[];
41
+ promptMessage: string;
42
+ maxLines: number;
43
+ returnFullResults: boolean;
44
+ keypress: TerminalKeypress;
45
+ input: Readable;
46
+ output: Writable;
47
+ noTty: boolean;
48
+ }
49
+ export declare function checkboxPromptEngine(config: CheckboxPromptConfig): Promise<OptionValue[]>;