-
Notifications
You must be signed in to change notification settings - Fork 7
/
spinner.ts
135 lines (108 loc) · 3.18 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
import { SPINNERS, SpinnerType } from './spinners.ts';
import { clearCurrentLine, printOnCurrentLine, printNewLine } from './util.ts';
import { red, green, bold, yellow } from 'https://deno.land/std/fmt/colors.ts';
export class Spinner {
private static instance: Spinner;
private currentText: string;
private currentTimer: number | null;
private spinner: any;
private cols: number;
private textEncoder: TextEncoder;
private spinnerType: SpinnerType
private constructor() {
this.spinnerType = 'dots2';
this.spinner = SPINNERS[this.spinnerType];
this.cols = 0;
this.textEncoder = new TextEncoder();
this.currentTimer = null;
this.currentText = '';
}
public static getInstance() {
if (Spinner.instance === undefined) {
return new Spinner();
} else {
return Spinner.instance;
}
}
public setSpinnerType(type: SpinnerType) {
if (SPINNERS[type]) {
this.spinner = SPINNERS[type];
} else {
throw new Error(`unknown spinner type ${type}`);
}
}
public async start(text: string) {
if (this.isRunning()) {
throw new Error(
"Can't start a new spinner while a spinner is already running!"
);
}
this.currentText = text;
let currentFrame = 0;
let totalFrames = this.spinner.frames.length;
this.currentTimer = setInterval(async () => {
let message = `${this.spinner.frames[currentFrame]} ${this.currentText}`;
let lastMessage;
if (lastMessage !== message) {
await clearCurrentLine(this.textEncoder, this.cols);
}
await this.print(message);
lastMessage = message;
currentFrame = (currentFrame + 1) % totalFrames;
}, this.spinner.interval);
}
public async setText(text: string) {
this.currentText = text;
}
public async stop() {
this.stopSpinner();
await clearCurrentLine(this.textEncoder, this.cols);
await this.printNewLine();
}
private async stopSpinner(text?: string) {
if (this.currentTimer) {
clearInterval(this.currentTimer);
}
if (text) {
await this.print(text);
}
this.currentTimer = null;
}
private async print(text: string) {
if (text.length > this.cols) {
this.cols = text.length;
}
await printOnCurrentLine(this.textEncoder, this.cols, text);
}
private async printNewLine() {
await printNewLine(this.textEncoder, this.cols);
}
public isRunning() {
if (this.currentTimer !== null) {
return true;
} else {
return false;
}
}
private async stopSpinnerWithStatus(status: string, text?: string) {
if (text) {
this.setText(text);
}
let message = `${status} ${this.currentText}`;
await this.stopSpinner(message);
await this.print(message);
await this.printNewLine();
}
public async succeed(text?: string) {
await this.stopSpinnerWithStatus(bold(green('√')), text);
}
public async fail(text?: string) {
await this.stopSpinnerWithStatus(bold(red('×')), text);
}
public async warn(text?: string) {
await this.stopSpinnerWithStatus(bold(yellow('!!')), text);
}
public async info(text?: string) {
await this.stopSpinnerWithStatus(bold(yellow('i')), text);
}
}