Skip to content

Commit b6db659

Browse files
fix: not incrementing in multi progress bars
1 parent f63b114 commit b6db659

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

.changeset/wicked-carrots-scream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@opentf/cli-pbar': minor
3+
---
4+
5+
Fixed not incrementing bars in multi progress bars.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ multiPBar.stop();
9595
```
9696

9797
> [!TIP]
98-
> It is recommended to use `MEDIUM` sized bars in multi progress bars for better visuals.
98+
> It is recommended to use the `MEDIUM` sized bars in multi progress bars for better visuals.
9999
100100
## Examples
101101

__tests__/progressBar.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ async function run(cb, options = {}, isTTY = true) {
4141
return await outputPromise;
4242
}
4343

44+
function getBarsStr(n, done = false) {
45+
return style(
46+
`$${done ? 'g' : 'gr'}.${done ? 'bol' : 'dim'}{${DEFAULT_BAR_CHAR}}`
47+
).repeat(n);
48+
}
49+
4450
function getBars(complete = 0, percent = 0, opt = {}) {
4551
const options = {
4652
width: 30,
@@ -190,4 +196,18 @@ describe('Single Progress Bar', () => {
190196
const bars = getBars(0, 0, { prefix: 'SUFFIX', suffix: 'SUFFIX' });
191197
expect(output[0].trim()).toMatch(bars.trim());
192198
});
199+
200+
it('renders with no precent & with count', async () => {
201+
const output = await run(
202+
async (pBar) => {
203+
pBar.start({ total: 3 });
204+
pBar.inc();
205+
pBar.inc();
206+
pBar.stop();
207+
},
208+
{ showPercent: false, width: 3, showCount: true }
209+
);
210+
const outBars = getBarsStr(1, true) + getBarsStr(2) + ' [2/3]';
211+
expect(output[2]).toStrictEqual(outBars);
212+
});
193213
});

src/ProgressBar.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ class ProgressBar {
106106
}
107107

108108
/** Starts rendering of the progress bars. */
109-
start(obj?: Partial<Bar>) {
110-
if (typeof obj === 'object') {
111-
this._bars.push({ ...obj, progress: true } as Bar);
109+
start(bar?: Partial<Bar>) {
110+
if (isObj(bar)) {
111+
this._bars.push({ ...bar, progress: true } as Bar);
112112
}
113113
this._render();
114114
}
@@ -132,35 +132,36 @@ class ProgressBar {
132132
}
133133

134134
/** Update the current progress bar */
135-
update(obj: Partial<Bar>, id?: number) {
135+
update(bar: Partial<Bar>, id?: number) {
136136
if (!id) {
137-
this._bars[0] = { ...(this._bars[0] as Bar), ...obj } as Bar;
137+
this._bars[0] = { ...(this._bars[0] as Bar), ...bar } as Bar;
138138
} else {
139139
const index = this._bars.findIndex((b) => isObj(b) && b.id === id);
140-
this._bars[index] = { ...(this._bars[index] as Bar), ...obj } as Bar;
140+
this._bars[index] = { ...(this._bars[index] as Bar), ...bar } as Bar;
141141
}
142142
this._options.stream.moveCursor(0, -(this._bars.length - 1));
143143
this._render();
144144
}
145145

146146
/** Adds a new progress bar to the rendering stack. */
147-
add(obj: Partial<Bar>) {
148-
if (isObj(obj)) {
149-
const id = this._bars.length + 1;
150-
const barInstance = { progress: true, ...obj, id } as Bar;
151-
this._bars.push(barInstance);
152-
if (this._bars.length > 2) {
153-
this._options.stream.moveCursor(0, -(this._bars.length - 2));
154-
}
155-
this._render();
156-
return {
157-
update: (obj: Partial<Bar>) => {
158-
this.update(obj, id);
159-
},
160-
};
147+
add(bar: Partial<Bar>) {
148+
const id = this._bars.length + 1;
149+
const barInstance = { progress: true, ...bar, id } as Bar;
150+
this._bars.push(barInstance);
151+
if (this._bars.length > 2) {
152+
this._options.stream.moveCursor(0, -(this._bars.length - 2));
161153
}
162-
163-
return null;
154+
this._render();
155+
return {
156+
update: (bar: Partial<Bar>) => {
157+
this.update(bar, id);
158+
},
159+
inc: (bar?: Partial<Bar>, val = 1) => {
160+
const curBar = this._bars.find((b) => b.id === id);
161+
const n = curBar?.value || 0;
162+
this.update({ ...bar, value: n + val }, id);
163+
},
164+
};
164165
}
165166

166167
/** Increment the value + 1, optionally with the provided value. */

0 commit comments

Comments
 (0)