-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathspinner.ts
269 lines (252 loc) · 6.26 KB
/
spinner.ts
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const encoder = new TextEncoder();
const LINE_CLEAR = encoder.encode("\r\u001b[K"); // From cli/prompt_secret.ts
const COLOR_RESET = "\u001b[0m";
const DEFAULT_INTERVAL = 75;
const DEFAULT_SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
/**
* This is a hack to allow us to use the same type for both the color name and
* an ANSI escape code.
*
* @see {@link https://github.com/microsoft/TypeScript/issues/29729#issuecomment-460346421}
*
* @internal
*/
// deno-lint-ignore ban-types
export type Ansi = string & {};
/**
* Color options for {@linkcode SpinnerOptions.color}.
*
* > [!WARNING]
* > **UNSTABLE**: New API, yet to be vetted.
*
* @experimental
*/
export type Color =
| "black"
| "red"
| "green"
| "yellow"
| "blue"
| "magenta"
| "cyan"
| "white"
| "gray"
| Ansi;
const COLORS: Record<Color, string> = {
black: "\u001b[30m",
red: "\u001b[31m",
green: "\u001b[32m",
yellow: "\u001b[33m",
blue: "\u001b[34m",
magenta: "\u001b[35m",
cyan: "\u001b[36m",
white: "\u001b[37m",
gray: "\u001b[90m",
};
/**
* Options for {@linkcode Spinner}.
*
* > [!WARNING]
* > **UNSTABLE**: New API, yet to be vetted.
*
* @experimental
*/
export interface SpinnerOptions {
/**
* The sequence of characters to be iterated through for animation.
*
* @default {["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]}
*/
spinner?: string[];
/**
* The message to display next to the spinner. This can be changed while the
* spinner is active.
*/
message?: string;
/**
* The time between each frame of the spinner in milliseconds.
*
* @default {75}
*/
interval?: number;
/**
* The color of the spinner. Defaults to the default terminal color.
* This can be changed while the spinner is active.
*/
color?: Color;
}
/**
* A spinner that can be used to indicate that something is loading.
*
* > [!WARNING]
* > **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts no-eval
* import { Spinner } from "@std/cli/spinner";
*
* const spinner = new Spinner({ message: "Loading...", color: "yellow" });
* spinner.start();
*
* setTimeout(() => {
* spinner.stop();
* console.log("Finished loading!");
* }, 3_000);
* ```
*
* @experimental
*/
export class Spinner {
#spinner: string[];
/**
* The message to display next to the spinner.
* This can be changed while the spinner is active.
*
* @example Usage
* ```ts no-eval
* import { Spinner } from "@std/cli/spinner";
*
* const spinner = new Spinner({ message: "Working..." });
* spinner.start();
*
* for (let step = 0; step < 5; step++) {
* // do some work
* await new Promise((resolve) => setTimeout(resolve, 1000));
*
* spinner.message = `Finished Step #${step}`;
* }
*
* spinner.stop();
* console.log("Done!");
* ```
*/
message: string;
#interval: number;
#color?: Color;
#intervalId: number | undefined;
#active = false;
/**
* Creates a new spinner.
*
* @param options Options for the spinner.
*
* @example Usage
* ```ts no-assert
* import { Spinner } from "@std/cli/spinner";
*
* const spinner = new Spinner({ message: "Loading..." });
* spinner.stop();
* ```
*/
constructor(options?: SpinnerOptions) {
const {
spinner = DEFAULT_SPINNER,
message = "",
interval = DEFAULT_INTERVAL,
color,
} = options ?? {};
this.#spinner = spinner;
this.message = message;
this.#interval = interval;
this.color = color;
}
/**
* Set the color of the spinner. This defaults to the default terminal color.
* This can be changed while the spinner is active.
*
* Providing `undefined` will use the default terminal color.
*
* @param value Color to set.
*
* @example Usage
* ```ts no-eval
* import { Spinner } from "@std/cli/spinner";
*
* const spinner = new Spinner({ message: "Loading...", color: "yellow" });
* spinner.start();
*
* // do some work
* await new Promise((resolve) => setTimeout(resolve, 1000));
*
* spinner.color = "magenta";
* ```
*/
set color(value: Color | undefined) {
this.#color = value ? COLORS[value] : undefined;
}
/**
* Get the current color of the spinner.
*
* @example Usage
* ```ts no-assert
* import { Spinner } from "@std/cli/spinner";
*
* const spinner = new Spinner({ message: "Loading", color: "blue" });
*
* spinner.color; // Blue ANSI escape sequence
* ```
* @returns The color of the spinner or `undefined` if it's using the terminal default.
*/
get color(): Color | undefined {
return this.#color;
}
/**
* Starts the spinner.
*
* @example Usage
* ```ts no-eval
* import { Spinner } from "@std/cli/spinner";
*
* const spinner = new Spinner({ message: "Loading..." });
* spinner.start();
* ```
*/
start() {
if (this.#active || Deno.stdout.writable.locked) {
return;
}
this.#active = true;
let i = 0;
const noColor = Deno.noColor;
// Updates the spinner after the given interval.
const updateFrame = () => {
const color = this.#color ?? "";
const frame = encoder.encode(
noColor
? this.#spinner[i] + " " + this.message
: color + this.#spinner[i] + COLOR_RESET + " " + this.message,
);
// call writeSync once to reduce flickering
const writeData = new Uint8Array(LINE_CLEAR.length + frame.length);
writeData.set(LINE_CLEAR);
writeData.set(frame, LINE_CLEAR.length);
Deno.stdout.writeSync(writeData);
i = (i + 1) % this.#spinner.length;
};
this.#intervalId = setInterval(updateFrame, this.#interval);
}
/**
* Stops the spinner.
*
* @example Usage
* ```ts no-eval
* import { Spinner } from "@std/cli/spinner";
*
* const spinner = new Spinner({ message: "Loading..." });
* spinner.start();
*
* setTimeout(() => {
* spinner.stop();
* console.log("Finished loading!");
* }, 3_000);
* ```
*/
stop() {
if (this.#intervalId && this.#active) {
clearInterval(this.#intervalId);
Deno.stdout.writeSync(LINE_CLEAR); // Clear the current line
this.#active = false;
}
}
}