@mpen/progressbar 0.1.9 → 0.1.11

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 CHANGED
@@ -22,6 +22,17 @@ The bar starts immediately and writes to stdout. Call `tick()` after each item,
22
22
  `tick(count)` after a batch, or `update(count)` to set the absolute completed count.
23
23
  Call `complete()` to render 100% and finish with a newline.
24
24
 
25
+ Speed (`it/s`) and remaining time use exponentially weighted throughput with a
26
+ 30-minute half-life by default. For jobs that run for hours or days and mix fast
27
+ records with occasional slow ones, set `rateHalfLifeSeconds: Infinity` to average
28
+ throughput over the whole run. If throughput changes for sustained periods, use a
29
+ longer finite half-life such as `rateHalfLifeSeconds: 3600` to give more weight to
30
+ the last few hours. A longer half-life takes more time to reflect a change in pace.
31
+ Batch updates are measured as items per elapsed second. Until time and progress
32
+ have been measured, the remaining time is shown as `--`. Redraws during a stall
33
+ lower the estimated speed; the bar does not redraw on a background timer.
34
+ On completion, the time field shows total elapsed time instead of remaining time.
35
+
25
36
  ## Customization
26
37
 
27
38
  ```ts
@@ -46,3 +57,5 @@ progress.complete()
46
57
 
47
58
  Use `logLine()` to write messages above the active bar. If work stops early, call
48
59
  `interrupt()` to render the current progress and finish with a newline.
60
+ After `complete()` has been called, `interrupt()` does nothing, so it is safe to
61
+ call in a `finally` block without changing successful output.
package/dist/index.d.mts CHANGED
@@ -87,6 +87,19 @@ interface ProgressBarOptions {
87
87
  * Maximum render frames per second.
88
88
  */
89
89
  fps?: number;
90
+ /**
91
+ * Seconds for past throughput to lose half its weight in the speed and ETA.
92
+ * Use a larger positive value for steadier estimates or `Infinity` to average
93
+ * throughput over the entire run.
94
+ *
95
+ * @defaultValue 1800 (30 minutes)
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const progress = new ProgressBar({ max: records.length, rateHalfLifeSeconds: Infinity })
100
+ * ```
101
+ */
102
+ rateHalfLifeSeconds?: number;
90
103
  /**
91
104
  * Characters used for fractional bar segments between whole terminal cells.
92
105
  */
@@ -167,8 +180,10 @@ declare class ProgressBar {
167
180
  private lastRenderTime?;
168
181
  private lastStrLength?;
169
182
  private startTime;
183
+ private readonly recentRate;
170
184
  private colors;
171
185
  private configuredWidth?;
186
+ private completed;
172
187
  /**
173
188
  * Create a progress bar.
174
189
  *
@@ -250,10 +265,17 @@ declare class ProgressBar {
250
265
  update(progress: number): void;
251
266
  /**
252
267
  * Mark the current output as interrupted and write a newline.
268
+ * Does nothing after [`ProgressBar.complete`]{@link ProgressBar#complete} has been called,
269
+ * so this method can safely be used in a `finally` block.
253
270
  *
254
271
  * @example
255
272
  * ```ts
256
- * progress.interrupt()
273
+ * try {
274
+ * await processFiles()
275
+ * progress.complete()
276
+ * } finally {
277
+ * progress.interrupt()
278
+ * }
257
279
  * ```
258
280
  *
259
281
  * @returns Nothing.
package/dist/index.mjs CHANGED
@@ -1,5 +1,41 @@
1
1
  import { AnsiColors } from "@mpen/ansi-colors/class";
2
2
  import stringWidth from "string-width";
3
+ //#region src/RecentRate.ts
4
+ /** @internal */
5
+ var RecentRate = class {
6
+ current;
7
+ time;
8
+ halfLifeSeconds;
9
+ weightedItems = 0;
10
+ weightedSeconds = 0;
11
+ constructor(current, time, halfLifeSeconds = 1800) {
12
+ this.current = current;
13
+ this.time = time;
14
+ this.halfLifeSeconds = halfLifeSeconds;
15
+ if (!(halfLifeSeconds > 0)) throw new RangeError("rateHalfLifeSeconds must be positive");
16
+ }
17
+ reset(current, time) {
18
+ this.weightedItems = 0;
19
+ this.weightedSeconds = 0;
20
+ this.current = current;
21
+ this.time = time;
22
+ }
23
+ sample(current, time) {
24
+ if (current < this.current) this.reset(current, time);
25
+ const seconds = Number(time - this.time) / 1e9;
26
+ if (seconds > 0) {
27
+ const exponent = seconds / this.halfLifeSeconds * Math.LN2;
28
+ const weight = -Math.expm1(-exponent);
29
+ const duration = weight === 0 ? seconds : this.halfLifeSeconds * weight / Math.LN2;
30
+ this.weightedItems = this.weightedItems * (1 - weight) + (current - this.current) / seconds * duration;
31
+ this.weightedSeconds = this.weightedSeconds * (1 - weight) + duration;
32
+ this.current = current;
33
+ this.time = time;
34
+ }
35
+ return this.weightedSeconds > 0 ? this.weightedItems / this.weightedSeconds : 0;
36
+ }
37
+ };
38
+ //#endregion
3
39
  //#region src/index.ts
4
40
  /**
5
41
  * Preset character sets for fractional progress bar segments.
@@ -122,8 +158,10 @@ var ProgressBar = class {
122
158
  lastRenderTime;
123
159
  lastStrLength;
124
160
  startTime = process.hrtime.bigint();
161
+ recentRate;
125
162
  colors;
126
163
  configuredWidth;
164
+ completed = false;
127
165
  constructor(maxOrOptions = 1, width = null, start = true) {
128
166
  const options = typeof maxOrOptions === "object" ? maxOrOptions : {
129
167
  max: maxOrOptions,
@@ -132,6 +170,7 @@ var ProgressBar = class {
132
170
  };
133
171
  this.colors = new AnsiColors(options.enableColors);
134
172
  this.max = options.max ?? 1;
173
+ this.recentRate = new RecentRate(this.current, this.startTime, options.rateHalfLifeSeconds);
135
174
  this.configuredWidth = options.width || void 0;
136
175
  this.msPerFrame = 1e3 / (options.fps ?? 30);
137
176
  this.partialBlocks = options.partialBlocks ?? ProgressBarPartialBlocks.smooth;
@@ -163,6 +202,7 @@ var ProgressBar = class {
163
202
  */
164
203
  start() {
165
204
  this.startTime = process.hrtime.bigint();
205
+ this.recentRate.reset(this.current, this.startTime);
166
206
  this.render();
167
207
  }
168
208
  render() {
@@ -186,22 +226,22 @@ var ProgressBar = class {
186
226
  renderNow() {
187
227
  this.renderNowWithColor("greenBright");
188
228
  }
189
- renderNowWithColor(fgColor) {
229
+ renderNowWithColor(fgColor, measuredCurrent = this.current) {
190
230
  const percent = this.current < this.max ? this.current / this.max : 1;
191
231
  const percentStr = `${String(Math.floor(percent * 100 + .25)).padStart(4, " ")}%`;
192
- const elapsed = process.hrtime.bigint() - this.startTime;
232
+ const now = process.hrtime.bigint();
233
+ const elapsed = now - this.startTime;
193
234
  const elapsedSeconds = Number(elapsed / 10000n) / 1e5;
235
+ const itemsPerSecond = this.recentRate.sample(measuredCurrent, now);
194
236
  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))}`;
195
237
  let suffix = ` ${quantStr}`;
196
238
  const sectionSeparator = this.colors.blackBright("│");
197
239
  if (this.current > 0) {
198
240
  let estimatedRemaining = 0;
199
241
  if (this.current === this.max) estimatedRemaining = elapsedSeconds;
200
- else if (elapsedSeconds > 0) {
201
- const progressRate = this.current / elapsedSeconds;
202
- estimatedRemaining = (this.max - this.current) / progressRate;
203
- }
204
- if (estimatedRemaining >= 6e3 * 60) {
242
+ else if (itemsPerSecond > 0) estimatedRemaining = (this.max - this.current) / itemsPerSecond;
243
+ if (this.current !== this.max && itemsPerSecond === 0) suffix += ` ${sectionSeparator} --`;
244
+ else if (estimatedRemaining >= 6e3 * 60) {
205
245
  const days = Math.floor(estimatedRemaining / (1440 * 60));
206
246
  const hours = Math.floor(estimatedRemaining / 3600 % 24);
207
247
  suffix += ` ${sectionSeparator} ${days}d${String(hours).padStart(2, "0")}h`;
@@ -217,7 +257,6 @@ var ProgressBar = class {
217
257
  const secondsFmt = estimatedRemaining < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
218
258
  suffix += ` ${sectionSeparator} ${secondsFmt.format(estimatedRemaining)}s`;
219
259
  }
220
- const itemsPerSecond = this.current > 0 ? this.current / elapsedSeconds : 0;
221
260
  const rateFmt = itemsPerSecond < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
222
261
  suffix += ` ${sectionSeparator} ${rateFmt.format(itemsPerSecond)} it/s`;
223
262
  }
@@ -289,19 +328,28 @@ var ProgressBar = class {
289
328
  */
290
329
  update(progress) {
291
330
  this.current = progress;
331
+ this.recentRate.sample(progress, process.hrtime.bigint());
292
332
  this.render();
293
333
  }
294
334
  /**
295
335
  * Mark the current output as interrupted and write a newline.
336
+ * Does nothing after [`ProgressBar.complete`]{@link ProgressBar#complete} has been called,
337
+ * so this method can safely be used in a `finally` block.
296
338
  *
297
339
  * @example
298
340
  * ```ts
299
- * progress.interrupt()
341
+ * try {
342
+ * await processFiles()
343
+ * progress.complete()
344
+ * } finally {
345
+ * progress.interrupt()
346
+ * }
300
347
  * ```
301
348
  *
302
349
  * @returns Nothing.
303
350
  */
304
351
  interrupt() {
352
+ if (this.completed) return;
305
353
  this.renderNowWithColor("redBright");
306
354
  process.stdout.write("\n");
307
355
  }
@@ -332,9 +380,11 @@ var ProgressBar = class {
332
380
  * @returns Nothing.
333
381
  */
334
382
  complete() {
383
+ const measuredCurrent = this.current;
335
384
  this.current = this.max;
336
- this.renderNowWithColor("blueBright");
385
+ this.renderNowWithColor("blueBright", measuredCurrent);
337
386
  process.stdout.write("\n");
387
+ this.completed = true;
338
388
  }
339
389
  /**
340
390
  * 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.9",
3
+ "version": "0.1.11",
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-04T22:25:30.365-07:00",
34
- "hgChangesetId": "36f79ac2bb8e+",
35
- "sha256": "5870a49673de1536219220bef58b8b98d35b3a223ca37c45e50dcbb138a90d1d"
33
+ "buildDate": "2026-11-27T22:01:36.032-08:00",
34
+ "hgChangesetId": "014c84349a09+",
35
+ "sha256": "28452b53e906997caae7c3adb78a00e02ff084ec9c8b111dc1d47b0a758cb9f4"
36
36
  }
37
37
  }