forked from bandhavya/wm-reactnative-cli
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprogress-bar.js
More file actions
164 lines (133 loc) · 5.32 KB
/
progress-bar.js
File metadata and controls
164 lines (133 loc) · 5.32 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const chalk = require('chalk');
class ProgressBar {
constructor(options = {}) {
this.showProgressBar = options.showProgressBar || false;
this.barCompleteChar = options.barCompleteChar || '█';
this.barIncompleteChar = options.barIncompleteChar || '░';
this.barWidth = options.barWidth || 20;
this.barFormat = options.barFormat || '[{bar}] {percentage}%';
this.total = options.total || 100;
this.value = 0;
this.startTime = null;
// Optional color configurations
this.completeColor = options.completeColor || null;
this.incompleteColor = options.incompleteColor || null;
this.textColor = options.textColor || null;
// Performance optimizations
this.cachedOutput = '';
this.lastValue = -1;
this.lastPercentage = -1;
this.etaCache = '?';
this.etaCacheTime = 0;
this.etaCacheInterval = 2000; // Cache ETA for 2 seconds
}
start() {
this.startTime = Date.now();
this.invalidateCache();
}
setProgress(value) {
const newValue = Math.min(Math.max(0, value), this.total);
if (newValue !== this.value) {
this.value = newValue;
this.invalidateCache();
}
}
incrementProgress(amount = 1) {
this.setProgress(this.value + amount);
}
setTotal(total) {
if (total !== this.total) {
this.total = total;
this.invalidateCache();
}
}
enable() {
this.showProgressBar = true;
this.invalidateCache();
}
disable() {
this.showProgressBar = false;
this.invalidateCache();
}
status() {
return this.showProgressBar;
}
invalidateCache() {
this.cachedOutput = '';
this.lastValue = -1;
this.lastPercentage = -1;
}
calculateETA() {
if (!this.startTime || this.value === 0 || this.value === this.total) {
return '?';
}
const now = Date.now();
// Use cached ETA if it's still fresh
if (now - this.etaCacheTime < this.etaCacheInterval && this.etaCache !== '?') {
return this.etaCache;
}
const elapsedTime = (now - this.startTime) / 1000;
if (elapsedTime < 2) { // Wait at least 2 seconds before calculating ETA
this.etaCache = '?';
this.etaCacheTime = now;
return '?';
}
const itemsPerSecond = this.value / elapsedTime;
if (itemsPerSecond <= 0) {
this.etaCache = '?';
this.etaCacheTime = now;
return '?';
}
const eta = Math.round((this.total - this.value) / itemsPerSecond);
this.etaCache = isFinite(eta) && eta > 0 ? this.formatTime(eta) : '?';
this.etaCacheTime = now;
return this.etaCache;
}
formatTime(seconds) {
if (seconds < 60) return `${seconds}s`;
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}m ${remainingSeconds}s`;
}
render() {
if (!this.showProgressBar) return '';
const percentage = Math.floor((this.value / this.total) * 100);
// Use cached output if nothing significant changed
if (this.value === this.lastValue && percentage === this.lastPercentage && this.cachedOutput) {
return this.cachedOutput;
}
const completeLength = Math.round((this.value / this.total) * this.barWidth);
const incompleteLength = this.barWidth - completeLength;
let completeBar = this.barCompleteChar.repeat(completeLength);
let incompleteBar = this.barIncompleteChar.repeat(incompleteLength);
// Apply colors only if specified
if (this.completeColor) completeBar = chalk[this.completeColor](completeBar);
if (this.incompleteColor) incompleteBar = chalk[this.incompleteColor](incompleteBar);
const bar = completeBar + incompleteBar;
// Apply textColor only to non-bar parts to preserve bar colors
let formattedText = this.barFormat
.replace('{bar}', bar)
.replace('{percentage}', this.textColor ? chalk[this.textColor](percentage) : percentage)
.replace('{value}', this.textColor ? chalk[this.textColor](this.value) : this.value)
.replace('{total}', this.textColor ? chalk[this.textColor](this.total) : this.total)
.replace('{eta}', this.textColor ? chalk[this.textColor](this.calculateETA()) : this.calculateETA());
// Cache the result
this.cachedOutput = formattedText;
this.lastValue = this.value;
this.lastPercentage = percentage;
return formattedText;
}
}
// Create a more efficient overall progress bar (disabled by default)
const overallProgressBar = new ProgressBar({
showProgressBar: false, // Disabled by default to avoid dual progress bars
barWidth: 30, // Slightly smaller for better performance
completeColor: 'green',
incompleteColor: 'gray',
textColor: 'cyan',
barFormat: '[{bar}] {percentage}%' // Removed ETA for overall progress to improve performance
});
module.exports = {
ProgressBar,
overallProgressBar
};