-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
294 lines (259 loc) · 10.4 KB
/
types.ts
File metadata and controls
294 lines (259 loc) · 10.4 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
/**
* @fileoverview Public type surface for `spinner/*` modules — the
* `Spinner` instance shape, configuration option records, progress and
* shimmer state types, plus the `withSpinner*` option records. Pure
* types only; no runtime side effects so this module stays cheap to
* import everywhere.
*/
import type { Writable } from 'node:stream'
import type { ColorInherit, ColorRgb, ColorValue } from '../colors/types'
import type {
Palette,
ShimmerConfig,
ShimmerDirection,
} from '../effects/shimmer'
/**
* Progress tracking information for display in spinner.
* Used by `progress()` and `progressStep()` methods to show animated progress bars.
*/
export type ProgressInfo = {
/** Current progress value */
current: number
/** Total/maximum progress value */
total: number
/** Optional unit label displayed after the progress count (e.g., 'files', 'items') */
unit?: string | undefined
}
/**
* Internal shimmer runtime state. Holds the user-facing config plus a
* monotonic frame counter; the spinner advances `frame` on each animation
* tick and feeds the current frame to the shimmer engine.
*/
export type ShimmerInfo = {
/** User-facing color reference (inherit, explicit value, or palette). */
color: ColorInherit | ColorValue | Palette
/** Current direction (driven by config, snapshotted here for getters). */
direction: ShimmerDirection
/** Steps per frame. */
speed: number
/** Monotonic frame counter — advanced on each animation tick. */
frame: number
}
/**
* Spinner instance for displaying animated loading indicators.
* Provides methods for status updates, progress tracking, and text shimmer effects.
*
* KEY BEHAVIORS:
* - Methods WITHOUT "AndStop" keep the spinner running (e.g., `success()`, `fail()`)
* - Methods WITH "AndStop" auto-clear the spinner line (e.g., `successAndStop()`, `failAndStop()`)
* - Status messages (done, success, fail, info, warn, reason, step, substep) go to stderr
* - Data messages (`log()`) go to stdout
*
* @example
* ```ts
* import { Spinner } from '@socketsecurity/lib/spinner/spinner'
*
* const spinner = Spinner({ text: 'Loading…' })
* spinner.start()
*
* // Show success while continuing to spin
* spinner.success('Step 1 complete')
*
* // Stop the spinner with success message
* spinner.successAndStop('All done!')
* ```
*/
export type Spinner = {
/** Current spinner color as RGB tuple */
color: ColorRgb
/** Current spinner animation style */
spinner: SpinnerStyle
/** Whether spinner is currently animating */
get isSpinning(): boolean
/** Get current shimmer state (enabled/disabled and configuration) */
get shimmerState(): ShimmerInfo | undefined
/** Clear the current line without stopping the spinner */
clear(): Spinner
/** Show debug message without stopping (only if debug mode enabled) */
debug(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show debug message and stop the spinner (only if debug mode enabled) */
debugAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Decrease indentation by specified spaces (default: 2) */
dedent(spaces?: number | undefined): Spinner
/** Disable shimmer effect (preserves config for later re-enable) */
disableShimmer(): Spinner
/** Alias for `success()` - show success without stopping */
done(text?: string | undefined, ...extras: unknown[]): Spinner
/** Alias for `successAndStop()` - show success and stop */
doneAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Enable shimmer effect (restores saved config or uses defaults) */
enableShimmer(): Spinner
/** Alias for `fail()` - show error without stopping */
error(text?: string | undefined, ...extras: unknown[]): Spinner
/** Alias for `failAndStop()` - show error and stop */
errorAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show failure (✗) without stopping the spinner */
fail(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show failure (✗) and stop the spinner, auto-clearing the line */
failAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Increase indentation by specified spaces (default: 2) */
indent(spaces?: number | undefined): Spinner
/** Show info (ℹ) message without stopping the spinner */
info(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show info (ℹ) message and stop the spinner, auto-clearing the line */
infoAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Log to stdout without stopping the spinner */
log(text?: string | undefined, ...extras: unknown[]): Spinner
/** Log and stop the spinner, auto-clearing the line */
logAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Update progress bar with current/total values and optional unit */
progress(current: number, total: number, unit?: string | undefined): Spinner
/** Increment progress by specified amount (default: 1) */
progressStep(amount?: number): Spinner
/** Set complete shimmer configuration */
setShimmer(config: ShimmerConfig): Spinner
/** Show skip (↻) message without stopping the spinner */
skip(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show skip (↻) message and stop the spinner, auto-clearing the line */
skipAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Start spinning with optional text */
start(text?: string | undefined): Spinner
/** Show main step message to stderr without stopping */
step(text?: string | undefined, ...extras: unknown[]): Spinner
/** Stop spinning and clear internal state, auto-clearing the line */
stop(text?: string | undefined): Spinner
/** Stop and show final text without clearing the line */
stopAndPersist(text?: string | undefined): Spinner
/** Show indented substep message to stderr without stopping */
substep(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show success (✓) without stopping the spinner */
success(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show success (✓) and stop the spinner, auto-clearing the line */
successAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
/** Get current spinner text (getter) or set new text (setter) */
text(value: string): Spinner
text(): string
/** Update partial shimmer configuration */
updateShimmer(config: Partial<ShimmerConfig>): Spinner
/** Show warning (⚠) without stopping the spinner */
warn(text?: string | undefined, ...extras: unknown[]): Spinner
/** Show warning (⚠) and stop the spinner, auto-clearing the line */
warnAndStop(text?: string | undefined, ...extras: unknown[]): Spinner
}
/**
* Configuration options for creating a spinner instance.
*/
export type SpinnerOptions = {
/**
* Spinner color as RGB tuple or color name.
* @default [140, 82, 255] Socket purple
*/
readonly color?: ColorValue | undefined
/**
* Shimmer effect configuration or direction string.
* When enabled, text will have an animated shimmer effect.
* @default undefined No shimmer effect
*/
readonly shimmer?: ShimmerConfig | ShimmerDirection | undefined
/**
* Animation style with frames and timing.
* @default 'socket' Custom Socket animation in CLI, minimal in CI
*/
readonly spinner?: SpinnerStyle | undefined
/**
* Abort signal for cancelling the spinner.
* @default getAbortSignal() from process constants
*/
readonly signal?: AbortSignal | undefined
/**
* Output stream for spinner rendering.
* @default process.stderr
*/
readonly stream?: Writable | undefined
/**
* Initial text to display with the spinner.
* @default undefined No initial text
*/
readonly text?: string | undefined
/**
* Theme to use for spinner colors.
* Accepts theme name ('socket', 'sunset', etc.) or Theme object.
* @default Current theme from getTheme()
*/
readonly theme?:
| import('../themes/types').Theme
| import('../themes/themes').ThemeName
| undefined
}
/**
* Animation style definition for spinner frames.
* Defines the visual appearance and timing of the spinner animation.
*/
export type SpinnerStyle = {
/** Array of animation frames (strings to display sequentially) */
readonly frames: string[]
/**
* Milliseconds between frame changes.
* @default 80 Standard frame rate
*/
readonly interval?: number | undefined
}
/**
* Symbol types for status messages.
* Maps to log symbols: fail (✗), info (ℹ), skip (↻), success (✓), warn (⚠).
*/
export type SymbolType = 'fail' | 'info' | 'skip' | 'success' | 'warn'
/**
* Configuration options for `withSpinner()` helper.
* @template T - Return type of the async operation
*/
export type WithSpinnerOptions<T> = {
/** Message to display while the spinner is running */
message: string
/** Async function to execute while spinner is active */
operation: () => Promise<T>
/**
* Optional spinner instance to use.
* If not provided, operation runs without spinner.
*/
spinner?: Spinner | undefined
/**
* Optional spinner options to apply during the operation.
* These options will be pushed when the operation starts and popped when it completes.
* Supports color and shimmer configuration.
*/
withOptions?: Partial<Pick<SpinnerOptions, 'color' | 'shimmer'>> | undefined
}
/**
* Configuration options for `withSpinnerRestore()` helper.
* @template T - Return type of the async operation
*/
export type WithSpinnerRestoreOptions<T> = {
/** Async function to execute while spinner is stopped */
operation: () => Promise<T>
/** Optional spinner instance to restore after operation */
spinner?: Spinner | undefined
/** Whether spinner was spinning before the operation (used to conditionally restart) */
wasSpinning: boolean
}
/**
* Configuration options for `withSpinnerSync()` helper.
* @template T - Return type of the sync operation
*/
export type WithSpinnerSyncOptions<T> = {
/** Message to display while the spinner is running */
message: string
/** Synchronous function to execute while spinner is active */
operation: () => T
/**
* Optional spinner instance to use.
* If not provided, operation runs without spinner.
*/
spinner?: Spinner | undefined
/**
* Optional spinner options to apply during the operation.
* These options will be pushed when the operation starts and popped when it completes.
* Supports color and shimmer configuration.
*/
withOptions?: Partial<Pick<SpinnerOptions, 'color' | 'shimmer'>> | undefined
}