-
Notifications
You must be signed in to change notification settings - Fork 9
/
kia.ts
245 lines (220 loc) · 5.17 KB
/
kia.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
import { Spinner, Spinners } from "./spinners.ts";
import {
clearLine,
Color,
colorise,
hideCursor,
showCursor,
writeLine,
} from "./util.ts";
import { Colors } from "./deps.ts";
export interface Options {
text: string;
color: Color;
spinner: Spinner;
prefixText: string;
indent: number;
cursor: boolean;
writer: Deno.WriterSync;
}
type InputOptions = Partial<Options>;
export default class Kia {
private options: Options = {
text: "",
color: "white",
spinner: Deno.build.os === "windows" ? Spinners.windows : Spinners.dots,
prefixText: "",
indent: 0,
cursor: false,
writer: Deno.stdout,
};
// deno-lint-ignore no-explicit-any
private timeoutRef: any;
private spinning = false;
private currentFrame = 0;
private textEncoder = new TextEncoder();
constructor(options?: InputOptions | string) {
if (!options) return;
if (typeof options === "string") {
options = {
text: options,
};
}
Object.assign(this.options, options);
this.render();
}
public set(options: InputOptions | string) {
if (typeof options === "string") {
options = {
text: options,
};
}
Object.assign(this.options, options);
this.render();
return this;
}
/**
* Starts the spinner
* @param text The text to display after the spinner
*/
start(text?: string) {
if (this.spinning) {
this.stop();
}
this.spinning = true;
if (text) this.set(text);
if (!this.options.cursor) {
hideCursor(this.options.writer, this.textEncoder);
}
this.timeoutRef = setInterval(() => {
this.currentFrame = (this.currentFrame + 1) %
this.options.spinner.frames.length;
this.render();
}, this.options.spinner.interval);
return this;
}
/**
* Stops the spinner and holds it in a static state. Returns the instance.
* @param options The options to apply after stopping the spinner
*/
stopAndPersist(options?: InputOptions) {
clearInterval(this.timeoutRef);
this.spinning = false;
if (options) this.set(options);
return this;
}
/**
* Renders the next frame of the spinner when it is stopped.
*/
renderNextFrame() {
if (this.spinning) {
throw new Error(
"You cannot manually render frames when the spinner is running, run stopAndPersist() first.",
);
}
this.currentFrame = (this.currentFrame + 1) %
this.options.spinner.frames.length;
this.render();
return this;
}
/**
* Stops the spinner and clears its line
*/
stop() {
clearInterval(this.timeoutRef);
clearLine(this.options.writer, this.textEncoder);
if (!this.options.cursor) {
showCursor(this.options.writer, this.textEncoder);
}
this.spinning = false;
return this;
}
/**
* Stops the spinner and leaves a message in its place
* @param text The message to show when stopped
* @param flair The icon to prepend the message
*/
stopWithFlair(text: string = this.options.text, flair: string) {
this.stop();
writeLine(
this.options.writer,
this.textEncoder,
`${flair} ${text}\n`,
this.options.indent,
);
return this;
}
/**
* Stops the spinner and leaves a success message.
*
* The function is a wrapper around ```stopWithFlair```.
* @param text The message to be shown when stopped
*/
succeed(text: string = this.options.text) {
return this.stopWithFlair(
text,
Colors.bold(
Colors.green(
Deno.build.os === "windows" ? String.fromCharCode(30) : "√",
),
),
);
}
/**
* Stops the spinner and leaves a failure message.
*
* The function is a wrapper around ```stopWithFlair```.
* @param text The message to be shown when stopped
*/
fail(text: string = this.options.text) {
return this.stopWithFlair(text, Colors.bold(Colors.red("X")));
}
/**
* Stops the spinner and leaves a warning message.
*
* The function is a wrapper around ```stopWithFlair```.
* @param text The message to be shown when stopped
*/
warn(text: string = this.options.text) {
return this.stopWithFlair(text, Colors.bold(Colors.yellow("!")));
}
/**
* Stops the spinner and leaves an information message.
*
* The function is a wrapper around ```stopWithFlair```.
* @param text The message to be shown when stopped
*/
info(text: string = this.options.text) {
return this.stopWithFlair(text, Colors.bold(Colors.blue("i")));
}
/**
* Returns whether the instance is currently spinning
*/
isSpinning(): boolean {
return this.spinning;
}
/**
* Returns the current spinner frame
*/
getFrame(): string {
return this.options.spinner.frames[this.currentFrame];
}
/**
* Gets the current text
*/
getText(): string {
return this.options.text;
}
/**
* Renders each frame of the spinner
*/
private render() {
const colorFunc = colorise(this.options.color);
writeLine(
this.options.writer,
this.textEncoder,
`${this.options.prefixText}${
colorFunc(
this.options.spinner.frames[this.currentFrame],
)
} ${this.options.text}`,
this.options.indent,
);
}
}
/**
* Starts a spinner for a promise
*/
// deno-lint-ignore ban-types
export const forPromise = async (action: Function, options: InputOptions) => {
const kia = new Kia(options).start();
await (async () => {
try {
await action();
kia.succeed();
} catch (_) {
kia.fail();
}
})();
return kia;
};