-
Notifications
You must be signed in to change notification settings - Fork 7
/
progress_bar.js
82 lines (67 loc) · 2.28 KB
/
progress_bar.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const _progress = require('cli-progress');
/**
* Provides functionality for displaying, stopping and clearing progress bars
*
* @type {module.ProgressBar}
*/
module.exports = class ProgressBar {
/**
* Initializes the ProgressBar class with a progress_obj member object
* @param progress_obj {Object}
* @returns void
*/
constructor(progress_obj){
/**
* A structures which stores the current value of the progress bar, the total expected value,
* and a message to output before the progress bar starts
* {
* counter: {number} the current value of the progress bar,
* total: {number} the total expected value,
* message: {string} message to output before the progress bar starts
* };
*
* @type {Object}
*/
this.progress_obj = progress_obj;
}
/**
* Resets the value counter of the progress bar object
* @returns void
*/
clear(){
this.progress_obj.counter = 0;
}
/**
* Initializes the progress bar and displays it to the console
* @returns void
*/
start() {
// Needed to access member properties from within the setInterval function
const self = this;
// Output the message of the member progress bar object
console.log(this.progress_obj.message);
// create new progress bar using default values
this.progress_obj.bar = new _progress.Bar({}, _progress.Presets.shades_grey);
this.progress_obj.bar.start(this.progress_obj.total, 0);
// 50ms update rate
this.progress_obj.timer = setInterval(function () {
// update the bar value
//console.log(progress_obj.counter);
self.progress_obj.bar.update(self.progress_obj.counter);
// limit reached
if (self.progress_obj.counter >= self.progress_obj.bar.getTotal()) {
// stop timer
clearInterval(self.progress_obj.timer);
self.progress_obj.bar.stop();
}
}, 50);
}
/**
* Stops the progress bar from continuing to update
* @returns void
*/
stop(){
clearInterval(this.progress_obj.timer);
this.progress_obj.bar.stop();
}
};