-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressBar.js
46 lines (39 loc) · 1.12 KB
/
progressBar.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
import chalk from 'chalk'
export default class ProgressBar {
constructor() {
this.total;
this.current;
this.bar_length = process.stdout.columns - 30;
}
init(total) {
this.total = total;
this.current = 0;
this.update(this.current);
}
update(current) {
this.current = current;
const current_progress = this.current / this.total;
this.draw(current_progress);
}
draw(current_progress) {
const filled_bar_length = (current_progress * this.bar_length).toFixed(
0
);
const empty_bar_length = this.bar_length - filled_bar_length;
const filled_bar = this.get_bar(filled_bar_length, " ", chalk.bgHex('#03ff00'));
const empty_bar = this.get_bar(empty_bar_length, "-");
const percentage_progress = (current_progress * 100).toFixed(2);
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(
`${chalk.greenBright.bold('Current progress: [')}${filled_bar}${empty_bar}${chalk.greenBright.bold('] | ' + percentage_progress + '%')}`
);
}
get_bar(length, char, color = a => a) {
let str = "";
for (let i = 0; i < length; i++) {
str += char;
}
return color(str);
}
};