@mpen/progressbar 0.1.8 → 0.1.9

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/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,7 @@ declare class ProgressBar {
165
168
  private lastStrLength?;
166
169
  private startTime;
167
170
  private colors;
171
+ private configuredWidth?;
168
172
  /**
169
173
  * Create a progress bar.
170
174
  *
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,7 @@ var ProgressBar = class {
117
123
  lastStrLength;
118
124
  startTime = process.hrtime.bigint();
119
125
  colors;
126
+ configuredWidth;
120
127
  constructor(maxOrOptions = 1, width = null, start = true) {
121
128
  const options = typeof maxOrOptions === "object" ? maxOrOptions : {
122
129
  max: maxOrOptions,
@@ -125,7 +132,7 @@ var ProgressBar = class {
125
132
  };
126
133
  this.colors = new AnsiColors(options.enableColors);
127
134
  this.max = options.max ?? 1;
128
- this.width = options.width || Math.max((process.stdout.columns ?? 80) - 40, 16);
135
+ this.configuredWidth = options.width || void 0;
129
136
  this.msPerFrame = 1e3 / (options.fps ?? 30);
130
137
  this.partialBlocks = options.partialBlocks ?? ProgressBarPartialBlocks.smooth;
131
138
  const fullBlock = this.partialBlocks.length > 0 ? this.partialBlocks.charAt(this.partialBlocks.length - 1) : "█";
@@ -181,27 +188,11 @@ var ProgressBar = class {
181
188
  }
182
189
  renderNowWithColor(fgColor) {
183
190
  const percent = this.current < this.max ? this.current / this.max : 1;
184
- const innerWidth = this.width - 6;
185
191
  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
192
  const elapsed = process.hrtime.bigint() - this.startTime;
190
193
  const elapsedSeconds = Number(elapsed / 10000n) / 1e5;
191
194
  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 filler = (this.partialBlocks.length > 0 ? this.partialBlocks.charAt(this.partialBlocks.length - 1) : "█").repeat(fullBars);
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}`;
195
+ let suffix = ` ${quantStr}`;
205
196
  const sectionSeparator = this.colors.blackBright("│");
206
197
  if (this.current > 0) {
207
198
  let estimatedRemaining = 0;
@@ -213,23 +204,43 @@ var ProgressBar = class {
213
204
  if (estimatedRemaining >= 6e3 * 60) {
214
205
  const days = Math.floor(estimatedRemaining / (1440 * 60));
215
206
  const hours = Math.floor(estimatedRemaining / 3600 % 24);
216
- fullLine += ` ${sectionSeparator} ${days}d${String(hours).padStart(2, "0")}h`;
207
+ suffix += ` ${sectionSeparator} ${days}d${String(hours).padStart(2, "0")}h`;
217
208
  } else if (estimatedRemaining >= 6e3) {
218
209
  const hours = Math.floor(estimatedRemaining / 3600);
219
210
  const minutes = Math.floor(estimatedRemaining / 60 % 60);
220
- fullLine += ` ${sectionSeparator} ${hours}h${String(minutes).padStart(2, "0")}m`;
211
+ suffix += ` ${sectionSeparator} ${hours}h${String(minutes).padStart(2, "0")}m`;
221
212
  } else if (estimatedRemaining >= 60 || elapsedSeconds >= 60) {
222
213
  const minutes = Math.floor(estimatedRemaining / 60);
223
214
  const seconds = Math.floor(estimatedRemaining % 60);
224
- fullLine += ` ${sectionSeparator} ${minutes}m${String(seconds).padStart(2, "0")}s`;
215
+ suffix += ` ${sectionSeparator} ${minutes}m${String(seconds).padStart(2, "0")}s`;
225
216
  } else {
226
217
  const secondsFmt = estimatedRemaining < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
227
- fullLine += ` ${sectionSeparator} ${secondsFmt.format(estimatedRemaining)}s`;
218
+ suffix += ` ${sectionSeparator} ${secondsFmt.format(estimatedRemaining)}s`;
228
219
  }
229
220
  const itemsPerSecond = this.current > 0 ? this.current / elapsedSeconds : 0;
230
221
  const rateFmt = itemsPerSecond < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
231
- fullLine += ` ${sectionSeparator} ${rateFmt.format(itemsPerSecond)} it/s`;
222
+ suffix += ` ${sectionSeparator} ${rateFmt.format(itemsPerSecond)} it/s`;
232
223
  }
224
+ const available = Math.max(0, (process.stdout.columns ?? 80) - 1);
225
+ if (stringWidth(percentStr) + 1 + stringWidth(suffix) > available) suffix = ` ${quantStr}`;
226
+ if (stringWidth(percentStr) + 1 + stringWidth(suffix) > available) suffix = "";
227
+ const innerWidth = Math.max(0, Math.min(this.width - 6, available - stringWidth(percentStr) - 1 - stringWidth(suffix)));
228
+ const barLength = percent * innerWidth;
229
+ const fullBars = Math.floor(barLength);
230
+ const frac = barLength - fullBars;
231
+ let filler = (this.partialBlocks.length > 0 ? this.partialBlocks.charAt(this.partialBlocks.length - 1) : "█").repeat(fullBars);
232
+ if (frac > 0) {
233
+ const subLen = stringWidth(this.partialBlocks) + 1;
234
+ const si = Math.floor(frac * subLen);
235
+ if (si > 0) filler += this.partialBlocks.charAt(si - 1);
236
+ }
237
+ let barStr;
238
+ if (this.emptyBlock === " ") barStr = this.colors.bgBlackBright(this.colors[fgColor](filler.padEnd(innerWidth, this.emptyBlock)));
239
+ else {
240
+ const emptyStr = this.colors.blackBright(this.emptyBlock.repeat(Math.max(0, innerWidth - stringWidth(filler))));
241
+ barStr = this.colors[fgColor](filler) + emptyStr;
242
+ }
243
+ const fullLine = available < 6 ? percentStr.trim().slice(0, available) : `${percentStr} ${barStr}${suffix}`;
233
244
  if (fullLine === this.lastLine) return;
234
245
  this.lastLine = fullLine;
235
246
  this.writeLine(fullLine);
@@ -237,9 +248,11 @@ var ProgressBar = class {
237
248
  }
238
249
  writeLine(str) {
239
250
  const len = stringWidth(str);
240
- if (this.lastStrLength) {
241
- str = `\r${str}`;
242
- if (this.lastStrLength > len) str += " ".repeat(this.lastStrLength - len);
251
+ if (process.stdout.isTTY) str = `\r\u001b[2K${str}`;
252
+ else if (this.lastStrLength) {
253
+ const available = Math.max(0, (process.stdout.columns ?? 80) - 1);
254
+ const padding = Math.max(0, Math.min(this.lastStrLength, available) - len);
255
+ str = `\r${str}${" ".repeat(padding)}`;
243
256
  }
244
257
  process.stdout.write(str);
245
258
  this.lastStrLength = len;
@@ -303,7 +316,10 @@ var ProgressBar = class {
303
316
  * @returns Nothing.
304
317
  */
305
318
  clear() {
306
- if (this.lastStrLength) process.stdout.write(`\r${" ".repeat(this.lastStrLength)}\r`);
319
+ if (this.lastStrLength) {
320
+ const available = Math.max(0, (process.stdout.columns ?? 80) - 1);
321
+ process.stdout.write(process.stdout.isTTY ? "\r\x1B[2K" : `\r${" ".repeat(Math.min(this.lastStrLength, available))}\r`);
322
+ }
307
323
  }
308
324
  /**
309
325
  * Complete the progress bar, render it in the completion color, and write a newline.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpen/progressbar",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
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-04T20:49:25.311-07:00",
33
+ "buildDate": "2026-10-04T22:25:30.365-07:00",
34
34
  "hgChangesetId": "36f79ac2bb8e+",
35
- "sha256": "7716c28611411cfd7826d4a6e5006281626208711a5de2903b23d39efcc73091"
35
+ "sha256": "5870a49673de1536219220bef58b8b98d35b3a223ca37c45e50dcbb138a90d1d"
36
36
  }
37
37
  }