@mpen/progressbar 0.1.8 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.d.mts +14 -2
- package/dist/index.mjs +55 -29
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -46,3 +46,5 @@ progress.complete()
|
|
|
46
46
|
|
|
47
47
|
Use `logLine()` to write messages above the active bar. If work stops early, call
|
|
48
48
|
`interrupt()` to render the current progress and finish with a newline.
|
|
49
|
+
After `complete()` has been called, `interrupt()` does nothing, so it is safe to
|
|
50
|
+
call in a `finally` block without changing successful output.
|
package/dist/index.d.mts
CHANGED
|
@@ -76,6 +76,7 @@ interface ProgressBarOptions {
|
|
|
76
76
|
max?: number;
|
|
77
77
|
/**
|
|
78
78
|
* The rendered bar width in terminal columns.
|
|
79
|
+
* When omitted or null, follows the current terminal size on each render.
|
|
79
80
|
*/
|
|
80
81
|
width?: number | null;
|
|
81
82
|
/**
|
|
@@ -135,13 +136,15 @@ declare class ProgressBar {
|
|
|
135
136
|
max: number;
|
|
136
137
|
/**
|
|
137
138
|
* Rendered bar width in terminal columns.
|
|
139
|
+
* Automatically follows the current terminal size unless explicitly set.
|
|
138
140
|
*
|
|
139
141
|
* @example
|
|
140
142
|
* ```ts
|
|
141
143
|
* progress.width = 40
|
|
142
144
|
* ```
|
|
143
145
|
*/
|
|
144
|
-
width: number;
|
|
146
|
+
get width(): number;
|
|
147
|
+
set width(value: number);
|
|
145
148
|
/** @internal */
|
|
146
149
|
msPerFrame: number;
|
|
147
150
|
/**
|
|
@@ -165,6 +168,8 @@ declare class ProgressBar {
|
|
|
165
168
|
private lastStrLength?;
|
|
166
169
|
private startTime;
|
|
167
170
|
private colors;
|
|
171
|
+
private configuredWidth?;
|
|
172
|
+
private completed;
|
|
168
173
|
/**
|
|
169
174
|
* Create a progress bar.
|
|
170
175
|
*
|
|
@@ -246,10 +251,17 @@ declare class ProgressBar {
|
|
|
246
251
|
update(progress: number): void;
|
|
247
252
|
/**
|
|
248
253
|
* Mark the current output as interrupted and write a newline.
|
|
254
|
+
* Does nothing after [`ProgressBar.complete`]{@link ProgressBar#complete} has been called,
|
|
255
|
+
* so this method can safely be used in a `finally` block.
|
|
249
256
|
*
|
|
250
257
|
* @example
|
|
251
258
|
* ```ts
|
|
252
|
-
*
|
|
259
|
+
* try {
|
|
260
|
+
* await processFiles()
|
|
261
|
+
* progress.complete()
|
|
262
|
+
* } finally {
|
|
263
|
+
* progress.interrupt()
|
|
264
|
+
* }
|
|
253
265
|
* ```
|
|
254
266
|
*
|
|
255
267
|
* @returns Nothing.
|
package/dist/index.mjs
CHANGED
|
@@ -87,13 +87,19 @@ var ProgressBar = class {
|
|
|
87
87
|
max;
|
|
88
88
|
/**
|
|
89
89
|
* Rendered bar width in terminal columns.
|
|
90
|
+
* Automatically follows the current terminal size unless explicitly set.
|
|
90
91
|
*
|
|
91
92
|
* @example
|
|
92
93
|
* ```ts
|
|
93
94
|
* progress.width = 40
|
|
94
95
|
* ```
|
|
95
96
|
*/
|
|
96
|
-
width
|
|
97
|
+
get width() {
|
|
98
|
+
return this.configuredWidth ?? Math.max((process.stdout.columns ?? 80) - 40, 16);
|
|
99
|
+
}
|
|
100
|
+
set width(value) {
|
|
101
|
+
this.configuredWidth = value;
|
|
102
|
+
}
|
|
97
103
|
/** @internal */
|
|
98
104
|
msPerFrame;
|
|
99
105
|
/**
|
|
@@ -117,6 +123,8 @@ var ProgressBar = class {
|
|
|
117
123
|
lastStrLength;
|
|
118
124
|
startTime = process.hrtime.bigint();
|
|
119
125
|
colors;
|
|
126
|
+
configuredWidth;
|
|
127
|
+
completed = false;
|
|
120
128
|
constructor(maxOrOptions = 1, width = null, start = true) {
|
|
121
129
|
const options = typeof maxOrOptions === "object" ? maxOrOptions : {
|
|
122
130
|
max: maxOrOptions,
|
|
@@ -125,7 +133,7 @@ var ProgressBar = class {
|
|
|
125
133
|
};
|
|
126
134
|
this.colors = new AnsiColors(options.enableColors);
|
|
127
135
|
this.max = options.max ?? 1;
|
|
128
|
-
this.
|
|
136
|
+
this.configuredWidth = options.width || void 0;
|
|
129
137
|
this.msPerFrame = 1e3 / (options.fps ?? 30);
|
|
130
138
|
this.partialBlocks = options.partialBlocks ?? ProgressBarPartialBlocks.smooth;
|
|
131
139
|
const fullBlock = this.partialBlocks.length > 0 ? this.partialBlocks.charAt(this.partialBlocks.length - 1) : "█";
|
|
@@ -181,27 +189,11 @@ var ProgressBar = class {
|
|
|
181
189
|
}
|
|
182
190
|
renderNowWithColor(fgColor) {
|
|
183
191
|
const percent = this.current < this.max ? this.current / this.max : 1;
|
|
184
|
-
const innerWidth = this.width - 6;
|
|
185
192
|
const percentStr = `${String(Math.floor(percent * 100 + .25)).padStart(4, " ")}%`;
|
|
186
|
-
const barLength = percent * innerWidth;
|
|
187
|
-
const fullBars = Math.floor(barLength);
|
|
188
|
-
const frac = barLength - fullBars;
|
|
189
193
|
const elapsed = process.hrtime.bigint() - this.startTime;
|
|
190
194
|
const elapsedSeconds = Number(elapsed / 10000n) / 1e5;
|
|
191
195
|
const quantStr = this.current === this.max ? this.colors.whiteBright(this.intFmt.format(this.max)) : `${this.colors.whiteBright(this.intFmt.format(this.current))}/${this.colors.whiteBright(this.intFmt.format(this.max))}`;
|
|
192
|
-
let
|
|
193
|
-
if (frac > 0) {
|
|
194
|
-
const subLen = stringWidth(this.partialBlocks) + 1;
|
|
195
|
-
const si = Math.floor(frac * subLen);
|
|
196
|
-
if (si > 0) filler += this.partialBlocks.charAt(si - 1);
|
|
197
|
-
}
|
|
198
|
-
let barStr;
|
|
199
|
-
if (this.emptyBlock === " ") barStr = this.colors.bgBlackBright(this.colors[fgColor](filler.padEnd(innerWidth, this.emptyBlock)));
|
|
200
|
-
else {
|
|
201
|
-
const emptyStr = this.colors.blackBright(this.emptyBlock.repeat(Math.max(0, innerWidth - stringWidth(filler))));
|
|
202
|
-
barStr = this.colors[fgColor](filler) + emptyStr;
|
|
203
|
-
}
|
|
204
|
-
let fullLine = `${percentStr} ${barStr} ${quantStr}`;
|
|
196
|
+
let suffix = ` ${quantStr}`;
|
|
205
197
|
const sectionSeparator = this.colors.blackBright("│");
|
|
206
198
|
if (this.current > 0) {
|
|
207
199
|
let estimatedRemaining = 0;
|
|
@@ -213,23 +205,43 @@ var ProgressBar = class {
|
|
|
213
205
|
if (estimatedRemaining >= 6e3 * 60) {
|
|
214
206
|
const days = Math.floor(estimatedRemaining / (1440 * 60));
|
|
215
207
|
const hours = Math.floor(estimatedRemaining / 3600 % 24);
|
|
216
|
-
|
|
208
|
+
suffix += ` ${sectionSeparator} ${days}d${String(hours).padStart(2, "0")}h`;
|
|
217
209
|
} else if (estimatedRemaining >= 6e3) {
|
|
218
210
|
const hours = Math.floor(estimatedRemaining / 3600);
|
|
219
211
|
const minutes = Math.floor(estimatedRemaining / 60 % 60);
|
|
220
|
-
|
|
212
|
+
suffix += ` ${sectionSeparator} ${hours}h${String(minutes).padStart(2, "0")}m`;
|
|
221
213
|
} else if (estimatedRemaining >= 60 || elapsedSeconds >= 60) {
|
|
222
214
|
const minutes = Math.floor(estimatedRemaining / 60);
|
|
223
215
|
const seconds = Math.floor(estimatedRemaining % 60);
|
|
224
|
-
|
|
216
|
+
suffix += ` ${sectionSeparator} ${minutes}m${String(seconds).padStart(2, "0")}s`;
|
|
225
217
|
} else {
|
|
226
218
|
const secondsFmt = estimatedRemaining < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
|
|
227
|
-
|
|
219
|
+
suffix += ` ${sectionSeparator} ${secondsFmt.format(estimatedRemaining)}s`;
|
|
228
220
|
}
|
|
229
221
|
const itemsPerSecond = this.current > 0 ? this.current / elapsedSeconds : 0;
|
|
230
222
|
const rateFmt = itemsPerSecond < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
|
|
231
|
-
|
|
223
|
+
suffix += ` ${sectionSeparator} ${rateFmt.format(itemsPerSecond)} it/s`;
|
|
224
|
+
}
|
|
225
|
+
const available = Math.max(0, (process.stdout.columns ?? 80) - 1);
|
|
226
|
+
if (stringWidth(percentStr) + 1 + stringWidth(suffix) > available) suffix = ` ${quantStr}`;
|
|
227
|
+
if (stringWidth(percentStr) + 1 + stringWidth(suffix) > available) suffix = "";
|
|
228
|
+
const innerWidth = Math.max(0, Math.min(this.width - 6, available - stringWidth(percentStr) - 1 - stringWidth(suffix)));
|
|
229
|
+
const barLength = percent * innerWidth;
|
|
230
|
+
const fullBars = Math.floor(barLength);
|
|
231
|
+
const frac = barLength - fullBars;
|
|
232
|
+
let filler = (this.partialBlocks.length > 0 ? this.partialBlocks.charAt(this.partialBlocks.length - 1) : "█").repeat(fullBars);
|
|
233
|
+
if (frac > 0) {
|
|
234
|
+
const subLen = stringWidth(this.partialBlocks) + 1;
|
|
235
|
+
const si = Math.floor(frac * subLen);
|
|
236
|
+
if (si > 0) filler += this.partialBlocks.charAt(si - 1);
|
|
237
|
+
}
|
|
238
|
+
let barStr;
|
|
239
|
+
if (this.emptyBlock === " ") barStr = this.colors.bgBlackBright(this.colors[fgColor](filler.padEnd(innerWidth, this.emptyBlock)));
|
|
240
|
+
else {
|
|
241
|
+
const emptyStr = this.colors.blackBright(this.emptyBlock.repeat(Math.max(0, innerWidth - stringWidth(filler))));
|
|
242
|
+
barStr = this.colors[fgColor](filler) + emptyStr;
|
|
232
243
|
}
|
|
244
|
+
const fullLine = available < 6 ? percentStr.trim().slice(0, available) : `${percentStr} ${barStr}${suffix}`;
|
|
233
245
|
if (fullLine === this.lastLine) return;
|
|
234
246
|
this.lastLine = fullLine;
|
|
235
247
|
this.writeLine(fullLine);
|
|
@@ -237,9 +249,11 @@ var ProgressBar = class {
|
|
|
237
249
|
}
|
|
238
250
|
writeLine(str) {
|
|
239
251
|
const len = stringWidth(str);
|
|
240
|
-
if (
|
|
241
|
-
|
|
242
|
-
|
|
252
|
+
if (process.stdout.isTTY) str = `\r\u001b[2K${str}`;
|
|
253
|
+
else if (this.lastStrLength) {
|
|
254
|
+
const available = Math.max(0, (process.stdout.columns ?? 80) - 1);
|
|
255
|
+
const padding = Math.max(0, Math.min(this.lastStrLength, available) - len);
|
|
256
|
+
str = `\r${str}${" ".repeat(padding)}`;
|
|
243
257
|
}
|
|
244
258
|
process.stdout.write(str);
|
|
245
259
|
this.lastStrLength = len;
|
|
@@ -280,15 +294,23 @@ var ProgressBar = class {
|
|
|
280
294
|
}
|
|
281
295
|
/**
|
|
282
296
|
* Mark the current output as interrupted and write a newline.
|
|
297
|
+
* Does nothing after [`ProgressBar.complete`]{@link ProgressBar#complete} has been called,
|
|
298
|
+
* so this method can safely be used in a `finally` block.
|
|
283
299
|
*
|
|
284
300
|
* @example
|
|
285
301
|
* ```ts
|
|
286
|
-
*
|
|
302
|
+
* try {
|
|
303
|
+
* await processFiles()
|
|
304
|
+
* progress.complete()
|
|
305
|
+
* } finally {
|
|
306
|
+
* progress.interrupt()
|
|
307
|
+
* }
|
|
287
308
|
* ```
|
|
288
309
|
*
|
|
289
310
|
* @returns Nothing.
|
|
290
311
|
*/
|
|
291
312
|
interrupt() {
|
|
313
|
+
if (this.completed) return;
|
|
292
314
|
this.renderNowWithColor("redBright");
|
|
293
315
|
process.stdout.write("\n");
|
|
294
316
|
}
|
|
@@ -303,7 +325,10 @@ var ProgressBar = class {
|
|
|
303
325
|
* @returns Nothing.
|
|
304
326
|
*/
|
|
305
327
|
clear() {
|
|
306
|
-
if (this.lastStrLength)
|
|
328
|
+
if (this.lastStrLength) {
|
|
329
|
+
const available = Math.max(0, (process.stdout.columns ?? 80) - 1);
|
|
330
|
+
process.stdout.write(process.stdout.isTTY ? "\r\x1B[2K" : `\r${" ".repeat(Math.min(this.lastStrLength, available))}\r`);
|
|
331
|
+
}
|
|
307
332
|
}
|
|
308
333
|
/**
|
|
309
334
|
* Complete the progress bar, render it in the completion color, and write a newline.
|
|
@@ -319,6 +344,7 @@ var ProgressBar = class {
|
|
|
319
344
|
this.current = this.max;
|
|
320
345
|
this.renderNowWithColor("blueBright");
|
|
321
346
|
process.stdout.write("\n");
|
|
347
|
+
this.completed = true;
|
|
322
348
|
}
|
|
323
349
|
/**
|
|
324
350
|
* Whether the progress value has reached the configured maximum.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpen/progressbar",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "Terminal progress bar utilities.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"dev": "bun src/dev.ts"
|
|
31
31
|
},
|
|
32
32
|
"publishHash": {
|
|
33
|
-
"buildDate": "2026-10-
|
|
34
|
-
"hgChangesetId": "
|
|
35
|
-
"sha256": "
|
|
33
|
+
"buildDate": "2026-10-05T23:28:33.541-07:00",
|
|
34
|
+
"hgChangesetId": "f6ba09d4d9e4+",
|
|
35
|
+
"sha256": "05e262f9e1a8c071141dcec4f358e3d50917e935921c0bb97e9bb5b4326ed924"
|
|
36
36
|
}
|
|
37
37
|
}
|