@mpen/progressbar 0.1.0

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.
@@ -0,0 +1,257 @@
1
+ //#region src/index.d.ts
2
+ /**
3
+ * Preset character sets for fractional progress bar segments.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * const progress = new ProgressBar({
8
+ * max: 100,
9
+ * partialBlocks: ProgressBarPartialBlocks.shades,
10
+ * })
11
+ * ```
12
+ */
13
+ declare const ProgressBarPartialBlocks: {
14
+ /**
15
+ * Left-to-right eighth-block segments.
16
+ */
17
+ readonly smooth: "▏▎▍▌▋▊▉█";
18
+ /**
19
+ * Bottom-to-top block segments.
20
+ */
21
+ readonly vertical: "▁▂▃▄▅▆▇█";
22
+ /**
23
+ * Light-to-heavy shade segments.
24
+ */
25
+ readonly shades: "░▒▓█";
26
+ };
27
+ /**
28
+ * Built-in fractional progress bar segment character sets.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const smooth: ProgressBarPartialBlockPreset = ProgressBarPartialBlocks.smooth
33
+ * ```
34
+ */
35
+ type ProgressBarPartialBlockPreset = (typeof ProgressBarPartialBlocks)[keyof typeof ProgressBarPartialBlocks];
36
+ /**
37
+ * Options that control a [`ProgressBar`]{@link ProgressBar} instance.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * const progress = new ProgressBar({
42
+ * max: 100,
43
+ * partialBlocks: ProgressBarPartialBlocks.shades,
44
+ * })
45
+ * ```
46
+ */
47
+ interface ProgressBarOptions {
48
+ /**
49
+ * The total number of items represented by a complete progress bar.
50
+ */
51
+ max?: number;
52
+ /**
53
+ * The rendered bar width in terminal columns.
54
+ */
55
+ width?: number | null;
56
+ /**
57
+ * Whether to start rendering immediately when the instance is created.
58
+ */
59
+ start?: boolean;
60
+ /**
61
+ * Maximum render frames per second.
62
+ */
63
+ fps?: number;
64
+ /**
65
+ * Characters used for fractional bar segments between whole terminal cells.
66
+ */
67
+ partialBlocks?: ProgressBarPartialBlockPreset | string;
68
+ }
69
+ /**
70
+ * Render an updating progress bar to stdout.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const progress = new ProgressBar({ max: files.length })
75
+ *
76
+ * for (const file of files) {
77
+ * await processFile(file)
78
+ * progress.tick()
79
+ * }
80
+ *
81
+ * progress.complete()
82
+ * ```
83
+ */
84
+ declare class ProgressBar {
85
+ /**
86
+ * Current completed item count.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * progress.update(5)
91
+ * ```
92
+ */
93
+ current: number;
94
+ /**
95
+ * Total item count represented by a complete progress bar.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * progress.max = 100
100
+ * ```
101
+ */
102
+ max: number;
103
+ /**
104
+ * Rendered bar width in terminal columns.
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * progress.width = 40
109
+ * ```
110
+ */
111
+ width: number;
112
+ /** @internal */
113
+ msPerFrame: number;
114
+ /**
115
+ * Characters used for fractional bar segments between whole terminal cells.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * progress.partialBlocks = ProgressBarPartialBlocks.shades
120
+ * ```
121
+ */
122
+ partialBlocks: string;
123
+ private intFmt;
124
+ private oneDecimalFmt;
125
+ private twoDecimalFmt;
126
+ private lastLine?;
127
+ private lastRenderTime?;
128
+ private lastStrLength?;
129
+ private startTime;
130
+ /**
131
+ * Create a progress bar.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const progress = new ProgressBar({ max: 50, width: 30, start: false })
136
+ * progress.start()
137
+ * ```
138
+ *
139
+ * @param options - Progress bar behavior and rendering options.
140
+ */
141
+ constructor(options?: ProgressBarOptions);
142
+ /**
143
+ * Create a progress bar.
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * const progress = new ProgressBar(50, 30, true)
148
+ * ```
149
+ *
150
+ * @param max - The total number of items represented by a complete progress bar.
151
+ * @param width - The rendered bar width in terminal columns.
152
+ * @param start - Whether to start rendering immediately when the instance is created.
153
+ */
154
+ constructor(max?: number, width?: number | null, start?: boolean);
155
+ /**
156
+ * Start the progress timer and render the initial bar.
157
+ *
158
+ * @example
159
+ * ```ts
160
+ * const progress = new ProgressBar({ max: 10, start: false })
161
+ * progress.start()
162
+ * ```
163
+ *
164
+ * @returns Nothing.
165
+ */
166
+ start(): void;
167
+ private render;
168
+ /**
169
+ * Advance the progress bar by a number of items.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * progress.tick()
174
+ * progress.tick(5)
175
+ * ```
176
+ *
177
+ * @param items - Number of completed items to add to the current progress.
178
+ * @returns Nothing.
179
+ */
180
+ tick(items?: number): void;
181
+ private renderNow;
182
+ private renderNowWithColor;
183
+ private writeLine;
184
+ /**
185
+ * Write a log line without permanently disrupting the active progress bar.
186
+ *
187
+ * @example
188
+ * ```ts
189
+ * progress.logLine('Downloaded manifest')
190
+ * progress.tick()
191
+ * ```
192
+ *
193
+ * @param line - Log line to write above the redrawn progress bar.
194
+ * @returns Nothing.
195
+ */
196
+ logLine(line: string): void;
197
+ /**
198
+ * Set the absolute current progress and render the bar.
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * progress.update(25)
203
+ * ```
204
+ *
205
+ * @param progress - Absolute current progress value.
206
+ * @returns Nothing.
207
+ */
208
+ update(progress: number): void;
209
+ /**
210
+ * Mark the current output as interrupted and write a newline.
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * progress.interrupt()
215
+ * ```
216
+ *
217
+ * @returns Nothing.
218
+ */
219
+ interrupt(): void;
220
+ /**
221
+ * Clear the last rendered progress line.
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * progress.clear()
226
+ * ```
227
+ *
228
+ * @returns Nothing.
229
+ */
230
+ clear(): void;
231
+ /**
232
+ * Complete the progress bar, render it in the completion color, and write a newline.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * progress.complete()
237
+ * ```
238
+ *
239
+ * @returns Nothing.
240
+ */
241
+ complete(): void;
242
+ /**
243
+ * Whether the progress value has reached the configured maximum.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * if (progress.isComplete) {
248
+ * progress.clear()
249
+ * }
250
+ * ```
251
+ *
252
+ * @returns True when `current` is greater than or equal to `max`.
253
+ */
254
+ get isComplete(): boolean;
255
+ }
256
+ //#endregion
257
+ export { ProgressBarOptions, ProgressBarPartialBlockPreset, ProgressBarPartialBlocks, ProgressBar as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,290 @@
1
+ import chalk from "chalk";
2
+ import stringWidth from "string-width";
3
+ //#region src/index.ts
4
+ /**
5
+ * Preset character sets for fractional progress bar segments.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const progress = new ProgressBar({
10
+ * max: 100,
11
+ * partialBlocks: ProgressBarPartialBlocks.shades,
12
+ * })
13
+ * ```
14
+ */
15
+ const ProgressBarPartialBlocks = {
16
+ /**
17
+ * Left-to-right eighth-block segments.
18
+ */
19
+ smooth: "▏▎▍▌▋▊▉█",
20
+ /**
21
+ * Bottom-to-top block segments.
22
+ */
23
+ vertical: "▁▂▃▄▅▆▇█",
24
+ /**
25
+ * Light-to-heavy shade segments.
26
+ */
27
+ shades: "░▒▓█"
28
+ };
29
+ /**
30
+ * Render an updating progress bar to stdout.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const progress = new ProgressBar({ max: files.length })
35
+ *
36
+ * for (const file of files) {
37
+ * await processFile(file)
38
+ * progress.tick()
39
+ * }
40
+ *
41
+ * progress.complete()
42
+ * ```
43
+ */
44
+ var ProgressBar = class {
45
+ /**
46
+ * Current completed item count.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * progress.update(5)
51
+ * ```
52
+ */
53
+ current = 0;
54
+ /**
55
+ * Total item count represented by a complete progress bar.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * progress.max = 100
60
+ * ```
61
+ */
62
+ max;
63
+ /**
64
+ * Rendered bar width in terminal columns.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * progress.width = 40
69
+ * ```
70
+ */
71
+ width;
72
+ /** @internal */
73
+ msPerFrame;
74
+ /**
75
+ * Characters used for fractional bar segments between whole terminal cells.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * progress.partialBlocks = ProgressBarPartialBlocks.shades
80
+ * ```
81
+ */
82
+ partialBlocks;
83
+ intFmt;
84
+ oneDecimalFmt;
85
+ twoDecimalFmt;
86
+ lastLine;
87
+ lastRenderTime;
88
+ lastStrLength;
89
+ startTime = process.hrtime.bigint();
90
+ constructor(maxOrOptions = 1, width = null, start = true) {
91
+ const options = typeof maxOrOptions === "object" ? maxOrOptions : {
92
+ max: maxOrOptions,
93
+ width,
94
+ start
95
+ };
96
+ this.max = options.max ?? 1;
97
+ this.width = options.width || Math.max((process.stdout.columns ?? 80) - 40, 16);
98
+ this.msPerFrame = 1e3 / (options.fps ?? 30);
99
+ this.partialBlocks = options.partialBlocks ?? ProgressBarPartialBlocks.smooth;
100
+ this.intFmt = Intl.NumberFormat(void 0, {});
101
+ this.oneDecimalFmt = Intl.NumberFormat(void 0, {
102
+ minimumFractionDigits: 1,
103
+ maximumFractionDigits: 1
104
+ });
105
+ this.twoDecimalFmt = Intl.NumberFormat(void 0, {
106
+ minimumFractionDigits: 2,
107
+ maximumFractionDigits: 2
108
+ });
109
+ if (options.start ?? true) this.start();
110
+ }
111
+ /**
112
+ * Start the progress timer and render the initial bar.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * const progress = new ProgressBar({ max: 10, start: false })
117
+ * progress.start()
118
+ * ```
119
+ *
120
+ * @returns Nothing.
121
+ */
122
+ start() {
123
+ this.startTime = process.hrtime.bigint();
124
+ this.render();
125
+ }
126
+ render() {
127
+ if (!this.lastRenderTime || Date.now() - this.lastRenderTime > this.msPerFrame) this.renderNow();
128
+ }
129
+ /**
130
+ * Advance the progress bar by a number of items.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * progress.tick()
135
+ * progress.tick(5)
136
+ * ```
137
+ *
138
+ * @param items - Number of completed items to add to the current progress.
139
+ * @returns Nothing.
140
+ */
141
+ tick(items = 1) {
142
+ this.update(this.current + items);
143
+ }
144
+ renderNow() {
145
+ this.renderNowWithColor("greenBright");
146
+ }
147
+ renderNowWithColor(fgColor) {
148
+ const percent = this.current < this.max ? this.current / this.max : 1;
149
+ const innerWidth = this.width - 6;
150
+ const percentStr = `${String(Math.floor(percent * 100 + .25)).padStart(4, " ")}%`;
151
+ const barLength = percent * innerWidth;
152
+ const fullBars = Math.floor(barLength);
153
+ const frac = barLength - fullBars;
154
+ const elapsed = process.hrtime.bigint() - this.startTime;
155
+ const elapsedSeconds = Number(elapsed / 10000n) / 1e5;
156
+ const quantStr = this.current === this.max ? chalk.whiteBright(this.intFmt.format(this.max)) : `${chalk.whiteBright(this.intFmt.format(this.current))}/${chalk.whiteBright(this.intFmt.format(this.max))}`;
157
+ let filler = "█".repeat(fullBars);
158
+ if (frac > 0) {
159
+ const subLen = stringWidth(this.partialBlocks) + 1;
160
+ const si = Math.floor(frac * subLen);
161
+ if (si > 0) filler += this.partialBlocks.charAt(si - 1);
162
+ }
163
+ let fullLine = `${percentStr} ${chalk.bgBlackBright[fgColor](filler.padEnd(innerWidth, " "))} ${quantStr}`;
164
+ if (this.current > 0) {
165
+ let estimatedRemaining = 0;
166
+ if (this.current === this.max) estimatedRemaining = elapsedSeconds;
167
+ else if (elapsedSeconds > 0) {
168
+ const progressRate = this.current / elapsedSeconds;
169
+ estimatedRemaining = (this.max - this.current) / progressRate;
170
+ }
171
+ if (estimatedRemaining >= 60 || elapsedSeconds >= 60) {
172
+ const minutes = Math.floor(estimatedRemaining / 60);
173
+ const seconds = Math.floor(estimatedRemaining % 60);
174
+ fullLine += ` │ ${minutes}m${String(seconds).padStart(2, "0")}s`;
175
+ } else {
176
+ const secondsFmt = estimatedRemaining < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
177
+ fullLine += ` │ ${secondsFmt.format(estimatedRemaining)}s`;
178
+ }
179
+ const itemsPerSecond = this.current > 0 ? this.current / elapsedSeconds : 0;
180
+ const rateFmt = itemsPerSecond < 10 ? this.twoDecimalFmt : this.oneDecimalFmt;
181
+ fullLine += ` │ ${rateFmt.format(itemsPerSecond)} it/s`;
182
+ }
183
+ if (fullLine === this.lastLine) return;
184
+ this.lastLine = fullLine;
185
+ this.writeLine(fullLine);
186
+ this.lastRenderTime = Date.now();
187
+ }
188
+ writeLine(str) {
189
+ const len = stringWidth(str);
190
+ if (this.lastStrLength) {
191
+ str = `\r${str}`;
192
+ if (this.lastStrLength > len) str += " ".repeat(this.lastStrLength - len);
193
+ }
194
+ process.stdout.write(str);
195
+ this.lastStrLength = len;
196
+ }
197
+ /**
198
+ * Write a log line without permanently disrupting the active progress bar.
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * progress.logLine('Downloaded manifest')
203
+ * progress.tick()
204
+ * ```
205
+ *
206
+ * @param line - Log line to write above the redrawn progress bar.
207
+ * @returns Nothing.
208
+ */
209
+ logLine(line) {
210
+ this.clear();
211
+ process.stdout.write(`${line}\n`);
212
+ this.lastLine = void 0;
213
+ this.lastStrLength = void 0;
214
+ this.renderNow();
215
+ }
216
+ /**
217
+ * Set the absolute current progress and render the bar.
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * progress.update(25)
222
+ * ```
223
+ *
224
+ * @param progress - Absolute current progress value.
225
+ * @returns Nothing.
226
+ */
227
+ update(progress) {
228
+ this.current = progress;
229
+ this.render();
230
+ }
231
+ /**
232
+ * Mark the current output as interrupted and write a newline.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * progress.interrupt()
237
+ * ```
238
+ *
239
+ * @returns Nothing.
240
+ */
241
+ interrupt() {
242
+ this.renderNowWithColor("redBright");
243
+ process.stdout.write("\n");
244
+ }
245
+ /**
246
+ * Clear the last rendered progress line.
247
+ *
248
+ * @example
249
+ * ```ts
250
+ * progress.clear()
251
+ * ```
252
+ *
253
+ * @returns Nothing.
254
+ */
255
+ clear() {
256
+ if (this.lastStrLength) process.stdout.write(`\r${" ".repeat(this.lastStrLength)}\r`);
257
+ }
258
+ /**
259
+ * Complete the progress bar, render it in the completion color, and write a newline.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * progress.complete()
264
+ * ```
265
+ *
266
+ * @returns Nothing.
267
+ */
268
+ complete() {
269
+ this.current = this.max;
270
+ this.renderNowWithColor("blueBright");
271
+ process.stdout.write("\n");
272
+ }
273
+ /**
274
+ * Whether the progress value has reached the configured maximum.
275
+ *
276
+ * @example
277
+ * ```ts
278
+ * if (progress.isComplete) {
279
+ * progress.clear()
280
+ * }
281
+ * ```
282
+ *
283
+ * @returns True when `current` is greater than or equal to `max`.
284
+ */
285
+ get isComplete() {
286
+ return this.current >= this.max;
287
+ }
288
+ };
289
+ //#endregion
290
+ export { ProgressBarPartialBlocks, ProgressBar as default };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@mpen/progressbar",
3
+ "version": "0.1.0",
4
+ "description": "Terminal progress bar utilities.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "exports": {
15
+ ".": "./dist/index.mjs",
16
+ "./package.json": "./package.json"
17
+ },
18
+ "types": "./dist/index.d.mts",
19
+ "dependencies": {
20
+ "chalk": "^5",
21
+ "string-width": "^8.2.0"
22
+ },
23
+ "devDependencies": {
24
+ "@types/bun": "latest",
25
+ "tsdown": "^0.21",
26
+ "typescript": "^6"
27
+ },
28
+ "scripts": {
29
+ "build": "bun run --bun tsdown",
30
+ "dev": "bun src/dev.ts"
31
+ }
32
+ }