-
Notifications
You must be signed in to change notification settings - Fork 84
/
example.js
executable file
·207 lines (164 loc) · 5.15 KB
/
example.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const _progress = require('../cli-progress');
// run the example sequentially! otherwise both will write to stdout/stderr simultaneous !
Example1(function(){
Example2(function(){
Example3(function(){
Example4(function(){
Example5(function(){
Example6(function(){
console.log('\nDemo finished!');
});});});});});});
function Example1(onComplete){
// EXAMPLE 1 ---------------------------------------------
console.log('\nExample 1 - Standard configuration (4s)');
// create new progress bar using default values
const b1 = new _progress.Bar();
b1.start(200, 0);
// the bar value - will be linear incremented
let value = 0;
// 20ms update rate
const timer = setInterval(function(){
// increment value
value++;
// update the bar value
b1.update(value)
// set limit
if (value >= b1.getTotal()){
// stop timer
clearInterval(timer);
b1.stop();
// run complete callback
onComplete.apply(this);
}
}, 20);
}
function Example2(onComplete){
// EXAMPLE 2 ---------------------------------------------
console.log('\nExample 2 - Custom configuration');
// create new progress bar using default values
const b2 = new _progress.Bar({
barCompleteChar: '#',
barIncompleteChar: '_',
format: ' |- Current Upload Progress: {percentage}%' + ' - ' + '||{bar}||',
fps: 5,
stream: process.stdout,
barsize: 30
});
b2.start(100, 0);
// 50ms update rate
const timer = setInterval(function(){
// increment value
b2.increment();
// set limit
if (b2.value >= b2.getTotal()){
// stop timer
clearInterval(timer);
b2.stop();
// run complete callback
onComplete.apply(this);
}
}, 50);
}
function Example3(onComplete){
// EXAMPLE 3 ---------------------------------------------
console.log('\nExample 3 - Stop the Bar Automatically');
// create new progress bar using default values
const b3 = new _progress.Bar({
stopOnComplete: true,
clearOnComplete: true
});
b3.start(200, 0);
// the bar value - will be linear incremented
let value = 0;
// 20ms update rate
const timer = setInterval(function(){
// increment value
value++;
// update the bar value
b3.update(value);
// set limit
if (value >= b3.getTotal()){
// stop timer
clearInterval(timer);
// run complete callback
onComplete.apply(this);
}
}, 20);
}
function Example4(onComplete){
// EXAMPLE 1 ---------------------------------------------
console.log('\nExample 4 - Start ZERO');
// create new progress bar using default values
const b1 = new _progress.Bar();
b1.start(0, 0);
setTimeout(function(){
b1.stop();
// run complete callback
onComplete.apply(this);
}, 1000);
}
function Example5(onComplete){
// EXAMPLE 5 ---------------------------------------------
console.log('\nExample 5 - Custom Payload');
// create new progress bar
const b1 = new _progress.Bar({
format: 'progress [{bar}] {percentage}% | ETA: {eta}s | {value}/{total} | Speed: {speed}'
});
// initialize the bar - defining payload token "speed" with the default value "N/A"
b1.start(200, 0, {
speed: "N/A"
});
// the bar value - will be linear incremented
let value = 0;
const speedData = [];
// 20ms update rate
let timer = setInterval(function(){
// increment value
value++;
// example speed data
speedData.push(Math.random()*2+5);
const currentSpeedData = speedData.splice(-10);
// update the bar value
b1.update(value, {
speed: (currentSpeedData.reduce(function(a, b) { return a + b; }, 0) / currentSpeedData.length).toFixed(2) + "mb/s"
});
// set limit
if (value >= b1.getTotal()){
// stop timer
clearInterval(timer);
b1.stop();
// run complete callback
onComplete.apply(this);
}
}, 20);
}
function Example6(onComplete){
// EXAMPLE 1 ---------------------------------------------
console.log('\nExample 6 - Set dynamically the total progress');
// create new progress bar using default values
const b1 = new _progress.Bar({}, _progress.Presets.shades_grey);
b1.start(200, 0);
// the bar value - will be linear incremented
let value = 0;
// 50ms update rate
const timer = setInterval(function(){
// increment value
value++;
// update the bar value
b1.update(value)
// change the total value
if (value > 1500){
b1.setTotal(3000);
}else if (value > 150){
b1.setTotal(2000);
}
// limit reached ?
if (value >= b1.getTotal()){
// stop timer
clearInterval(timer);
b1.stop();
// run complete callback
onComplete.apply(this);
}
}, 15);
}