File: example-all.js

package info (click to toggle)
node-cli-spinners 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 644 kB
  • sloc: javascript: 124; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,261 bytes parent folder | download
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