-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathindex.js
executable file
·76 lines (60 loc) · 2.11 KB
/
index.js
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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get directory where package is installed
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Show help if requested
if (process.argv.includes('--help')) {
console.log();
console.log("Play text art animations in the terminal\n");
console.log("Usage: npx firew0rks [folder] [loops]");
console.log("\t[folder]\tFolder containing text art frames (default: fireworks)");
console.log("\t[loops]\t\tNumber of times to loop the animation or use -1 to loop until the user terminates the program (default: 20)");
console.log();
process.exit(0);
}
// Use default values if no arguments provided
const folderName = process.argv[2] || 'fireworks';
const loops = process.argv[3] ? parseInt(process.argv[3]) : 20;
// Resolve folder path relative to package location
const folderPath = path.join(__dirname, folderName);
if (!fs.existsSync(folderPath)) {
console.log(folderName + " could not be found");
process.exit(0);
}
const textFiles = [];
let numFound = 0;
let filesExist = true;
while (filesExist) {
const fileName = path.join(folderPath, numFound + ".txt");
if (fs.existsSync(fileName)) {
textFiles.push(fs.readFileSync(fileName, 'utf8'));
numFound++;
} else {
filesExist = false;
}
}
if (textFiles.length === 0) {
console.log(folderName + " did not have text art files");
process.exit(0);
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function playAnimation() {
let i = 0;
let first = true;
// Calculate number of lines to move up based on first frame
const backspaceAdjust = '\x1b[A'.repeat(textFiles[0].split('\n').length + 1);
while (i < loops || loops === -1) {
for (const frame of textFiles) {
if (!first) {
process.stdout.write(backspaceAdjust);
}
process.stdout.write(frame + '\n');
first = false;
await sleep(50); // 0.05 seconds
}
i++;
}
}
playAnimation();