-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-spinner-class.ts
More file actions
474 lines (436 loc) · 15.9 KB
/
create-spinner-class.ts
File metadata and controls
474 lines (436 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* @file Builds the lazy-init Socket `Spinner` class that extends the live
* `yocto-spinner` constructor. The class graph is constructed inside
* `createSpinnerClass()` so the `super()` call binds against the runtime
* `YoctoSpinner` constructor passed in by the factory; keeping it here
* (rather than at module scope) preserves the spinner's lazy-init guarantee
* while splitting the bulk of the class body out of the factory module.
*/
import type { ColorRgb, ColorValue } from '../colors/types'
import { isRgbTuple, toRgb } from '../colors/convert'
import { getAbortSignal } from '../process/abort'
import {
incLogCallCountSymbol,
lastWasBlankSymbol,
LOG_SYMBOLS,
} from '../logger/symbols'
import { ArrayPrototypeAt, ArrayPrototypeSlice } from '../primordials/array'
import { MathMax } from '../primordials/math'
import { ObjectDefineProperties } from '../primordials/object'
import { isBlankString } from '../strings/predicates'
import { stringWidth } from '../strings/width'
import { desc, formatProgress, normalizeText } from './format'
import {
applyShimmer,
parseShimmerOption,
resolveSpinnerColorRgb,
} from './spinner-internals'
import {
getSavedShimmerSymbol,
getShimmerSymbol,
installShimmerMethods,
setSavedShimmerSymbol,
setShimmerSymbol,
updateTextSymbol,
} from './spinner-shimmer-methods'
import {
applyStatusSymbol,
installStatusMethods,
showStatusSymbol,
} from './spinner-status-methods'
import type {
ProgressInfo,
ShimmerInfo,
SpinnerInstance,
SpinnerOptions,
SymbolType,
} from './types'
export type SpinnerCtorType = {
new (options?: SpinnerOptions | undefined): SpinnerInstance
}
export type YoctoSpinnerConstructor = new (...args: unknown[]) => {
// `color` and `text` are typed as accessors (get/set pairs) rather than
// plain properties so SpinnerClass can override them with its own
// RGB-narrowing accessor / getter-setter method overloads — TypeScript
// forbids overriding a base *property* with a subclass *accessor*
// (TS2611), but allows accessor-over-accessor.
get color(): ColorRgb | ColorValue
set color(value: ColorRgb | ColorValue)
// `text` is intentionally NOT modeled as a named member: yocto-spinner
// exposes it as a property, but SpinnerClass owns the public `text()`
// getter/setter method, and TypeScript forbids overriding a base property
// with a subclass method. The base `text` is reached through the index
// signature, so internal writes use `super['text']` (bracket access).
isSpinning: boolean
[key: string]: unknown
}
export type SpinnerLogger = {
error: (...args: unknown[]) => unknown
log: (...args: unknown[]) => unknown
[lastWasBlankSymbol]: (value: boolean) => unknown
[incLogCallCountSymbol]: () => unknown
}
/**
* Build the Socket `Spinner` class as a subclass of the live `yocto-spinner`
* constructor. Passing the parent class in keeps the `super()` binding against
* the runtime constructor while letting the bulk of the class body live outside
* the factory module.
*
* @param YoctoSpinnerClass - Runtime `yocto-spinner` constructor to extend.
* @param logger - Default logger used for status output.
*
* @returns The constructed Socket spinner constructor.
*/
export function createSpinnerClass(
YoctoSpinnerClass: YoctoSpinnerConstructor,
logger: SpinnerLogger,
): SpinnerCtorType {
const SpinnerCtor = class SpinnerClass extends YoctoSpinnerClass {
declare isSpinning: boolean
#baseText: string = ''
#indentation: string = ''
#progress?: ProgressInfo | undefined
#shimmer?: ShimmerInfo | undefined
#shimmerSavedConfig?: ShimmerInfo | undefined
constructor(ctorOptions?: SpinnerOptions | undefined) {
const opts = { __proto__: null, ...ctorOptions } as SpinnerOptions
const spinnerColorRgb = resolveSpinnerColorRgb(opts)
// Parse shimmer config - can be object or direction string.
const shimmerInfo = parseShimmerOption(opts.shimmer)
// eslint-disable-next-line constructor-super
super({
signal: getAbortSignal(),
...opts,
// Pass RGB color directly to yocto-spinner (it now supports RGB).
color: spinnerColorRgb,
// onRenderFrame callback provides full control over frame + text layout.
// Calculates spacing based on frame width to prevent text jumping.
onRenderFrame: (
frame: string,
text: string,
applyColor: (text: string) => string,
) => {
const width = stringWidth(frame)
// Narrow frames (width 1) get 2 spaces, wide frames (width 2) get 1 space.
// Total width is consistent: 3 characters (frame + spacing) before text.
const spacing = width === 1 ? ' ' : ' '
return frame ? `${applyColor(frame)}${spacing}${text}` : text
},
// onFrameUpdate callback is called by yocto-spinner whenever a frame advances.
// This ensures shimmer updates are perfectly synchronized with animation beats.
onFrameUpdate: shimmerInfo
? () => {
// Update parent's text without triggering render.
// Parent's #skipRender flag prevents nested render calls.
// Only update if we have base text to avoid blank frames.
if (this.#baseText) {
super['text'] = this.#buildDisplayText()
}
}
: undefined,
})
this.#shimmer = shimmerInfo
this.#shimmerSavedConfig = shimmerInfo
}
// Override color getter to ensure it's always RGB.
override get color(): ColorRgb {
const value = super.color
return isRgbTuple(value) ? value : toRgb(value)
}
// Override color setter to always convert to RGB before passing to yocto-spinner.
override set color(value: ColorValue | ColorRgb) {
super.color = isRgbTuple(value) ? value : toRgb(value)
}
/**
* Apply a yocto-spinner method and update logger state. Handles text
* normalization, extra arguments, and logger tracking. Exposed under
* `applyStatusSymbol` so the status methods installed from
* `spinner-status-methods.ts` can drive it.
*/
[applyStatusSymbol](methodName: string, args: unknown[]) {
let extras: unknown[]
let text = ArrayPrototypeAt(args, 0)
if (typeof text === 'string') {
extras = ArrayPrototypeSlice(args, 1)
} else {
extras = args
text = ''
}
const wasSpinning = this.isSpinning
const normalized = normalizeText(text)
// Dynamic dispatch to a base yocto-spinner method; the base type's
// index signature is `unknown`, so cast to a callable locally.
const superMethod = super[methodName] as unknown as (
...args: unknown[]
) => unknown
if (methodName === 'stop' && !normalized) {
superMethod.call(this)
} else {
superMethod.call(this, normalized)
}
if (methodName === 'stop') {
if (wasSpinning && normalized) {
logger[lastWasBlankSymbol](isBlankString(normalized))
logger[incLogCallCountSymbol]()
}
} else {
logger[lastWasBlankSymbol](false)
logger[incLogCallCountSymbol]()
}
/* c8 ignore start - extras-empty no-op arm fires when log called without extras */
if (extras.length) {
logger.log(...extras)
logger[lastWasBlankSymbol](false)
}
/* c8 ignore stop */
return this
}
/**
* Build the complete display text with progress, shimmer, and indentation.
* Combines base text, progress bar, shimmer effects, and indentation.
*
* @private
*/
#buildDisplayText() {
let displayText = this.#baseText
/* c8 ignore start - progress + shimmer paths fire only when caller seeded those configs */
if (this.#progress) {
const progressText = formatProgress(this.#progress)
displayText = displayText
? `${displayText} ${progressText}`
: progressText
}
if (displayText && this.#shimmer) {
displayText = applyShimmer(displayText, this.#shimmer, this.color)
}
// Indentation arm fires only when caller calls indent().
if (this.#indentation && displayText) {
displayText = this.#indentation + displayText
}
/* c8 ignore stop */
return displayText
}
/**
* Show a status message without stopping the spinner. Outputs the symbol
* and message to stderr, then continues spinning. Exposed under
* `showStatusSymbol` so the status methods installed from
* `spinner-status-methods.ts` can drive it.
*/
[showStatusSymbol](symbolType: SymbolType, args: unknown[]) {
let text = ArrayPrototypeAt(args, 0)
let extras: unknown[]
if (typeof text === 'string') {
extras = ArrayPrototypeSlice(args, 1)
} else {
extras = args
text = ''
}
// Note: Status messages always go to stderr.
logger.error(`${LOG_SYMBOLS[symbolType]} ${text}`, ...extras)
return this
}
/**
* Update the spinner's displayed text. Rebuilds display text and triggers
* render.
*
* @private
*/
#updateSpinnerText() {
// Call the parent class's text setter, which triggers render.
super['text'] = this.#buildDisplayText()
}
// Expose the shimmer state + render helper under well-known symbols so the
// shimmer methods installed from `spinner-shimmer-methods.ts` can read and
// write the private fields without crossing the private-field boundary.
[getShimmerSymbol]() {
return this.#shimmer
}
[setShimmerSymbol](value: ShimmerInfo | undefined) {
this.#shimmer = value
}
[getSavedShimmerSymbol]() {
return this.#shimmerSavedConfig
}
[setSavedShimmerSymbol](value: ShimmerInfo | undefined) {
this.#shimmerSavedConfig = value
}
[updateTextSymbol]() {
this.#updateSpinnerText()
}
/**
* Decrease indentation level by removing spaces from the left. Pass 0 to
* reset indentation to zero completely.
*
* @default spaces=2
*
* @param spaces - Number of spaces to remove.
*
* @returns This spinner for chaining
*/
dedent(spaces?: number | undefined) {
// Pass 0 to reset indentation
if (spaces === 0) {
this.#indentation = ''
} else {
const amount = spaces ?? 2
const newLength = MathMax(0, this.#indentation.length - amount)
this.#indentation = this.#indentation.slice(0, newLength)
}
this.#updateSpinnerText()
return this
}
/**
* Increase indentation level by adding spaces to the left. Pass 0 to reset
* indentation to zero completely.
*
* @default spaces=2
*
* @param spaces - Number of spaces to add.
*
* @returns This spinner for chaining
*/
indent(spaces?: number | undefined) {
/* c8 ignore start - spaces===0 fires when caller passes 0; else-branch + default fire for omitted/non-zero */
if (spaces === 0) {
this.#indentation = ''
} else {
const amount = spaces ?? 2
this.#indentation += ' '.repeat(amount)
}
/* c8 ignore stop */
this.#updateSpinnerText()
return this
}
/**
* Update progress information displayed with the spinner. Shows a progress
* bar with percentage and optional unit label.
*
* @param current - Current progress value.
* @param total - Total/maximum progress value.
* @param unit - Optional unit label (e.g., 'files', 'items')
*
* @returns This spinner for chaining
*/
progress = (current: number, total: number, unit?: string | undefined) => {
this.#progress = {
__proto__: null,
current,
total,
...(unit ? { unit } : {}),
} as ProgressInfo
this.#updateSpinnerText()
return this
}
/**
* Increment progress by a specified amount. Updates the progress bar
* displayed with the spinner. Clamps the result between 0 and the total
* value.
*
* @default amount=1
*
* @param amount - Amount to increment by.
*
* @returns This spinner for chaining
*/
progressStep(amount: number = 1) {
/* c8 ignore start - no-progress no-op fires before progress() seed; unit-spread arm fires when seeded with unit */
if (this.#progress) {
const newCurrent = this.#progress.current + amount
this.#progress = {
__proto__: null,
current: MathMax(0, Math.min(newCurrent, this.#progress.total)),
total: this.#progress.total,
...(this.#progress.unit ? { unit: this.#progress.unit } : {}),
} as ProgressInfo
this.#updateSpinnerText()
}
return this
/* c8 ignore stop */
}
/**
* Start the spinner animation with optional text. Begins displaying the
* animated spinner on stderr.
*
* @param text - Optional text to display with the spinner.
*
* @returns This spinner for chaining
*/
start(...args: unknown[]) {
/* c8 ignore start - args-length and normalized-falsy arms exercised across calls; some test paths skip both */
if (args.length) {
const text = ArrayPrototypeAt(args, 0)
const normalized = normalizeText(text)
if (!normalized) {
this.#baseText = ''
super['text'] = ''
} else {
this.#baseText = normalized
}
}
/* c8 ignore stop */
this.#updateSpinnerText()
return this[applyStatusSymbol]('start', [])
}
/**
* Stop the spinner animation and clear internal state. Auto-clears the
* spinner line via yocto-spinner.stop(). Resets progress, shimmer, and text
* state.
*
* @param text - Optional final text to display after stopping.
*
* @returns This spinner for chaining
*/
stop(...args: unknown[]) {
// Clear internal state.
this.#baseText = ''
this.#progress = undefined
// Reset shimmer animation state.
this.#shimmer = undefined
// Call parent stop (clears screen, sets isSpinning = false).
const result = this[applyStatusSymbol]('stop', args)
return result
}
/**
* Get or set the spinner text. When called with no arguments, returns the
* current base text. When called with text, updates the display and returns
* the spinner for chaining.
*
* @param value - Text to display (omit to get current text)
*
* @returns Current text (getter) or this spinner (setter)
*/
text(): string
text(value: string): SpinnerInstance
text(value?: string): string | SpinnerInstance {
// biome-ignore lint/complexity/noArguments: Function overload for getter/setter pattern.
if (arguments.length === 0) {
// Getter: return current base text
return this.#baseText
}
// Setter: update base text and refresh display
this.#baseText = value ?? ''
this.#updateSpinnerText()
return this as unknown as SpinnerInstance
}
} as unknown as SpinnerCtorType
// Install the status-presentation methods (debug/done/fail/info/skip/step/
// substep/success/warn families + log) onto the prototype. They reach the
// class's #apply / #showStatusAndKeepSpinning helpers through the two
// well-known symbols the class exposes.
installStatusMethods(SpinnerCtor.prototype, logger)
// Install the shimmer-configuration methods + shimmerState getter. They reach
// the class's #shimmer / #shimmerSavedConfig state through well-known symbols.
installShimmerMethods(SpinnerCtor.prototype)
// Add aliases.
ObjectDefineProperties(SpinnerCtor.prototype, {
error: desc((SpinnerCtor.prototype as { fail: unknown }).fail),
errorAndStop: desc(
(SpinnerCtor.prototype as { failAndStop: unknown }).failAndStop,
),
warning: desc((SpinnerCtor.prototype as { warn: unknown }).warn),
warningAndStop: desc(
(SpinnerCtor.prototype as { warnAndStop: unknown }).warnAndStop,
),
})
return SpinnerCtor
}