-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mod.ts
319 lines (266 loc) Β· 7.17 KB
/
mod.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
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
import * as colors from "@std/fmt/colors";
import * as tty from "@denosaurs/tty";
import spinners from "./spinners.ts";
import { symbols } from "./log_symbols.ts";
export { spinners, symbols };
const encoder = new TextEncoder();
type ColorFunction = (message: string) => string;
const colormap: { [key: string]: ColorFunction } = {
black: colors.black,
red: colors.red,
green: colors.green,
yellow: colors.yellow,
blue: colors.blue,
magenta: colors.magenta,
cyan: colors.cyan,
white: colors.white,
gray: colors.gray,
};
export interface SpinnerAnimation {
interval: number;
frames: string[];
}
export interface SpinnerOptions {
text: string;
prefix?: string;
spinner?: string | SpinnerAnimation;
color?: string | ColorFunction;
hideCursor?: boolean;
indent?: number;
interval?: number;
stream?: tty.SyncStream;
enabled?: boolean;
discardStdin?: boolean;
interceptConsole?: boolean;
}
export interface PersistOptions {
prefix?: string;
symbol?: string;
text?: string;
}
export type Console = typeof globalThis.console;
export function wait(opts: string | SpinnerOptions): Spinner {
if (typeof opts === "string") {
opts = { text: opts };
}
return new Spinner({
text: opts.text,
prefix: opts.prefix ?? "",
color: opts.color ?? colors.cyan,
spinner: opts.spinner ?? "dots",
hideCursor: opts.hideCursor ?? true,
indent: opts.indent ?? 0,
interval: opts.interval ?? 100,
stream: opts.stream ?? Deno.stdout,
enabled: true,
discardStdin: true,
interceptConsole: opts.interceptConsole ?? true,
});
}
export class Spinner {
#opts: Required<SpinnerOptions>;
isSpinning: boolean;
#stream: tty.SyncStream;
indent: number;
interval: number;
#id = 0;
#enabled: boolean;
#frameIndex: number;
#linesToClear: number;
#linesCount: number;
constructor(opts: Required<SpinnerOptions>) {
this.#opts = opts;
this.#stream = this.#opts.stream;
this.text = this.#opts.text;
this.prefix = this.#opts.prefix;
this.color = this.#opts.color;
this.spinner = this.#opts.spinner;
this.indent = this.#opts.indent;
this.interval = this.#opts.interval;
this.isSpinning = false;
this.#frameIndex = 0;
this.#linesToClear = 0;
this.#linesCount = 1;
this.#enabled = typeof opts.enabled === "boolean"
? opts.enabled
: tty.isInteractive(this.#stream);
if (opts.hideCursor) {
addEventListener("unload", () => {
tty.showCursorSync(this.#stream);
});
}
if (opts.interceptConsole) {
this.#interceptConsole();
}
}
#spinner: SpinnerAnimation = spinners.dots;
#color: ColorFunction = colors.cyan;
#text = "";
#prefix = "";
#interceptConsole(): void {
const methods: (keyof Console)[] = [
"log",
"debug",
"info",
"dir",
"dirxml",
"warn",
"error",
"assert",
"count",
"countReset",
"table",
"time",
"timeLog",
"timeEnd",
"group",
"groupCollapsed",
"groupEnd",
"clear",
"trace",
"profile",
"profileEnd",
"timeStamp",
];
for (const method of methods) {
const original = console[method] as (...args: unknown[]) => void;
// @ts-ignore Ignore the next line for dnt to type check properly
console[method] = (...args: unknown[]) => {
if (this.isSpinning) {
this.stop();
this.clear();
original(...args);
this.start();
} else {
original(...args);
}
};
}
}
set spinner(spin: string | SpinnerAnimation) {
this.#frameIndex = 0;
if (typeof spin === "string") this.#spinner = spinners[spin];
else this.#spinner = spin;
}
get spinner(): SpinnerAnimation {
return this.#spinner;
}
set color(color: string | ColorFunction) {
if (typeof color === "string") this.#color = colormap[color];
else this.#color = color;
}
get color(): ColorFunction {
return this.#color;
}
set text(value: string) {
this.#text = value;
this.updateLines();
}
get text(): string {
return this.#text;
}
set prefix(value: string) {
this.#prefix = value;
this.updateLines();
}
get prefix(): string {
return this.#prefix;
}
#write(data: string): void {
this.#stream.writeSync(encoder.encode(data));
}
start(): Spinner {
if (!this.#enabled) {
if (this.text) {
this.#write(`- ${this.text}\n`);
}
return this;
}
if (this.isSpinning) return this;
if (this.#opts.hideCursor) {
tty.hideCursorSync(this.#stream);
}
this.isSpinning = true;
this.render();
this.#id = setInterval(this.render.bind(this), this.interval);
return this;
}
render(): void {
this.clear();
this.#write(`${this.frame()}\n`);
this.updateLines();
this.#linesToClear = this.#linesCount;
}
frame(): string {
const { frames } = this.#spinner;
let frame = frames[this.#frameIndex];
frame = this.#color(frame);
this.#frameIndex = ++this.#frameIndex % frames.length;
const fullPrefixText = typeof this.prefix === "string" && this.prefix !== ""
? this.prefix + " "
: "";
const fullText = typeof this.text === "string" ? " " + this.text : "";
return fullPrefixText + frame + fullText;
}
clear(): void {
if (!this.#enabled) return;
for (let i = 0; i < this.#linesToClear; i++) {
tty.goUpSync(1, this.#stream);
tty.clearLineSync(this.#stream);
tty.goRightSync(this.indent - 1, this.#stream);
}
this.#linesToClear = 0;
}
updateLines(): void {
let columns = 80;
try {
//@ts-ignore TS2339
columns = Deno.consoleSize().columns ?? columns;
} catch {
// Unstable APIs is not enabled, fallback to default
}
const fullPrefixText = typeof this.prefix === "string"
? this.prefix + "-"
: "";
this.#linesCount = tty
.stripAnsi(fullPrefixText + "--" + this.text)
.split("\n")
.reduce((count, line) => {
return count + Math.max(1, Math.ceil(tty.wcswidth(line) / columns));
}, 0);
}
stop(): void {
if (!this.#enabled) return;
clearInterval(this.#id);
this.#id = -1;
this.#frameIndex = 0;
this.clear();
this.isSpinning = false;
if (this.#opts.hideCursor) {
tty.showCursorSync(this.#stream);
}
}
stopAndPersist(options: PersistOptions = {}): void {
const prefix = options.prefix || this.prefix;
const fullPrefix = typeof prefix === "string" && prefix !== ""
? prefix + " "
: "";
const text = options.text || this.text;
const fullText = typeof text === "string" ? " " + text : "";
this.stop();
// https://github.com/denoland/deno/issues/6001
this.#write(`${fullPrefix}${options.symbol || " "}${fullText}\n`);
}
succeed(text?: string): void {
return this.stopAndPersist({ symbol: symbols.success, text });
}
fail(text?: string): void {
return this.stopAndPersist({ symbol: symbols.error, text });
}
warn(text?: string): void {
return this.stopAndPersist({ symbol: symbols.warning, text });
}
info(text?: string): void {
return this.stopAndPersist({ symbol: symbols.info, text });
}
}