-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
84 lines (72 loc) · 2.25 KB
/
main.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
#!/usr/bin/env node
import chalk from "chalk";
import { readdir, readFile } from "fs/promises";
import { dirname } from "path";
import { fileURLToPath } from "url";
const { log, clear } = console;
interface LineColor {
start: number;
end: number;
color: number;
}
type FrameColor = LineColor[];
const __filename = fileURLToPath(import.meta.url);
const resourcesPath = dirname(__filename) + "/resources";
const framesPath = `${resourcesPath}/frames`;
const frameColorsPath = `${resourcesPath}/frame-colors`;
async function frameColorsLoader(path: string): Promise<FrameColor[]> {
const entities = await getFiles(path);
const colorFrames: FrameColor[] = [];
for (const entity of entities) {
const frameColor: FrameColor = [];
const lines = entity.split("\n");
for (const line of lines) {
const range = line.split("-");
frameColor.push({
start: parseInt(range[0]),
end: parseInt(range[1]),
color: parseInt(range[2]),
});
}
colorFrames.push(frameColor.reverse());
}
return colorFrames;
}
async function getFiles(path: string) {
const files = await readdir(path);
const collator = new Intl.Collator([], { numeric: true });
files.sort((a, b) => collator.compare(a, b));
const framePromises: Promise<string>[] = [];
for (const file of files) {
framePromises.push(readFile(`${path}/${file}`, "utf-8"));
}
return Promise.all(framePromises);
}
function renderFrame(frame: string, frameColor: FrameColor): void {
for (const sectionColor of frameColor) {
const colorize = chalk.ansi256(sectionColor.color);
const sectionFrame = frame.slice(sectionColor.start, sectionColor.end);
const start = frame.slice(0, sectionColor.start);
const end = frame.slice(sectionColor.end);
frame = start + colorize(sectionFrame) + end;
}
log(frame);
}
async function main() {
let frameIndex = 0;
const frames: string[] = await getFiles(framesPath);
const colorFrames: FrameColor[] = await frameColorsLoader(frameColorsPath);
setInterval(() => {
clear();
renderFrame(frames[frameIndex], colorFrames[frameIndex]);
frameIndex++;
if (frameIndex >= frames.length) {
frameIndex = 0;
}
}, 100);
}
process.on("SIGINT", () => {
console.clear();
process.exit();
});
main();