@mpen/progressbar 0.1.10 → 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
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,6 +180,7 @@ 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?;
172
186
  private completed;
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,6 +158,7 @@ var ProgressBar = class {
122
158
  lastRenderTime;
123
159
  lastStrLength;
124
160
  startTime = process.hrtime.bigint();
161
+ recentRate;
125
162
  colors;
126
163
  configuredWidth;
127
164
  completed = false;
@@ -133,6 +170,7 @@ var ProgressBar = class {
133
170
  };
134
171
  this.colors = new AnsiColors(options.enableColors);
135
172
  this.max = options.max ?? 1;
173
+ this.recentRate = new RecentRate(this.current, this.startTime, options.rateHalfLifeSeconds);
136
174
  this.configuredWidth = options.width || void 0;
137
175
  this.msPerFrame = 1e3 / (options.fps ?? 30);
138
176
  this.partialBlocks = options.partialBlocks ?? ProgressBarPartialBlocks.smooth;
@@ -164,6 +202,7 @@ var ProgressBar = class {
164
202
  */
165
203
  start() {
166
204
  this.startTime = process.hrtime.bigint();
205
+ this.recentRate.reset(this.current, this.startTime);
167
206
  this.render();
168
207
  }
169
208
  render() {
@@ -187,22 +226,22 @@ var ProgressBar = class {
187
226
  renderNow() {
188
227
  this.renderNowWithColor("greenBright");
189
228
  }
190
- renderNowWithColor(fgColor) {
229
+ renderNowWithColor(fgColor, measuredCurrent = this.current) {
191
230
  const percent = this.current < this.max ? this.current / this.max : 1;
192
231
  const percentStr = `${String(Math.floor(percent * 100 + .25)).padStart(4, " ")}%`;
193
- const elapsed = process.hrtime.bigint() - this.startTime;
232
+ const now = process.hrtime.bigint();
233
+ const elapsed = now - this.startTime;
194
234
  const elapsedSeconds = Number(elapsed / 10000n) / 1e5;
235
+ const itemsPerSecond = this.recentRate.sample(measuredCurrent, now);
195
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))}`;
196
237
  let suffix = ` ${quantStr}`;
197
238
  const sectionSeparator = this.colors.blackBright("│");
198
239
  if (this.current > 0) {
199
240
  let estimatedRemaining = 0;
200
241
  if (this.current === this.max) estimatedRemaining = elapsedSeconds;
201
- else if (elapsedSeconds > 0) {
202
- const progressRate = this.current / elapsedSeconds;
203
- estimatedRemaining = (this.max - this.current) / progressRate;
204
- }
205
- 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) {
206
245
  const days = Math.floor(estimatedRemaining / (1440 * 60));
207
246
  const hours = Math.floor(estimatedRemaining / 3600 % 24);
208
247
  suffix += ` ${sectionSeparator} ${days}d${String(hours).padStart(2, "0")}h`;
@@ -218,7 +257,6 @@ var ProgressBar = class {
218
257
  const secondsFmt = estimatedRemaining < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
219
258
  suffix += ` ${sectionSeparator} ${secondsFmt.format(estimatedRemaining)}s`;
220
259
  }
221
- const itemsPerSecond = this.current > 0 ? this.current / elapsedSeconds : 0;
222
260
  const rateFmt = itemsPerSecond < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
223
261
  suffix += ` ${sectionSeparator} ${rateFmt.format(itemsPerSecond)} it/s`;
224
262
  }
@@ -290,6 +328,7 @@ var ProgressBar = class {
290
328
  */
291
329
  update(progress) {
292
330
  this.current = progress;
331
+ this.recentRate.sample(progress, process.hrtime.bigint());
293
332
  this.render();
294
333
  }
295
334
  /**
@@ -341,8 +380,9 @@ var ProgressBar = class {
341
380
  * @returns Nothing.
342
381
  */
343
382
  complete() {
383
+ const measuredCurrent = this.current;
344
384
  this.current = this.max;
345
- this.renderNowWithColor("blueBright");
385
+ this.renderNowWithColor("blueBright", measuredCurrent);
346
386
  process.stdout.write("\n");
347
387
  this.completed = true;
348
388
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpen/progressbar",
3
- "version": "0.1.10",
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-05T23:28:33.541-07:00",
34
- "hgChangesetId": "f6ba09d4d9e4+",
35
- "sha256": "05e262f9e1a8c071141dcec4f358e3d50917e935921c0bb97e9bb5b4326ed924"
33
+ "buildDate": "2026-11-27T22:01:36.032-08:00",
34
+ "hgChangesetId": "014c84349a09+",
35
+ "sha256": "28452b53e906997caae7c3adb78a00e02ff084ec9c8b111dc1d47b0a758cb9f4"
36
36
  }
37
37
  }