-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (132 loc) · 4 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const ora = require("ora");
let isActive = false;
/**
* Creates a progress bar for cli.
* @constructor
* @param {string} title - The text for the progress bar.
* @param {string} goal - The number of steps required to finish.
* @param {object} options - Additional options for the ora spinner.(see ora-js doc)
*/
function OraProgressBar(title, goal, options) {
if (isActive) throw new Error("Only one spinner can be active at a time");
if (goal < 0 || isNaN(goal)) throw new Error("Invalid goal");
let isStopped = false;
let startTime = null;
let ETA = 0;
this.goal = goal;
let text = title;
let currentText = text;
let currentStep = 0;
let spinner = {};
let lastDifference = 0;
const alpha = 0.6;
if (options) {
options.text = currentText;
spinner = ora(options).start();
} else {
spinner = ora(currentText).start();
}
isActive = true;
const calculateETA = () => {
if (startTime === null) {
startTime = new Date();
} else {
let progressTime = new Date();
let difference = progressTime - startTime;
if (lastDifference === 0) {
lastDifference = difference;
}
const expDifference = difference * alpha + (1 - alpha) * lastDifference;
lastDifference = expDifference;
return (
(expDifference / (currentStep - 1)) * (this.goal - (currentStep - 1))
);
}
};
const parseETA = () => {
if (isNaN(ETA)) {
return "ETA: Calculating";
}
const ETAHour = Math.floor(ETA / 3600000);
const ETAMinute = Math.floor((ETA % 3600000) / 60000);
const ETASecond = Math.floor((ETA % 60000) / 1000);
return `ETA: ${ETAHour}h:${ETAMinute}m:${ETASecond}s`;
};
const getPercentage = () => {
const percentage = Math.floor((currentStep / goal) * 100);
return `%${percentage}`;
};
const updateText = () => {
const ETAString = parseETA();
const percentage = getPercentage();
currentText = `${text} ${currentStep}/${this.goal} ${percentage}\t${ETAString}`;
spinner.text = currentText;
};
updateText();
/**
* Increments the current progress of the progress bar.
* @param {number} step - Increment amount
*/
this.progress = (step = 1) => {
if (isStopped) throw new Error("This progress bar already stopped");
currentStep += step;
ETA = calculateETA();
updateText();
if (currentStep >= this.goal) {
this.succeed();
}
};
/**
* Fails the progress bar.
* @param {string} text - Text to replace current text of progress bar.
*/
this.fail = (text = "") => {
if (isStopped) throw new Error("This progress bar already stopped");
spinner.fail(text);
isStopped = true;
isActive = false;
};
/**
* Succeeds the progress bar.
* @param {string} text - Text to replace current text of progress bar.
*/
this.succeed = (text = "") => {
if (isStopped) throw new Error("This progress bar already stopped");
spinner.succeed(text);
isStopped = true;
isActive = false;
};
/**
* Changes the current progress of the bar.
* @param {number} newProgress - New progress value for the progress bar.
*/
this.updateProgress = (newProgress) => {
if (isStopped) throw new Error("This progress bar already stopped");
if (newProgress > this.goal)
throw new Error("Invalid progress, progress cannot be bigger than goal");
else if (newProgress === this.goal) {
currentStep = newProgress;
updateText();
this.succeed();
}
currentStep = newProgress;
updateText();
};
/**
* Changes the current goal of the bar.
* @param {number} newGoal - New goal value for the progress bar.
*/
this.updateGoal = (newGoal) => {
if (isStopped) throw new Error("This progress bar already stopped");
if (newGoal < currentStep)
throw new Error(
"Invalid goal, goal cannot be smaller than current progress"
);
this.goal = newGoal;
updateText();
if (this.goal === currentStep) {
this.succeed();
}
};
}
module.exports = OraProgressBar;