From 88dc9f96a3429883a7bfd11180362b2975e1dbc5 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 20 Feb 2025 00:27:21 +0100 Subject: [PATCH 1/9] initial commit --- cli/unstable_progress_bar.ts | 40 +++++++++++++++-------------- cli/unstable_progress_bar_stream.ts | 2 +- cli/unstable_progress_bar_test.ts | 8 +++--- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index 1cc1ee8e2309..bb13d00204fd 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -86,8 +86,8 @@ export interface ProgressBarOptions { /** * `ProgressBar` is a customisable class that reports updates to a - * {@link WritableStream} on a 1s interval. Progress is communicated by calling - * the `ProgressBar.add(x: number)` method. + * {@link WritableStream} on a 1s interval. Progress is communicated by using + * the `ProgressBar.value` property. * * @experimental **UNSTABLE**: New API, yet to be vetted. * @@ -107,7 +107,7 @@ export interface ProgressBarOptions { * const bar = new ProgressBar(Deno.stdout.writable, { max: 100_000 }); * * for await (const buffer of gen) { - * bar.add(buffer.length); + * bar.value += buffer.length; * await writer.write(buffer); * } * @@ -128,14 +128,25 @@ export interface ProgressBarOptions { * }); * * for (const x of Array(100)) { - * bar.add(1); + * bar.value += 1; * await delay(Math.random() * 500); * } * * bar.end(); */ export class ProgressBar { - #options: Required; + value: number; + + #options: { + max: number; + barLength: number; + fillChar: string; + emptyChar: string; + clear: boolean; + fmt: (fmt: ProgressBarFormatter) => string; + keepOpen: boolean; + }; + #unit: string; #rate: number; #writer: WritableStreamDefaultWriter; @@ -153,8 +164,8 @@ export class ProgressBar { writable: WritableStream, options: ProgressBarOptions, ) { + this.value = options.value ?? 0; this.#options = { - value: options.value ?? 0, max: options.max, barLength: options.barLength ?? 50, fillChar: options.fillChar ?? "#", @@ -191,12 +202,12 @@ export class ProgressBar { this.#id = setInterval(() => this.#print(), 1000); this.#startTime = performance.now(); this.#lastTime = this.#startTime; - this.#lastValue = this.#options.value; + this.#lastValue = this.value; } async #print(): Promise { const currentTime = performance.now(); - const size = this.#options.value / + const size = this.value / this.#options.max * this.#options.barLength | 0; const unit = this.#unit; @@ -228,25 +239,16 @@ export class ProgressBar { "] ", time: currentTime - this.#startTime, previousTime: this.#lastTime - this.#startTime, - value: this.#options.value, + value: this.value, previousValue: this.#lastValue, max: this.#options.max, }; this.#lastTime = currentTime; - this.#lastValue = this.#options.value; + this.#lastValue = this.value; await this.#writer.write("\r\u001b[K" + this.#options.fmt(x)) .catch(() => {}); } - /** - * Increments the progress by `x`. - * - * @param x The amount of progress that has been made. - */ - add(x: number): void { - this.#options.value += x; - } - /** * Ends the progress bar and cleans up any lose ends. */ diff --git a/cli/unstable_progress_bar_stream.ts b/cli/unstable_progress_bar_stream.ts index 2baa9a2cc55e..0d49297f927d 100644 --- a/cli/unstable_progress_bar_stream.ts +++ b/cli/unstable_progress_bar_stream.ts @@ -43,7 +43,7 @@ export class ProgressBarStream extends TransformStream { bar = new ProgressBar(writable, options); }, transform(chunk, controller) { - bar?.add(chunk.length); + if (bar) bar.value += chunk.length; controller.enqueue(chunk); }, flush(_controller) { diff --git a/cli/unstable_progress_bar_test.ts b/cli/unstable_progress_bar_test.ts index eac413eb82a7..4741b6b9a2c4 100644 --- a/cli/unstable_progress_bar_test.ts +++ b/cli/unstable_progress_bar_test.ts @@ -17,7 +17,7 @@ Deno.test("ProgressBar() outputs default result", async () => { const { readable, writable } = new TransformStream(); const bar = new ProgressBar(writable, { max: 10 * 1000 }); - for await (const a of getData(10, 1000)) bar.add(a.length); + for await (const a of getData(10, 1000)) bar.value += a.length; bar.end().then(() => writable.close()); for await (const buffer of readable) { @@ -66,7 +66,7 @@ Deno.test("ProgressBar() can handle a readable.cancel() correctly", async () => const { readable, writable } = new TransformStream(); const bar = new ProgressBar(writable, { max: 10 * 1000 }); - for await (const a of getData(10, 1000)) bar.add(a.length); + for await (const a of getData(10, 1000)) bar.value += a.length; bar.end(); await readable.cancel(); @@ -79,7 +79,7 @@ Deno.test("ProgressBar() can remove itself when finished", async () => { clear: true, }); - for await (const a of getData(10, 1000)) bar.add(a.length); + for await (const a of getData(10, 1000)) bar.value += a.length; bar.end() .then(() => writable.close()); @@ -104,7 +104,7 @@ Deno.test("ProgressBar() passes correct values to formatter", async () => { }, }); - for await (const a of getData(10, 1000)) bar.add(a.length); + for await (const a of getData(10, 1000)) bar.value += a.length; bar.end(); await new Response(readable).bytes(); From 07bf3445e7d9421f1d1abf1ad57ffdc5025b0461 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 22 Feb 2025 09:47:14 +0100 Subject: [PATCH 2/9] update --- cli/unstable_progress_bar.ts | 50 +++++++++++++++++------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index bb13d00204fd..a130a801f1b4 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -136,16 +136,14 @@ export interface ProgressBarOptions { */ export class ProgressBar { value: number; + max: number; - #options: { - max: number; - barLength: number; - fillChar: string; - emptyChar: string; - clear: boolean; - fmt: (fmt: ProgressBarFormatter) => string; - keepOpen: boolean; - }; + #barLength: number; + #fillChar: string; + #emptyChar: string; + #clear: boolean; + #fmt: (fmt: ProgressBarFormatter) => string; + #keepOpen: boolean; #unit: string; #rate: number; @@ -165,17 +163,15 @@ export class ProgressBar { options: ProgressBarOptions, ) { this.value = options.value ?? 0; - this.#options = { - max: options.max, - barLength: options.barLength ?? 50, - fillChar: options.fillChar ?? "#", - emptyChar: options.emptyChar ?? "-", - clear: options.clear ?? false, - fmt: options.fmt ?? function (x) { - return x.styledTime() + x.progressBar + x.styledData(); - }, - keepOpen: options.keepOpen ?? true, + this.max = options.max; + this.#barLength = options.barLength ?? 50; + this.#fillChar = options.fillChar ?? "#"; + this.#emptyChar = options.emptyChar ?? "-"; + this.#clear = options.clear ?? false; + this.#fmt = options.fmt ?? function (x: ProgressBarFormatter) { + return x.styledTime() + x.progressBar + x.styledData(); }; + this.#keepOpen = options.keepOpen ?? true; if (options.max < 2 ** 20) { this.#unit = "KiB"; @@ -196,7 +192,7 @@ export class ProgressBar { const stream = new TextEncoderStream(); stream.readable - .pipeTo(writable, { preventClose: this.#options.keepOpen }) + .pipeTo(writable, { preventClose: this.#keepOpen }) .catch(() => clearInterval(this.#id)); this.#writer = stream.writable.getWriter(); this.#id = setInterval(() => this.#print(), 1000); @@ -208,8 +204,8 @@ export class ProgressBar { async #print(): Promise { const currentTime = performance.now(); const size = this.value / - this.#options.max * - this.#options.barLength | 0; + this.max * + this.#barLength | 0; const unit = this.#unit; const rate = this.#rate; const x: ProgressBarFormatter = { @@ -234,18 +230,18 @@ export class ProgressBar { "] "; }, progressBar: "[" + - this.#options.fillChar.repeat(size) + - this.#options.emptyChar.repeat(this.#options.barLength - size) + + this.#fillChar.repeat(size) + + this.#emptyChar.repeat(this.#barLength - size) + "] ", time: currentTime - this.#startTime, previousTime: this.#lastTime - this.#startTime, value: this.value, previousValue: this.#lastValue, - max: this.#options.max, + max: this.max, }; this.#lastTime = currentTime; this.#lastValue = this.value; - await this.#writer.write("\r\u001b[K" + this.#options.fmt(x)) + await this.#writer.write("\r\u001b[K" + this.#fmt(x)) .catch(() => {}); } @@ -255,7 +251,7 @@ export class ProgressBar { async end(): Promise { clearInterval(this.#id); await this.#print() - .then(() => this.#writer.write(this.#options.clear ? "\r\u001b[K" : "\n")) + .then(() => this.#writer.write(this.#clear ? "\r\u001b[K" : "\n")) .then(() => this.#writer.close()) .catch(() => {}); } From 6daf4ec4d72869d1c1226587757ea07532220d7b Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 21 Mar 2025 11:59:50 +0900 Subject: [PATCH 3/9] clean up --- cli/unstable_progress_bar.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index ce08d23c3904..d81cf2a0d275 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -164,6 +164,7 @@ export class ProgressBar { ) { const { value = 0, + max, barLength = 50, fillChar = "#", emptyChar = "-", @@ -171,8 +172,8 @@ export class ProgressBar { fmt = (x) => x.styledTime() + x.progressBar + x.styledData(), keepOpen = true, } = options; - this.value = options.value ?? 0; - this.max = options.max; + this.value = value; + this.max = max; this.#barLength = barLength; this.#fillChar = fillChar; this.#emptyChar = emptyChar; From 8aab2f7e14a88c1059d449eeeba152d6e972db1d Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 21 Mar 2025 12:01:09 +0900 Subject: [PATCH 4/9] fix conflict resolution error --- cli/unstable_progress_bar.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index d81cf2a0d275..73d0021adce7 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -211,7 +211,7 @@ export class ProgressBar { async #print(): Promise { const currentTime = performance.now(); - const size = this.value / this.#max * this.#barLength | 0; + const size = this.value / this.max * this.#barLength | 0; const unit = this.#unit; const rate = this.#rate; const x: ProgressBarFormatter = { From fbe40875571479f02037bcaaf6ef677d202d59e8 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 1 Apr 2025 10:26:10 +0200 Subject: [PATCH 5/9] add jsdoc --- cli/unstable_progress_bar.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index 8c9e13152c65..c8218f53b4bb 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -140,7 +140,13 @@ export interface ProgressBarOptions { * bar.end(); */ export class ProgressBar { + /** + * The current progress that has been completed. + */ value: number; + /** + * The maximum progress that is expected. + */ max: number; #unit: string; From 8f181f43d0f3c56ba2f70627216a63a5c943c849 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 1 Apr 2025 14:16:10 +0200 Subject: [PATCH 6/9] update jsdoc --- cli/unstable_progress_bar.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index c8218f53b4bb..1724060c8158 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -142,10 +142,20 @@ export interface ProgressBarOptions { export class ProgressBar { /** * The current progress that has been completed. + * @example Usage + * ```ts no-assert + * const progressBar = new ProgressBar(Deno.stdout.writable, { max : 10 }); + * progressBar.value += 1 + * ``` */ value: number; /** * The maximum progress that is expected. + * @example Usage + * ```ts no-assert + * const progressBar = new ProgressBar(Deno.stdout.writable, { max : 1 }); + * progressBar.max = 100 + * ``` */ max: number; From 415282a05cb058cd9f62e5c072237ba8057489f2 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 2 Apr 2025 09:31:08 +0200 Subject: [PATCH 7/9] update --- cli/unstable_progress_bar.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index 1724060c8158..71b3a385f389 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -144,8 +144,14 @@ export class ProgressBar { * The current progress that has been completed. * @example Usage * ```ts no-assert + * import { ProgressBar } from "@std/cli/unstable-progress-bar"; + * * const progressBar = new ProgressBar(Deno.stdout.writable, { max : 10 }); * progressBar.value += 1 + * + * // do stuff + * + * progressBar.end() * ``` */ value: number; @@ -153,8 +159,14 @@ export class ProgressBar { * The maximum progress that is expected. * @example Usage * ```ts no-assert + * import { ProgressBar } from "@std/cli/unstable-progress-bar"; + * * const progressBar = new ProgressBar(Deno.stdout.writable, { max : 1 }); * progressBar.max = 100 + * + * // do stuff + * + * progressBar.end() * ``` */ max: number; From 69bf849f50f51aae1b9b66013e3834e194856463 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 25 Apr 2025 09:37:25 +0200 Subject: [PATCH 8/9] update --- cli/unstable_progress_bar.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index 5191bf815b76..68498b9f1c5b 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -155,7 +155,7 @@ const UNIT_RATE_MAP = new Map([ * await delay(Math.random() * 500); * } * - * bar.end(); + * await bar.end(); */ export class ProgressBar { /** @@ -169,7 +169,7 @@ export class ProgressBar { * * // do stuff * - * progressBar.end() + * await progressBar.end() * ``` */ value: number; @@ -184,7 +184,7 @@ export class ProgressBar { * * // do stuff * - * progressBar.end() + * await progressBar.end() * ``` */ max: number; From 53c02cf591b3eba52777eeea347da749869f359f Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 20 May 2025 15:00:56 +0200 Subject: [PATCH 9/9] update --- cli/unstable_progress_bar.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index ae9876160bf2..4cae047920bd 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -138,7 +138,7 @@ const UNIT_RATE_MAP = new Map([ * await writer.write(buffer); * } * - * await bar.end(); + * await bar.stop(); * await writer.close(); * ``` * @@ -159,7 +159,7 @@ const UNIT_RATE_MAP = new Map([ * await delay(Math.random() * 500); * } * - * await bar.end(); + * await bar.stop(); */ export class ProgressBar { /** @@ -168,12 +168,12 @@ export class ProgressBar { * ```ts no-assert * import { ProgressBar } from "@std/cli/unstable-progress-bar"; * - * const progressBar = new ProgressBar(Deno.stdout.writable, { max : 10 }); - * progressBar.value += 1 + * const progressBar = new ProgressBar({ max : 10 }); + * progressBar.value += 1; * * // do stuff * - * await progressBar.end() + * await progressBar.stop(); * ``` */ value: number; @@ -183,12 +183,12 @@ export class ProgressBar { * ```ts no-assert * import { ProgressBar } from "@std/cli/unstable-progress-bar"; * - * const progressBar = new ProgressBar(Deno.stdout.writable, { max : 1 }); - * progressBar.max = 100 + * const progressBar = new ProgressBar({ max : 1 }); + * progressBar.max = 100; * * // do stuff * - * await progressBar.end() + * await progressBar.stop(); * ``` */ max: number; @@ -286,8 +286,8 @@ export class ProgressBar { * ```ts ignore * import { ProgressBar } from "@std/cli/unstable-progress-bar"; * - * const progressBar = new ProgressBar(Deno.stdout.writable, { max: 100 }); - * await progressBar.end() + * const progressBar = new ProgressBar({ max: 100 }); + * await progressBar.stop() * ``` */ async stop(): Promise {