-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
example-all.js
53 lines (43 loc) · 1.24 KB
/
example-all.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
import process from 'node:process';
import readline from 'node:readline';
import logUpdate from 'log-update';
import cliSpinners from './index.js';
const spinners = Object.keys(cliSpinners);
let frame = 0;
let spinner = 0;
let next;
let scrutator;
const showNextFrame = () => {
const {frames} = cliSpinners[spinners[spinner]];
logUpdate(frames[frame++ % frames.length] + ' ' + spinners[spinner]);
};
const showNextSpinner = () => {
if (next) {
clearInterval(next);
spinner++;
}
if (spinner < spinners.length) {
const s = cliSpinners[spinners[spinner]];
next = setInterval(showNextFrame, s.interval);
scrutator = setTimeout(showNextSpinner, Math.max(s.interval * s.frames.length, 1000));
} else {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
}
};
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (string_, key) => {
if (key.ctrl && key.name === 'c') {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(130);
}
if (key.name === 'return' && scrutator) {
clearTimeout(scrutator);
showNextSpinner();
}
});
console.log(`${spinners.length} spinners\n`);
showNextSpinner();
// $ node example-all.js
// Press `Enter` to skip to the next spinner