summaryrefslogtreecommitdiff
path: root/src/misc/cli/indicator.ts
blob: 3e23f9b274bdf4ae15be7220b049daeba884a42c (plain)
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
import * as readline from 'readline';

/**
 * Indicator
 */
export default class {
	private clock: NodeJS.Timer;

	constructor(text: string) {
		let i = 0; // Dots counter

		draw();
		this.clock = setInterval(draw, 300);

		function draw(): void {
			cll();
			i = (i + 1) % 4;
			const dots = new Array(i + 1).join('.');
			process.stdout.write(text + dots); // Write text
		}
	}

	public end(): void {
		clearInterval(this.clock);
		cll();
	}
}

/**
 * Clear current line
 */
function cll(): void {
	readline.clearLine(process.stdout, 0); // Clear current text
	readline.cursorTo(process.stdout, 0, null); // Move cursor to the head of line
}