forked from sindresorhus/cli-spinners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-all.js
55 lines (45 loc) · 1.23 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
54
55
'use strict';
const readline = require('readline');
const logUpdate = require('log-update');
const cliSpinners = require('.');
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', (str, key) => {
if (key.ctrl && key.name === 'c') {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(130);
}
if (key.name === 'return') {
if (scrutator) {
clearTimeout(scrutator);
showNextSpinner();
}
}
});
console.log(spinners.length + ' spinners\n');
showNextSpinner();
// $ node example-all.js
// Press `Enter` to skip to the next spinner