-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathspinner.ts
58 lines (48 loc) · 1.25 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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import ora from 'ora';
import { colors } from './color';
import { isTTY } from './tty';
export class Spinner {
private readonly spinner: ora.Ora;
/** When false, only fail messages will be displayed. */
enabled = true;
readonly #isTTY = isTTY();
constructor(text?: string) {
this.spinner = ora({
text: text === undefined ? undefined : text + '\n',
// The below 2 options are needed because otherwise CTRL+C will be delayed
// when the underlying process is sync.
hideCursor: false,
discardStdin: false,
isEnabled: this.#isTTY,
});
}
set text(text: string) {
this.spinner.text = text;
}
get isSpinning(): boolean {
return this.spinner.isSpinning || !this.#isTTY;
}
succeed(text?: string): void {
if (this.enabled) {
this.spinner.succeed(text);
}
}
fail(text?: string): void {
this.spinner.fail(text && colors.redBright(text));
}
stop(): void {
this.spinner.stop();
}
start(text?: string): void {
if (this.enabled) {
this.spinner.start(text);
}
}
}