@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.10

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.
Files changed (194) hide show
  1. package/CHANGELOG.md +217 -1
  2. package/dist/core/cli/dispatch.d.mts +6 -5
  3. package/dist/core/cli/dispatch.mjs +205 -1
  4. package/dist/core/cli/help-links.d.mts +0 -1
  5. package/dist/core/cli/help-links.mjs +41 -1
  6. package/dist/core/cli/index.d.mts +81 -8
  7. package/dist/core/cli/index.mjs +945 -1
  8. package/dist/core/cli/planner.d.mts +5 -1
  9. package/dist/core/cli/planner.mjs +300 -1
  10. package/dist/core/cli/plugin.d.mts +1 -1
  11. package/dist/core/cli/plugin.mjs +41 -1
  12. package/dist/core/cli/propagate.d.mts +1 -1
  13. package/dist/core/cli/propagate.mjs +57 -1
  14. package/dist/core/cli/root-help.d.mts +2 -1
  15. package/dist/core/cli/root-help.mjs +226 -4
  16. package/dist/core/cli/root-surface.d.mts +12 -1
  17. package/dist/core/cli/root-surface.mjs +28 -1
  18. package/dist/core/cli/runtime-preflight.d.mts +14 -2
  19. package/dist/core/cli/runtime-preflight.mjs +169 -1
  20. package/dist/core/completion/index.d.mts +0 -1
  21. package/dist/core/completion/index.mjs +90 -1
  22. package/dist/core/completion/shells/bash.d.mts +0 -1
  23. package/dist/core/completion/shells/bash.mjs +308 -3
  24. package/dist/core/completion/shells/fish.d.mts +0 -1
  25. package/dist/core/completion/shells/fish.mjs +162 -2
  26. package/dist/core/completion/shells/powershell.d.mts +0 -1
  27. package/dist/core/completion/shells/powershell.mjs +358 -2
  28. package/dist/core/completion/shells/shared.d.mts +13 -2
  29. package/dist/core/completion/shells/shared.mjs +176 -1
  30. package/dist/core/completion/shells/zsh.d.mts +0 -1
  31. package/dist/core/completion/shells/zsh.mjs +251 -2
  32. package/dist/core/config/index.d.mts +2 -3
  33. package/dist/core/config/index.mjs +246 -1
  34. package/dist/core/config/package-json.d.mts +27 -4
  35. package/dist/core/config/package-json.mjs +386 -3
  36. package/dist/core/errors/index.d.mts +3 -3
  37. package/dist/core/errors/index.mjs +87 -1
  38. package/dist/core/execution/index.d.mts +0 -1
  39. package/dist/core/execution/index.mjs +201 -1
  40. package/dist/core/help/ansi.d.mts +11 -5
  41. package/dist/core/help/ansi.mjs +121 -2
  42. package/dist/core/help/index.d.mts +22 -3
  43. package/dist/core/help/index.mjs +341 -7
  44. package/dist/core/help/theme.d.mts +68 -0
  45. package/dist/core/help/theme.mjs +46 -0
  46. package/dist/core/json-schema/index.d.mts +15 -2
  47. package/dist/core/json-schema/index.mjs +536 -12
  48. package/dist/core/json-schema/meta-descriptions.generated.d.mts +55 -38
  49. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  50. package/dist/core/output/activity.d.mts +0 -1
  51. package/dist/core/output/activity.mjs +452 -1
  52. package/dist/core/output/bind.mjs +56 -1
  53. package/dist/core/output/contracts.d.mts +4 -5
  54. package/dist/core/output/contracts.mjs +57 -1
  55. package/dist/core/output/display-value.mjs +32 -1
  56. package/dist/core/output/index.d.mts +21 -1
  57. package/dist/core/output/index.mjs +442 -2
  58. package/dist/core/output/renderers.d.mts +0 -1
  59. package/dist/core/output/renderers.mjs +46 -1
  60. package/dist/core/output/writer.mjs +12 -1
  61. package/dist/core/parse/index.d.mts +40 -7
  62. package/dist/core/parse/index.mjs +727 -1
  63. package/dist/core/prompt/index.d.mts +1 -1
  64. package/dist/core/prompt/index.mjs +348 -6
  65. package/dist/core/resolve/args.d.mts +1 -1
  66. package/dist/core/resolve/args.mjs +105 -1
  67. package/dist/core/resolve/coerce.d.mts +1 -1
  68. package/dist/core/resolve/coerce.mjs +336 -1
  69. package/dist/core/resolve/config.mjs +19 -1
  70. package/dist/core/resolve/contracts.d.mts +9 -4
  71. package/dist/core/resolve/errors.d.mts +0 -1
  72. package/dist/core/resolve/errors.mjs +140 -2
  73. package/dist/core/resolve/flags.d.mts +7 -2
  74. package/dist/core/resolve/flags.mjs +251 -1
  75. package/dist/core/resolve/index.d.mts +2 -2
  76. package/dist/core/resolve/index.mjs +85 -1
  77. package/dist/core/resolve/property.d.mts +4 -2
  78. package/dist/core/resolve/property.mjs +53 -1
  79. package/dist/core/schema/activity.d.mts +31 -11
  80. package/dist/core/schema/arg.d.mts +3 -4
  81. package/dist/core/schema/arg.mjs +463 -1
  82. package/dist/core/schema/command.d.mts +26 -6
  83. package/dist/core/schema/command.mjs +726 -1
  84. package/dist/core/schema/flag.d.mts +485 -19
  85. package/dist/core/schema/flag.mjs +705 -1
  86. package/dist/core/schema/index.d.mts +4 -2
  87. package/dist/core/schema/middleware.d.mts +4 -3
  88. package/dist/core/schema/middleware.mjs +41 -1
  89. package/dist/core/schema/number-constraints.mjs +60 -1
  90. package/dist/core/schema/prompt.d.mts +6 -3
  91. package/dist/core/schema/run.d.mts +20 -1
  92. package/dist/core/schema/string-constraints.d.mts +97 -0
  93. package/dist/core/schema/string-constraints.mjs +69 -0
  94. package/dist/core/schema/value-parsers.d.mts +78 -0
  95. package/dist/core/schema/value-parsers.mjs +142 -0
  96. package/dist/core/schema-dsl/runtime.mjs +271 -2
  97. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  98. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  99. package/dist/core/testkit/index.d.mts +0 -31
  100. package/dist/core/testkit/index.mjs +83 -1
  101. package/dist/index.d.mts +11 -6
  102. package/dist/index.mjs +22 -1
  103. package/dist/runtime/adapter.d.mts +38 -2
  104. package/dist/runtime/adapter.mjs +87 -1
  105. package/dist/runtime/auto.d.mts +2 -3
  106. package/dist/runtime/auto.mjs +45 -1
  107. package/dist/runtime/deno.d.mts +24 -5
  108. package/dist/runtime/deno.mjs +262 -2
  109. package/dist/runtime/detect.mjs +45 -1
  110. package/dist/runtime/index.d.mts +3 -4
  111. package/dist/runtime/node.d.mts +8 -2
  112. package/dist/runtime/node.mjs +220 -2
  113. package/dist/runtime/paths.mjs +48 -1
  114. package/dist/runtime.d.mts +4 -4
  115. package/dist/runtime.mjs +6 -1
  116. package/dist/strings.mjs +22 -1
  117. package/dist/testkit.d.mts +1 -0
  118. package/dist/testkit.mjs +5 -1
  119. package/dist/version.d.mts +7 -3
  120. package/dist/version.mjs +7 -1
  121. package/dreamcli.schema.json +36 -7
  122. package/package.json +45 -42
  123. package/dist/runtime/bun.d.mts +0 -28
  124. package/dist/runtime/bun.mjs +0 -1
  125. package/src/core/cli/dispatch.ts +0 -355
  126. package/src/core/cli/help-links.ts +0 -82
  127. package/src/core/cli/index.ts +0 -1614
  128. package/src/core/cli/planner.ts +0 -656
  129. package/src/core/cli/plugin.ts +0 -117
  130. package/src/core/cli/propagate.ts +0 -97
  131. package/src/core/cli/root-help.ts +0 -342
  132. package/src/core/cli/root-surface.ts +0 -87
  133. package/src/core/cli/runtime-preflight.ts +0 -456
  134. package/src/core/completion/index.ts +0 -138
  135. package/src/core/completion/shells/bash.ts +0 -459
  136. package/src/core/completion/shells/fish.ts +0 -259
  137. package/src/core/completion/shells/powershell.ts +0 -439
  138. package/src/core/completion/shells/shared.ts +0 -310
  139. package/src/core/completion/shells/zsh.ts +0 -381
  140. package/src/core/config/index.ts +0 -417
  141. package/src/core/config/package-json.ts +0 -560
  142. package/src/core/errors/index.ts +0 -193
  143. package/src/core/execution/index.ts +0 -265
  144. package/src/core/help/ansi.ts +0 -144
  145. package/src/core/help/index.ts +0 -563
  146. package/src/core/json-schema/index.ts +0 -910
  147. package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
  148. package/src/core/output/activity.ts +0 -575
  149. package/src/core/output/bind.ts +0 -59
  150. package/src/core/output/contracts.ts +0 -194
  151. package/src/core/output/display-value.ts +0 -48
  152. package/src/core/output/index.ts +0 -704
  153. package/src/core/output/renderers.ts +0 -125
  154. package/src/core/output/writer.ts +0 -46
  155. package/src/core/parse/index.ts +0 -742
  156. package/src/core/prompt/index.ts +0 -623
  157. package/src/core/resolve/args.ts +0 -144
  158. package/src/core/resolve/coerce.ts +0 -500
  159. package/src/core/resolve/config.ts +0 -26
  160. package/src/core/resolve/contracts.ts +0 -135
  161. package/src/core/resolve/errors.ts +0 -227
  162. package/src/core/resolve/flags.ts +0 -269
  163. package/src/core/resolve/index.ts +0 -108
  164. package/src/core/resolve/property.ts +0 -99
  165. package/src/core/schema/activity.ts +0 -240
  166. package/src/core/schema/arg.ts +0 -841
  167. package/src/core/schema/command.ts +0 -1500
  168. package/src/core/schema/flag.ts +0 -917
  169. package/src/core/schema/index.ts +0 -96
  170. package/src/core/schema/middleware.ts +0 -171
  171. package/src/core/schema/number-constraints.ts +0 -153
  172. package/src/core/schema/prompt.ts +0 -171
  173. package/src/core/schema/run.ts +0 -198
  174. package/src/core/schema-dsl/index.ts +0 -109
  175. package/src/core/schema-dsl/parse.ts +0 -262
  176. package/src/core/schema-dsl/runtime.ts +0 -609
  177. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  178. package/src/core/testkit/index.ts +0 -147
  179. package/src/index.ts +0 -181
  180. package/src/runtime/adapter.ts +0 -303
  181. package/src/runtime/auto.ts +0 -86
  182. package/src/runtime/bun.ts +0 -48
  183. package/src/runtime/deno.ts +0 -367
  184. package/src/runtime/detect.ts +0 -105
  185. package/src/runtime/index.ts +0 -21
  186. package/src/runtime/node-builtins.d.ts +0 -27
  187. package/src/runtime/node.ts +0 -289
  188. package/src/runtime/paths.ts +0 -64
  189. package/src/runtime/support.ts +0 -77
  190. package/src/runtime.ts +0 -31
  191. package/src/schema.ts +0 -16
  192. package/src/strings.ts +0 -34
  193. package/src/testkit.ts +0 -25
  194. package/src/version.ts +0 -20
@@ -1,575 +0,0 @@
1
- /**
2
- * Activity handle implementations — spinner and progress bar.
3
- *
4
- * Four tiers per activity type:
5
- * - **Noop** — silent, all methods no-op (jsonMode / non-TTY silent)
6
- * - **Static** — plain text at lifecycle boundaries (non-TTY, fallback='static')
7
- * - **TTY** — animated ANSI rendering (braille spinner, bar with percentage)
8
- * - **Capture** — records `ActivityEvent[]` for testkit assertion
9
- *
10
- * Extracted from `index.ts` to keep the main OutputChannel file focused on
11
- * the `Out` implementation. Depends only on `writer.ts` (leaf) and
12
- * `schema/command.ts` (types).
13
- *
14
- * @module dreamcli/core/output/activity
15
- * @internal
16
- */
17
-
18
- import type {
19
- ActivityEvent,
20
- ProgressHandle,
21
- ProgressOptions,
22
- SpinnerHandle,
23
- } from '#internals/core/schema/activity.ts';
24
- import { bindMethods } from './bind.ts';
25
- import type { WriteFn } from './writer.ts';
26
- import { writeLine } from './writer.ts';
27
-
28
- // --- Timer globals — minimal declarations for setInterval/clearInterval.
29
- //
30
- // The project targets ES2022 without DOM or Node lib typings. Timer APIs
31
- // are universally available across Node, Bun, Deno, and browsers, but the
32
- // type checker doesn't know about them. Declaring the subset we need here
33
- // avoids adding `@types/node` as a dependency.
34
-
35
- declare function setInterval(callback: () => void, ms: number): unknown;
36
- declare function clearInterval(handle: unknown): void;
37
-
38
- // --- Noop activity handles — silent mode (non-TTY, fallback='silent')
39
-
40
- /**
41
- * Noop spinner handle singleton.
42
- *
43
- * All methods are no-ops. Used when spinners should produce no output
44
- * at all — `jsonMode`, or non-TTY with `fallback: 'silent'` (default).
45
- *
46
- * @internal
47
- */
48
- const noopSpinnerHandle: SpinnerHandle = {
49
- update() {},
50
- succeed() {},
51
- fail() {},
52
- stop() {},
53
- wrap<T>(promise: Promise<T>): Promise<T> {
54
- return promise;
55
- },
56
- };
57
-
58
- /**
59
- * Noop progress handle singleton.
60
- *
61
- * All methods are no-ops. Used when progress bars should produce no
62
- * output at all — `jsonMode`, or non-TTY with `fallback: 'silent'`.
63
- *
64
- * @internal
65
- */
66
- const noopProgressHandle: ProgressHandle = {
67
- increment() {},
68
- update() {},
69
- done() {},
70
- fail() {},
71
- };
72
-
73
- // --- Static activity handles — plain text mode (non-TTY, fallback='static')
74
-
75
- /**
76
- * Static spinner handle — emits plain text at lifecycle boundaries.
77
- *
78
- * Used in non-TTY environments with `fallback: 'static'`. No animation,
79
- * no ANSI codes. Emits text on start and terminal events only.
80
- *
81
- * Terminal methods are idempotent — calling after stop is a no-op.
82
- *
83
- * @internal
84
- */
85
- class StaticSpinnerHandle implements SpinnerHandle {
86
- /** Whether the handle has been stopped (terminal state reached). */
87
- private stopped = false;
88
-
89
- constructor(
90
- text: string,
91
- private readonly write: WriteFn,
92
- ) {
93
- writeLine(write, text);
94
- bindMethods(this);
95
- }
96
-
97
- update(_text: string): void {
98
- // Static mode — no update output (no terminal to overwrite).
99
- }
100
-
101
- succeed(text?: string): void {
102
- if (this.stopped) return;
103
- this.stopped = true;
104
- if (text !== undefined) {
105
- writeLine(this.write, text);
106
- }
107
- }
108
-
109
- fail(text?: string): void {
110
- if (this.stopped) return;
111
- this.stopped = true;
112
- if (text !== undefined) {
113
- writeLine(this.write, text);
114
- }
115
- }
116
-
117
- stop(): void {
118
- if (this.stopped) return;
119
- this.stopped = true;
120
- }
121
-
122
- async wrap<T>(
123
- promise: Promise<T>,
124
- options?: { readonly succeed?: string; readonly fail?: string },
125
- ): Promise<T> {
126
- try {
127
- const value = await promise;
128
- this.succeed(options?.succeed);
129
- return value;
130
- } catch (error: unknown) {
131
- this.fail(options?.fail);
132
- throw error;
133
- }
134
- }
135
- }
136
-
137
- /**
138
- * Static progress handle — emits plain text at lifecycle boundaries.
139
- *
140
- * Used in non-TTY environments with `fallback: 'static'`. No animation,
141
- * no progress bar rendering. Emits label on start, text on done/fail.
142
- *
143
- * Terminal methods are idempotent — calling after stop is a no-op.
144
- *
145
- * @internal
146
- */
147
- class StaticProgressHandle implements ProgressHandle {
148
- /** Whether the handle has been stopped (terminal state reached). */
149
- private stopped = false;
150
-
151
- constructor(
152
- label: string | undefined,
153
- private readonly write: WriteFn,
154
- ) {
155
- if (label !== undefined) {
156
- writeLine(write, label);
157
- }
158
- bindMethods(this);
159
- }
160
-
161
- increment(_n?: number): void {
162
- // Static mode — no incremental output.
163
- }
164
-
165
- update(_value: number): void {
166
- // Static mode — no incremental output.
167
- }
168
-
169
- done(text?: string): void {
170
- if (this.stopped) return;
171
- this.stopped = true;
172
- if (text !== undefined) {
173
- writeLine(this.write, text);
174
- }
175
- }
176
-
177
- fail(text?: string): void {
178
- if (this.stopped) return;
179
- this.stopped = true;
180
- if (text !== undefined) {
181
- writeLine(this.write, text);
182
- }
183
- }
184
- }
185
-
186
- // --- ANSI escape sequences
187
-
188
- /** Hide the terminal cursor. @internal */
189
- const HIDE_CURSOR = '\x1b[?25l';
190
-
191
- /** Show the terminal cursor. @internal */
192
- const SHOW_CURSOR = '\x1b[?25h';
193
-
194
- /** Erase the entire current line. @internal */
195
- const ERASE_LINE = '\x1b[2K';
196
-
197
- /**
198
- * Braille-dot spinner frames.
199
- *
200
- * Provides smooth animation across most terminal emulators. The 10-frame
201
- * sequence completes a full rotation of the braille clock pattern.
202
- *
203
- * @internal
204
- */
205
- const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] as const;
206
-
207
- /** @internal */ const SPINNER_INTERVAL_MS = 80;
208
-
209
- /** Success symbol for terminal output. @internal */
210
- const CHECK = '✓';
211
-
212
- /** Failure symbol for terminal output. @internal */
213
- const CROSS = '✗';
214
-
215
- /** Filled segment of a progress bar. @internal */
216
- const BAR_FILLED = '█';
217
-
218
- /** Empty segment of a progress bar. @internal */
219
- const BAR_EMPTY = '░';
220
-
221
- /** Width of the progress bar in characters. @internal */
222
- const BAR_WIDTH = 20;
223
-
224
- /** Pulse width for indeterminate progress (number of filled segments). @internal */
225
- const PULSE_WIDTH = 3;
226
-
227
- /** Animation interval for indeterminate progress pulse (ms). @internal */
228
- const PULSE_INTERVAL_MS = 80;
229
-
230
- // --- TTY activity handles — animated mode (isTTY && !jsonMode)
231
-
232
- /**
233
- * TTY spinner handle — animated braille frames with ANSI line overwrite.
234
- *
235
- * Used in TTY environments (non-JSON mode). Renders a spinning indicator
236
- * with configurable text, using `\r` + erase-line to overwrite in place.
237
- * Hides the cursor during animation and restores it on any terminal method.
238
- *
239
- * Terminal methods (`succeed`, `fail`, `stop`) are idempotent — calling any
240
- * of them after the handle is already stopped is a no-op.
241
- *
242
- * **Note:** If the process exits abnormally (e.g. `SIGKILL`) while a spinner
243
- * is active, the cursor may remain hidden. Use {@link SpinnerHandle.wrap} to
244
- * ensure cleanup on both success and failure paths.
245
- *
246
- * @internal
247
- */
248
- class TTYSpinnerHandle implements SpinnerHandle {
249
- /** Whether the handle has been stopped (terminal state reached). */
250
- private stopped = false;
251
- /** Animation timer handle — cleared on any terminal method. */
252
- private timer: unknown;
253
- /** Current position in the braille frame sequence. */
254
- private frameIndex = 0;
255
- /** Current spinner text (mutable via `update()`). */
256
- private text: string;
257
-
258
- constructor(
259
- text: string,
260
- private readonly write: WriteFn,
261
- ) {
262
- this.text = text;
263
- write(HIDE_CURSOR);
264
- this.render();
265
- this.timer = setInterval(() => {
266
- this.frameIndex = (this.frameIndex + 1) % SPINNER_FRAMES.length;
267
- this.render();
268
- }, SPINNER_INTERVAL_MS);
269
- bindMethods(this);
270
- }
271
-
272
- /** Render the current frame + text, overwriting the current line. */
273
- private render(): void {
274
- const frame = SPINNER_FRAMES[this.frameIndex] ?? SPINNER_FRAMES[0];
275
- this.write(`\r${ERASE_LINE}${frame} ${this.text}`);
276
- }
277
-
278
- /** Clear the animation timer, erase the line, and restore the cursor. */
279
- private cleanup(): void {
280
- if (this.timer !== undefined) {
281
- clearInterval(this.timer);
282
- this.timer = undefined;
283
- }
284
- this.write(`\r${ERASE_LINE}${SHOW_CURSOR}`);
285
- }
286
-
287
- update(text: string): void {
288
- if (this.stopped) return;
289
- this.text = text;
290
- // Render immediately so the text change is visible before the next frame.
291
- this.render();
292
- }
293
-
294
- succeed(text?: string): void {
295
- if (this.stopped) return;
296
- this.stopped = true;
297
- this.cleanup();
298
- if (text !== undefined) {
299
- writeLine(this.write, `${CHECK} ${text}`);
300
- }
301
- }
302
-
303
- fail(text?: string): void {
304
- if (this.stopped) return;
305
- this.stopped = true;
306
- this.cleanup();
307
- if (text !== undefined) {
308
- writeLine(this.write, `${CROSS} ${text}`);
309
- }
310
- }
311
-
312
- stop(): void {
313
- if (this.stopped) return;
314
- this.stopped = true;
315
- this.cleanup();
316
- }
317
-
318
- async wrap<T>(
319
- promise: Promise<T>,
320
- options?: { readonly succeed?: string; readonly fail?: string },
321
- ): Promise<T> {
322
- try {
323
- const value = await promise;
324
- this.succeed(options?.succeed);
325
- return value;
326
- } catch (error: unknown) {
327
- this.fail(options?.fail);
328
- throw error;
329
- }
330
- }
331
- }
332
-
333
- /**
334
- * TTY progress handle — bar rendering with ANSI line overwrite.
335
- *
336
- * Supports two modes:
337
- * - **Determinate** (`total` provided) — renders `[████░░░░░░] 40% label`,
338
- * re-rendered on each `increment()` / `update()` call. No timer.
339
- * - **Indeterminate** (`total` omitted) — renders a pulsing highlight that
340
- * bounces across the bar via `setInterval`.
341
- *
342
- * Terminal methods (`done`, `fail`) are idempotent — calling any of them
343
- * after the handle is already stopped is a no-op.
344
- *
345
- * @internal
346
- */
347
- class TTYProgressHandle implements ProgressHandle {
348
- /** Whether the handle has been stopped (terminal state reached). */
349
- private stopped = false;
350
- /** Animation timer for indeterminate mode — `undefined` in determinate mode. */
351
- private timer: unknown;
352
- /** Current progress value (units completed). */
353
- private current = 0;
354
- /** Total units of work (`undefined` = indeterminate mode). */
355
- private readonly total: number | undefined;
356
- /** Label displayed alongside the bar. */
357
- private readonly label: string;
358
- /** Pulse position for indeterminate animation (bouncing highlight). */
359
- private pulsePos = 0;
360
- /** Pulse direction: 1 = forward, -1 = backward (for bounce). */
361
- private pulseDir: 1 | -1 = 1;
362
-
363
- constructor(
364
- opts: ProgressOptions,
365
- private readonly write: WriteFn,
366
- ) {
367
- this.total = opts.total;
368
- this.label = opts.label ?? '';
369
- write(HIDE_CURSOR);
370
- this.render();
371
-
372
- // Only start animation timer for indeterminate mode.
373
- if (this.total === undefined) {
374
- this.timer = setInterval(() => {
375
- this.advancePulse();
376
- this.render();
377
- }, PULSE_INTERVAL_MS);
378
- }
379
- bindMethods(this);
380
- }
381
-
382
- /** Advance the indeterminate pulse position with bounce logic. */
383
- private advancePulse(): void {
384
- this.pulsePos += this.pulseDir;
385
- // Bounce at boundaries: when the rightmost lit segment would exceed the bar.
386
- if (this.pulsePos + PULSE_WIDTH > BAR_WIDTH) {
387
- this.pulseDir = -1;
388
- this.pulsePos = BAR_WIDTH - PULSE_WIDTH;
389
- } else if (this.pulsePos < 0) {
390
- this.pulseDir = 1;
391
- this.pulsePos = 0;
392
- }
393
- }
394
-
395
- /** Render the progress bar, overwriting the current line. */
396
- private render(): void {
397
- const bar =
398
- this.total !== undefined ? this.renderDeterminate(this.total) : this.renderIndeterminate();
399
- const suffix = this.label.length > 0 ? ` ${this.label}` : '';
400
- this.write(`\r${ERASE_LINE}${bar}${suffix}`);
401
- }
402
-
403
- /** Render a determinate bar: `[████░░░░░░] 40%`. */
404
- private renderDeterminate(total: number): string {
405
- const ratio = total > 0 ? Math.max(0, Math.min(this.current / total, 1)) : 0;
406
- const filled = Math.round(ratio * BAR_WIDTH);
407
- const empty = BAR_WIDTH - filled;
408
- const pct = Math.round(ratio * 100);
409
- return `[${BAR_FILLED.repeat(filled)}${BAR_EMPTY.repeat(empty)}] ${String(pct)}%`;
410
- }
411
-
412
- /** Render an indeterminate bar: pulsing highlight bouncing across empty segments. */
413
- private renderIndeterminate(): string {
414
- let bar = '';
415
- for (let i = 0; i < BAR_WIDTH; i++) {
416
- bar += i >= this.pulsePos && i < this.pulsePos + PULSE_WIDTH ? BAR_FILLED : BAR_EMPTY;
417
- }
418
- return `[${bar}]`;
419
- }
420
-
421
- /** Clear the animation timer, erase the line, and restore the cursor. */
422
- private cleanup(): void {
423
- if (this.timer !== undefined) {
424
- clearInterval(this.timer);
425
- this.timer = undefined;
426
- }
427
- this.write(`\r${ERASE_LINE}${SHOW_CURSOR}`);
428
- }
429
-
430
- increment(n?: number): void {
431
- if (this.stopped) return;
432
- this.current += n ?? 1;
433
- this.render();
434
- }
435
-
436
- update(value: number): void {
437
- if (this.stopped) return;
438
- this.current = value;
439
- this.render();
440
- }
441
-
442
- done(text?: string): void {
443
- if (this.stopped) return;
444
- this.stopped = true;
445
- this.cleanup();
446
- if (text !== undefined) {
447
- writeLine(this.write, `${CHECK} ${text}`);
448
- }
449
- }
450
-
451
- fail(text?: string): void {
452
- if (this.stopped) return;
453
- this.stopped = true;
454
- this.cleanup();
455
- if (text !== undefined) {
456
- writeLine(this.write, `${CROSS} ${text}`);
457
- }
458
- }
459
- }
460
-
461
- // --- Capture activity handles — testkit recording mode
462
-
463
- /**
464
- * Spinner handle that records lifecycle events to a shared `ActivityEvent[]`.
465
- *
466
- * Used by `createCaptureOutput()` so testkit can assert on spinner behaviour
467
- * without polluting stdout/stderr arrays. Terminal methods are idempotent.
468
- *
469
- * @internal
470
- */
471
- class CaptureSpinnerHandle implements SpinnerHandle {
472
- /** Whether the handle has been stopped (terminal state reached). */
473
- private stopped = false;
474
-
475
- constructor(
476
- text: string,
477
- private readonly events: ActivityEvent[],
478
- ) {
479
- events.push({ type: 'spinner:start', text });
480
- bindMethods(this);
481
- }
482
-
483
- update(text: string): void {
484
- if (this.stopped) return;
485
- this.events.push({ type: 'spinner:update', text });
486
- }
487
-
488
- succeed(text?: string): void {
489
- if (this.stopped) return;
490
- this.stopped = true;
491
- this.events.push({ type: 'spinner:succeed', text: text ?? '' });
492
- }
493
-
494
- fail(text?: string): void {
495
- if (this.stopped) return;
496
- this.stopped = true;
497
- this.events.push({ type: 'spinner:fail', text: text ?? '' });
498
- }
499
-
500
- stop(): void {
501
- if (this.stopped) return;
502
- this.stopped = true;
503
- this.events.push({ type: 'spinner:stop' });
504
- }
505
-
506
- async wrap<T>(
507
- promise: Promise<T>,
508
- options?: { readonly succeed?: string; readonly fail?: string },
509
- ): Promise<T> {
510
- try {
511
- const value = await promise;
512
- this.succeed(options?.succeed);
513
- return value;
514
- } catch (error: unknown) {
515
- this.fail(options?.fail);
516
- throw error;
517
- }
518
- }
519
- }
520
-
521
- /**
522
- * Progress handle that records lifecycle events to a shared `ActivityEvent[]`.
523
- *
524
- * Used by `createCaptureOutput()` so testkit can assert on progress behaviour
525
- * without polluting stdout/stderr arrays. Terminal methods are idempotent.
526
- *
527
- * @internal
528
- */
529
- class CaptureProgressHandle implements ProgressHandle {
530
- /** Whether the handle has been stopped (terminal state reached). */
531
- private stopped = false;
532
-
533
- constructor(
534
- opts: ProgressOptions,
535
- private readonly events: ActivityEvent[],
536
- ) {
537
- events.push({ type: 'progress:start', label: opts.label ?? '', total: opts.total });
538
- bindMethods(this);
539
- }
540
-
541
- increment(n?: number): void {
542
- if (this.stopped) return;
543
- this.events.push({ type: 'progress:increment', delta: n ?? 1 });
544
- }
545
-
546
- update(value: number): void {
547
- if (this.stopped) return;
548
- this.events.push({ type: 'progress:update', value });
549
- }
550
-
551
- done(text?: string): void {
552
- if (this.stopped) return;
553
- this.stopped = true;
554
- this.events.push({ type: 'progress:done', text });
555
- }
556
-
557
- fail(text?: string): void {
558
- if (this.stopped) return;
559
- this.stopped = true;
560
- this.events.push({ type: 'progress:fail', text });
561
- }
562
- }
563
-
564
- // --- Exports
565
-
566
- export {
567
- CaptureProgressHandle,
568
- CaptureSpinnerHandle,
569
- noopProgressHandle,
570
- noopSpinnerHandle,
571
- StaticProgressHandle,
572
- StaticSpinnerHandle,
573
- TTYProgressHandle,
574
- TTYSpinnerHandle,
575
- };
@@ -1,59 +0,0 @@
1
- /**
2
- * Method-binding helper for value objects handed to consumers.
3
- *
4
- * Output value objects — the `Out` channel and the spinner/progress handles
5
- * — expose methods that read instance state via `this`. Consumers naturally
6
- * detach those methods, either by destructuring
7
- * (`const { log } = out` / `const { succeed, fail } = spinner`) or by passing
8
- * them as callbacks (`promise.finally(spinner.stop)`). Detached, an unbound
9
- * prototype method loses its `this` and crashes on the first field access.
10
- *
11
- * {@linkcode bindMethods} eliminates that footgun by installing instance-bound
12
- * copies of every method, so the ergonomic usage is also the correct one.
13
- *
14
- * @module dreamcli/core/output/bind
15
- * @internal
16
- */
17
-
18
- /**
19
- * Bind every method on an instance's prototype chain to the instance.
20
- *
21
- * Walks the prototype chain up to (but excluding) `Object.prototype`, binding
22
- * each method name exactly once — the most-derived definition wins, so subclass
23
- * overrides (e.g. `CaptureOutputChannel.spinner`) are preserved rather than
24
- * clobbered by a base-class version. Bound methods are installed as
25
- * non-enumerable own properties, mirroring the enumerability of the prototype
26
- * methods they shadow. Getters and non-function members are left untouched.
27
- *
28
- * Call this once, last, in a class constructor. Because it reads the live
29
- * prototype rather than a hand-maintained name list, methods added later are
30
- * bound automatically — the binding can't silently fall out of date.
31
- *
32
- * @param instance - The object whose methods should be bound to it.
33
- * @internal
34
- */
35
- function bindMethods(instance: object): void {
36
- const bound = instance as Record<PropertyKey, unknown>;
37
- const seen = new Set<string>();
38
- let proto: object | null = Object.getPrototypeOf(instance);
39
-
40
- while (proto !== null && proto !== Object.prototype) {
41
- for (const name of Object.getOwnPropertyNames(proto)) {
42
- if (name === 'constructor' || seen.has(name)) continue;
43
- seen.add(name);
44
-
45
- const descriptor = Object.getOwnPropertyDescriptor(proto, name);
46
- if (descriptor === undefined || typeof descriptor.value !== 'function') continue;
47
-
48
- Object.defineProperty(bound, name, {
49
- value: descriptor.value.bind(instance),
50
- writable: true,
51
- enumerable: false,
52
- configurable: true,
53
- });
54
- }
55
- proto = Object.getPrototypeOf(proto);
56
- }
57
- }
58
-
59
- export { bindMethods };