Skip to content

Commit bd09d07

Browse files
committed
allow to pass payload as first argument to increment/update #67
1 parent 1314b5b commit bd09d07

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

‎CHANGES.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Branch 3.x ##
22

3+
### 3.8.0 ###
4+
5+
* Changed: allow to pass payload as first argument to `increment()` with implicit delta of 1 - thanks to [ecdeveloper on GitHub](https://github.com/AndiDittrich/Node.CLI-Progress/pull/67)
6+
* Changed: allow to pass payload as first argument to `update()` without updating bar value
7+
8+
39
### 3.7.0 ###
410

511
* Added: asynchronous eta update for long running processes (optional) - feature [requested on GitHub](https://github.com/AndiDittrich/Node.CLI-Progress/issues/65)

‎lib/generic-bar.js‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,28 @@ module.exports = class GenericBar extends _EventEmitter{
133133
}
134134

135135
// update the bar value
136-
update(current, payload){
137-
if (typeof current !== 'undefined' && current !== null) {
136+
// update(value, payload)
137+
// update(payload)
138+
update(arg0, arg1 = {}){
139+
// value set ?
140+
// update(value, [payload]);
141+
if (typeof arg0 === 'number') {
138142
// update value
139-
this.value = current;
143+
this.value = arg0;
140144

141145
// add new value; recalculate eta
142-
this.eta.update(Date.now(), current, this.total);
146+
this.eta.update(Date.now(), arg0, this.total);
143147
}
144148

149+
// extract payload
150+
// update(value, payload)
151+
// update(payload)
152+
const payloadData = ((typeof arg0 === 'object') ? arg0 : arg1) || {};
153+
145154
// update event (before stop() is called)
146155
this.emit('update', this.total, this.value);
147156

148157
// merge payload
149-
const payloadData = payload || {};
150158
for (const key in payloadData){
151159
this.payload[key] = payloadData[key];
152160
}
@@ -158,9 +166,18 @@ module.exports = class GenericBar extends _EventEmitter{
158166
}
159167

160168
// update the bar value
161-
increment(step, payload){
162-
step = step || 1;
163-
this.update(this.value + step, payload);
169+
// increment(delta, payload)
170+
// increment(payload)
171+
increment(arg0 = 1, arg1 = {}){
172+
// increment([payload]) => step=1
173+
// handle the use case when `step` is omitted but payload is passed
174+
if (typeof arg0 === 'object') {
175+
this.update(this.value + 1, arg0);
176+
177+
// increment([step=1], [payload={}])
178+
}else{
179+
this.update(this.value + arg0, arg1);
180+
}
164181
}
165182

166183
// get the total (limit) value

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cli-progress",
3-
"version": "3.7.0",
3+
"version": "3.8.0",
44
"description": "easy to use progress-bar for command-line/terminal applications",
55
"keywords": [
66
"cli",

‎yarn.lock‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"@babel/highlight" "^7.8.3"
1111

1212
"@babel/helper-validator-identifier@^7.9.0":
13-
version "7.9.0"
14-
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.0.tgz#ad53562a7fc29b3b9a91bbf7d10397fd146346ed"
15-
integrity sha512-6G8bQKjOh+of4PV/ThDm/rRqlU7+IGoJuofpagU5GlEl29Vv0RGqqt86ZGRV8ZuSOY3o+8yXl5y782SMcG7SHw==
13+
version "7.9.5"
14+
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.5.tgz#90977a8e6fbf6b431a7dc31752eee233bf052d80"
15+
integrity sha512-/8arLKUFq882w4tWGj9JYzRpAlZgiWUJ+dtteNTDqrRBz9Iguck9Rn3ykuBDoUwh2TO4tSAJlrxDUOXWklJe4g==
1616

1717
"@babel/highlight@^7.8.3":
1818
version "7.9.0"
@@ -195,9 +195,9 @@ cli-cursor@^3.1.0:
195195
restore-cursor "^3.1.0"
196196

197197
cli-width@^2.0.0:
198-
version "2.2.0"
199-
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
200-
integrity sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=
198+
version "2.2.1"
199+
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.1.tgz#b0433d0b4e9c847ef18868a4ef16fd5fc8271c48"
200+
integrity sha512-GRMWDxpOB6Dgk2E5Uo+3eEBvtOOlimMmpbFiKuLFnQzYDavtLFY3K5ona41jgN/WdRZtG7utuVSVTL4HbZHGkw==
201201

202202
cliui@^5.0.0:
203203
version "5.0.0"
@@ -420,11 +420,11 @@ esprima@^4.0.0:
420420
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
421421

422422
esquery@^1.0.1:
423-
version "1.2.0"
424-
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.2.0.tgz#a010a519c0288f2530b3404124bfb5f02e9797fe"
425-
integrity sha512-weltsSqdeWIX9G2qQZz7KlTRJdkkOCTPgLYJUz1Hacf48R4YOwGPHO3+ORfWedqJKbq5WQmsgK90n+pFLIKt/Q==
423+
version "1.3.1"
424+
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.3.1.tgz#b78b5828aa8e214e29fb74c4d5b752e1c033da57"
425+
integrity sha512-olpvt9QG0vniUBZspVRN6lwB7hOZoTRtT+jzR+tS4ffYx2mzbw+z0XCOk44aaLYKApNX5nMm+E+P6o25ip/DHQ==
426426
dependencies:
427-
estraverse "^5.0.0"
427+
estraverse "^5.1.0"
428428

429429
esrecurse@^4.1.0:
430430
version "4.2.1"
@@ -438,10 +438,10 @@ estraverse@^4.1.0, estraverse@^4.1.1:
438438
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.3.0.tgz#398ad3f3c5a24948be7725e83d11a7de28cdbd1d"
439439
integrity sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw==
440440

441-
estraverse@^5.0.0:
442-
version "5.0.0"
443-
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.0.0.tgz#ac81750b482c11cca26e4b07e83ed8f75fbcdc22"
444-
integrity sha512-j3acdrMzqrxmJTNj5dbr1YbjacrYgAxVMeF0gK16E3j494mOe7xygM/ZLIguEQ0ETwAg2hlJCtHRGav+y0Ny5A==
441+
estraverse@^5.1.0:
442+
version "5.1.0"
443+
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.1.0.tgz#374309d39fd935ae500e7b92e8a6b4c720e59642"
444+
integrity sha512-FyohXK+R0vE+y1nHLoBM7ZTyqRpqAlhdZHCWIWEviFLiGB8b04H6bQs8G+XTthacvT8VuwvteiP7RJSxMs8UEw==
445445

446446
esutils@^2.0.2:
447447
version "2.0.3"
@@ -1150,9 +1150,9 @@ string-width@^4.1.0, string-width@^4.2.0:
11501150
strip-ansi "^6.0.0"
11511151

11521152
string.prototype.trimend@^1.0.0:
1153-
version "1.0.0"
1154-
resolved "https://registry.yarnpkg.com/string.prototype.trimend/-/string.prototype.trimend-1.0.0.tgz#ee497fd29768646d84be2c9b819e292439614373"
1155-
integrity sha512-EEJnGqa/xNfIg05SxiPSqRS7S9qwDhYts1TSLR1BQfYUfPe1stofgGKvwERK9+9yf+PpfBMlpBaCHucXGPQfUA==
1153+
version "1.0.1"
1154+
resolved "https://registry.yarnpkg.com/string.prototype.trimend/-/string.prototype.trimend-1.0.1.tgz#85812a6b847ac002270f5808146064c995fb6913"
1155+
integrity sha512-LRPxFUaTtpqYsTeNKaFOw3R4bxIzWOnbQ837QfBylo8jIxtcbK/A/sMV7Q+OAV/vWo+7s25pOE10KYSjaSO06g==
11561156
dependencies:
11571157
define-properties "^1.1.3"
11581158
es-abstract "^1.17.5"
@@ -1176,9 +1176,9 @@ string.prototype.trimright@^2.1.1:
11761176
string.prototype.trimend "^1.0.0"
11771177

11781178
string.prototype.trimstart@^1.0.0:
1179-
version "1.0.0"
1180-
resolved "https://registry.yarnpkg.com/string.prototype.trimstart/-/string.prototype.trimstart-1.0.0.tgz#afe596a7ce9de905496919406c9734845f01a2f2"
1181-
integrity sha512-iCP8g01NFYiiBOnwG1Xc3WZLyoo+RuBymwIlWncShXDDJYWN6DbnM3odslBJdgCdRlq94B5s63NWAZlcn2CS4w==
1179+
version "1.0.1"
1180+
resolved "https://registry.yarnpkg.com/string.prototype.trimstart/-/string.prototype.trimstart-1.0.1.tgz#14af6d9f34b053f7cfc89b72f8f2ee14b9039a54"
1181+
integrity sha512-XxZn+QpvrBI1FOcg6dIpxUPgWCPuNXvMD72aaRaUQv1eD4e/Qy8i/hFTe0BUmD60p/QA6bh1avmuPTfNjqVWRw==
11821182
dependencies:
11831183
define-properties "^1.1.3"
11841184
es-abstract "^1.17.5"

0 commit comments

Comments
 (0)