-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Expand file tree
/
Copy pathloading-indicator.ts
More file actions
173 lines (159 loc) · 4.11 KB
/
Copy pathloading-indicator.ts
File metadata and controls
173 lines (159 loc) · 4.11 KB
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
// Adapted from https://github.com/withastro/cli-kit
// MIT License Copyright (c) 2022 Nate Moore
import process from "node:process";
import readline from "node:readline";
import { erase, cursor } from "sisteransi";
import { reverse, sleep, color } from "./utils";
const GRADIENT_COLORS: Array<`#${string}`> = [
"#ffffff",
"#dadada",
"#dadada",
"#a8deaa",
"#a8deaa",
"#a8deaa",
"#d0f0bd",
"#d0f0bd",
"#ffffed",
"#ffffed",
"#ffffed",
"#ffffed",
"#ffffed",
"#ffffed",
"#ffffed",
"#ffffed",
"#ffffed",
"#f7f8ca",
"#f7f8ca",
"#eae6ba",
"#eae6ba",
"#eae6ba",
"#dadada",
"#dadada",
"#ffffff",
];
const MAX_FRAMES = 8;
const LEADING_FRAMES = Array.from(
{ length: MAX_FRAMES * 2 },
() => GRADIENT_COLORS[0],
);
const TRAILING_FRAMES = Array.from(
{ length: MAX_FRAMES * 2 },
() => GRADIENT_COLORS[GRADIENT_COLORS.length - 1],
);
const INDICATOR_FULL_FRAMES = [
...LEADING_FRAMES,
...GRADIENT_COLORS,
...TRAILING_FRAMES,
...reverse(GRADIENT_COLORS),
];
const INDICATOR_GRADIENT = reverse(
INDICATOR_FULL_FRAMES.map((_, i) => loadingIndicatorFrame(i)),
);
export async function renderLoadingIndicator({
start,
end,
while: update = () => sleep(100),
noMotion = false,
stdin = process.stdin,
stdout = process.stdout,
}: {
start: string;
end: string;
while: (...args: any) => Promise<any>;
noMotion?: boolean;
stdin?: NodeJS.ReadStream & { fd: 0 };
stdout?: NodeJS.WriteStream & { fd: 1 };
}) {
let act = update();
let tooSlow = Object.create(null);
let result = await Promise.race([sleep(500).then(() => tooSlow), act]);
if (result === tooSlow) {
let loading = await gradient(color.green(start), {
stdin,
stdout,
noMotion,
});
await act;
loading.stop();
}
stdout.write(`${" ".repeat(5)} ${color.green("✔")} ${color.green(end)}\n`);
}
function loadingIndicatorFrame(offset = 0) {
let frames = INDICATOR_FULL_FRAMES.slice(offset, offset + (MAX_FRAMES - 2));
if (frames.length < MAX_FRAMES - 2) {
let filled = new Array(MAX_FRAMES - frames.length - 2).fill(
GRADIENT_COLORS[0],
);
frames.push(...filled);
}
return frames;
}
function getGradientAnimationFrames() {
return INDICATOR_GRADIENT.map(
(colors) => " " + colors.map((g, i) => color.hex(g)("█")).join(""),
);
}
async function gradient(
text: string,
{ stdin = process.stdin, stdout = process.stdout, noMotion = false } = {},
) {
let { createLogUpdate } = await import("log-update");
let logUpdate = createLogUpdate(stdout);
let frameIndex = 0;
let frames = getGradientAnimationFrames();
let rl = readline.createInterface({ input: stdin, escapeCodeTimeout: 50 });
readline.emitKeypressEvents(stdin, rl);
if (stdin.isTTY) stdin.setRawMode(true);
function keypress(char: string) {
if (char === "\x03") {
loadingIndicator.stop();
process.exit(0);
}
if (stdin.isTTY) stdin.setRawMode(true);
stdout.write(cursor.hide + erase.lines(1));
}
let done = false;
let loadingIndicator = {
start() {
stdout.write(cursor.hide);
stdin.on("keypress", keypress);
logUpdate(`${frames[0]} ${text}`);
async function loop() {
if (done) return;
if (frameIndex < frames.length - 1) {
frameIndex++;
} else {
frameIndex = 0;
}
let frame = frames[frameIndex];
logUpdate(
`${(noMotion
? getMotionlessFrame(frameIndex)
: color.supportsColor
? frame
: getColorlessFrame(frameIndex)
).padEnd(MAX_FRAMES - 1, " ")} ${text}`,
);
if (!done) await sleep(20);
loop();
}
loop();
},
stop() {
done = true;
stdin.removeListener("keypress", keypress);
logUpdate.clear();
rl.close();
},
};
loadingIndicator.start();
return loadingIndicator;
}
function getColorlessFrame(frameIndex: number) {
return (
frameIndex % 3 === 0 ? ".. .. " : frameIndex % 3 === 1 ? " .. .." : ". .. ."
).padEnd(MAX_FRAMES - 1 + 20, " ");
}
function getMotionlessFrame(frameIndex: number) {
return " ".repeat(MAX_FRAMES - 1);
}