console log update status bar

Solutions on MaxInterview for console log update status bar by the best coders in the world

showing results for - "console log update status bar"
Chiara
19 Sep 2018
1const { bgWhite } = require("chalk");
2
3module.exports = class ProgressBar {
4	constructor() {
5		this.total;
6		this.current;
7		this.bar_length = process.stdout.columns - 30;
8	}
9
10	init(total) {
11		this.total = total;
12		this.current = 0;
13		this.update(this.current);
14	}
15
16	update(current) {
17		this.current = current;
18		const current_progress = this.current / this.total;
19		this.draw(current_progress);
20	}
21
22	draw(current_progress) {
23		const filled_bar_length = (current_progress * this.bar_length).toFixed(
24			0
25		);
26		const empty_bar_length = this.bar_length - filled_bar_length;
27
28		const filled_bar = this.get_bar(filled_bar_length, " ", bgWhite);
29		const empty_bar = this.get_bar(empty_bar_length, "-");
30		const percentage_progress = (current_progress * 100).toFixed(2);
31
32		process.stdout.clearLine();
33		process.stdout.cursorTo(0);
34		process.stdout.write(
35			`Current progress: [${filled_bar}${empty_bar}] | ${percentage_progress}%`
36		);
37	}
38
39	get_bar(length, char, color = a => a) {
40		let str = "";
41		for (let i = 0; i < length; i++) {
42			str += char;
43		}
44		return color(str);
45	}
46};
Hamza
05 Jan 2020
1$ yarn add cli-progress
2$ npm install cli-progress --save