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.
@@ -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
@@ -2,3 +2,4 @@ export * from './commander';
2
2
  export * from './prompt';
3
3
  export * from './question';
4
4
  export * from './resolvers';
5
+ export * from './ui';
package/index.js CHANGED
@@ -18,3 +18,4 @@ __exportStar(require("./commander"), exports);
18
18
  __exportStar(require("./prompt"), exports);
19
19
  __exportStar(require("./question"), exports);
20
20
  __exportStar(require("./resolvers"), exports);
21
+ __exportStar(require("./ui"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "4.1.1",
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": "bf69015f5975c1f2b8baa452240e3bcdba496cd1"
48
+ "gitHead": "89afff9da8425e38d1c8b75b2021833581a90307"
45
49
  }
package/prompt.js CHANGED
@@ -759,6 +759,19 @@ class Inquirerer {
759
759
  });
760
760
  const updateFilteredOptions = () => {
761
761
  filteredOptions = this.filterOptions(options, input);
762
+ // Clamp selectedIndex and startIndex when filtered results change
763
+ if (filteredOptions.length === 0) {
764
+ selectedIndex = 0;
765
+ startIndex = 0;
766
+ }
767
+ else {
768
+ if (selectedIndex >= filteredOptions.length) {
769
+ selectedIndex = filteredOptions.length - 1;
770
+ }
771
+ if (startIndex > Math.max(0, filteredOptions.length - maxLines)) {
772
+ startIndex = Math.max(0, filteredOptions.length - maxLines);
773
+ }
774
+ }
762
775
  };
763
776
  const display = () => {
764
777
  this.clearScreen();
@@ -795,6 +808,10 @@ class Inquirerer {
795
808
  });
796
809
  });
797
810
  this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
811
+ if (filteredOptions.length === 0) {
812
+ display();
813
+ return;
814
+ }
798
815
  selectedIndex = selectedIndex > 0 ? selectedIndex - 1 : filteredOptions.length - 1;
799
816
  if (selectedIndex < startIndex) {
800
817
  startIndex = selectedIndex; // Scroll up
@@ -805,6 +822,10 @@ class Inquirerer {
805
822
  display();
806
823
  });
807
824
  this.keypress.on(keypress_1.KEY_CODES.DOWN_ARROW, () => {
825
+ if (filteredOptions.length === 0) {
826
+ display();
827
+ return;
828
+ }
808
829
  selectedIndex = (selectedIndex + 1) % filteredOptions.length;
809
830
  if (selectedIndex >= startIndex + maxLines) {
810
831
  startIndex = selectedIndex - maxLines + 1; // Scroll down
@@ -815,6 +836,10 @@ class Inquirerer {
815
836
  display();
816
837
  });
817
838
  this.keypress.on(keypress_1.KEY_CODES.SPACE, () => {
839
+ if (filteredOptions.length === 0 || !filteredOptions[selectedIndex]) {
840
+ display();
841
+ return;
842
+ }
818
843
  // Map filtered index back to the original index in options
819
844
  selections[options.indexOf(filteredOptions[selectedIndex])] = !selections[options.indexOf(filteredOptions[selectedIndex])];
820
845
  display();
@@ -914,6 +939,10 @@ class Inquirerer {
914
939
  });
915
940
  // Navigation
916
941
  this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
942
+ if (filteredOptions.length === 0) {
943
+ display();
944
+ return;
945
+ }
917
946
  selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : filteredOptions.length - 1;
918
947
  if (selectedIndex < startIndex) {
919
948
  startIndex = selectedIndex; // Scroll up
@@ -924,6 +953,10 @@ class Inquirerer {
924
953
  display();
925
954
  });
926
955
  this.keypress.on(keypress_1.KEY_CODES.DOWN_ARROW, () => {
956
+ if (filteredOptions.length === 0) {
957
+ display();
958
+ return;
959
+ }
927
960
  selectedIndex = (selectedIndex + 1) % filteredOptions.length;
928
961
  if (selectedIndex >= startIndex + maxLines) {
929
962
  startIndex = selectedIndex - maxLines + 1; // Scroll down
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 };