From 6568bbe4a40b9cf10d000dc179cd16a6c6e93704 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 21 May 2025 11:01:02 +0200 Subject: [PATCH 1/4] initial commit --- cli/_unit.ts | 31 +++++ cli/unstable_progress_bar.ts | 66 ++++----- cli/unstable_progress_bar_test.ts | 222 ++++++++++++++++++++++-------- 3 files changed, 230 insertions(+), 89 deletions(-) create mode 100644 cli/_unit.ts diff --git a/cli/_unit.ts b/cli/_unit.ts new file mode 100644 index 000000000000..096aff3d44f6 --- /dev/null +++ b/cli/_unit.ts @@ -0,0 +1,31 @@ +// Copyright 2018-2025 the Deno authors. MIT license. + +export type Unit = "KiB" | "MiB" | "GiB" | "TiB" | "PiB"; + +function getUnit(max: number): Unit { + if (max < 2 ** 20) return "KiB"; + if (max < 2 ** 30) return "MiB"; + if (max < 2 ** 40) return "GiB"; + if (max < 2 ** 50) return "TiB"; + return "PiB"; +} + +const UNIT_RATE_MAP = new Map([ + ["KiB", 2 ** 10], + ["MiB", 2 ** 20], + ["GiB", 2 ** 30], + ["TiB", 2 ** 40], + ["PiB", 2 ** 50], +]); + +export function formatUnitFraction( + value: number, + max: number, + fractionDigits: number, +): string { + const unit = getUnit(max); + const rate = UNIT_RATE_MAP.get(unit)!; + const currentValue = (value / rate).toFixed(fractionDigits); + const maxValue = (max / rate).toFixed(fractionDigits); + return `${currentValue}/${maxValue} ${unit}`; +} diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index 4cae047920bd..f22bb7953bf0 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -1,5 +1,7 @@ // Copyright 2018-2025 the Deno authors. MIT license. +import { formatUnitFraction } from "./_unit.ts"; + /** * The properties provided to the fmt function upon every visual update. */ @@ -93,24 +95,6 @@ export interface ProgressBarOptions { keepOpen?: boolean; } -type Unit = "KiB" | "MiB" | "GiB" | "TiB" | "PiB"; - -function getUnit(max: number): Unit { - if (max < 2 ** 20) return "KiB"; - if (max < 2 ** 30) return "MiB"; - if (max < 2 ** 40) return "GiB"; - if (max < 2 ** 50) return "TiB"; - return "PiB"; -} - -const UNIT_RATE_MAP = new Map([ - ["KiB", 2 ** 10], - ["MiB", 2 ** 20], - ["GiB", 2 ** 30], - ["TiB", 2 ** 40], - ["PiB", 2 ** 50], -]); - /** * `ProgressBar` is a customisable class that reports updates to a * {@link WritableStream} on a 1s interval. Progress is communicated by using @@ -162,8 +146,12 @@ const UNIT_RATE_MAP = new Map([ * await bar.stop(); */ export class ProgressBar { + #value: number; /** * The current progress that has been completed. + * + * @param value Value to set. + * * @example Usage * ```ts no-assert * import { ProgressBar } from "@std/cli/unstable-progress-bar"; @@ -176,9 +164,20 @@ export class ProgressBar { * await progressBar.stop(); * ``` */ - value: number; + set value(value: number) { + this.#value = value; + this.#print(); + } + get value(): number { + return this.#value; + } + + #max: number; /** * The maximum progress that is expected. + * + * @param max Max to set. + * * @example Usage * ```ts no-assert * import { ProgressBar } from "@std/cli/unstable-progress-bar"; @@ -191,10 +190,14 @@ export class ProgressBar { * await progressBar.stop(); * ``` */ - max: number; + set max(value: number) { + this.#max = value; + this.#print(); + } + get max(): number { + return this.#max; + } - #unit: Unit; - #rate: number; #writer: WritableStreamDefaultWriter; #id: number; #startTime: number; @@ -225,8 +228,8 @@ export class ProgressBar { fmt = (x) => `${x.styledTime()} ${x.progressBar} ${x.styledData()} `, keepOpen = true, } = options; - this.value = value; - this.max = max; + this.#value = value; + this.#max = max; this.#barLength = barLength; this.#fillChar = fillChar; this.#emptyChar = emptyChar; @@ -234,18 +237,18 @@ export class ProgressBar { this.#fmt = fmt; this.#keepOpen = keepOpen; - this.#unit = getUnit(options.max); - this.#rate = UNIT_RATE_MAP.get(this.#unit)!; - const stream = new TextEncoderStream(); stream.readable .pipeTo(writable, { preventClose: this.#keepOpen }) .catch(() => clearInterval(this.#id)); this.#writer = stream.writable.getWriter(); - this.#id = setInterval(() => this.#print(), 1000); + this.#startTime = performance.now(); this.#lastTime = this.#startTime; this.#lastValue = this.value; + + this.#id = setInterval(() => this.#print(), 1000); + this.#print(); } async #print(): Promise { @@ -262,9 +265,7 @@ export class ProgressBar { return `[${minutes}:${seconds}]`; }, styledData: (fractions = 2): string => { - const currentValue = (this.value / this.#rate).toFixed(fractions); - const maxValue = (this.max / this.#rate).toFixed(fractions); - return `[${currentValue}/${maxValue} ${this.#unit}]`; + return `[${formatUnitFraction(this.value, this.max, fractions)}]`; }, progressBar: `[${fillChars}${emptyChars}]`, time: currentTime - this.#startTime, @@ -292,8 +293,7 @@ export class ProgressBar { */ async stop(): Promise { clearInterval(this.#id); - await this.#print() - .then(() => this.#writer.write(this.#clear ? "\r\u001b[K" : "\n")) + await this.#writer.write(this.#clear ? "\r\u001b[K" : "\n") .then(() => this.#writer.close()) .catch(() => {}); } diff --git a/cli/unstable_progress_bar_test.ts b/cli/unstable_progress_bar_test.ts index aa5336a2caff..c6ee3bd0be38 100644 --- a/cli/unstable_progress_bar_test.ts +++ b/cli/unstable_progress_bar_test.ts @@ -13,75 +13,122 @@ async function* getData( } } +const decoder = new TextDecoder(); + Deno.test("ProgressBar() outputs default result", async () => { const { readable, writable } = new TransformStream(); + const bar = new ProgressBar({ writable, max: 10 * 1000 }); + for (let index = 0; index < 10; index++) { + bar.value += 1000; + await new Promise((resolve) => setTimeout(resolve, 10)); + } + bar.stop().then(() => writable.close()); - for await (const a of getData(10, 1000)) bar.value += a.length; + const expected = [ + "\r\u001b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB] ", + "\r\u001b[K[00:00] [#####---------------------------------------------] [0.98/9.77 KiB] ", + "\r\u001b[K[00:00] [##########----------------------------------------] [1.95/9.77 KiB] ", + "\r\u001b[K[00:00] [###############-----------------------------------] [2.93/9.77 KiB] ", + "\r\u001b[K[00:00] [####################------------------------------] [3.91/9.77 KiB] ", + "\r\u001b[K[00:00] [#########################-------------------------] [4.88/9.77 KiB] ", + "\r\u001b[K[00:00] [##############################--------------------] [5.86/9.77 KiB] ", + "\r\u001b[K[00:00] [###################################---------------] [6.84/9.77 KiB] ", + "\r\u001b[K[00:00] [########################################----------] [7.81/9.77 KiB] ", + "\r\u001b[K[00:00] [#############################################-----] [8.79/9.77 KiB] ", + "\r\u001b[K[00:00] [##################################################] [9.77/9.77 KiB] ", + "\n", + ]; + + let index = 0; + for await (const buffer of readable) { + const actual = decoder.decode(buffer); + assertEquals(actual, expected[index++]); + } +}); + +Deno.test("ProgressBar() outputs passing time", async () => { + const { readable, writable } = new TransformStream(); + + const bar = new ProgressBar({ writable, max: 10 * 1000 }); + await new Promise((resolve) => setTimeout(resolve, 3000)); bar.stop().then(() => writable.close()); + const expected = [ + "\r\u001b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB] ", + "\r\u001b[K[00:01] [--------------------------------------------------] [0.00/9.77 KiB] ", + "\r\u001b[K[00:02] [--------------------------------------------------] [0.00/9.77 KiB] ", + "\n", + ]; + + let index = 0; for await (const buffer of readable) { - if (buffer.length == 1) { - assertEquals(buffer[0], 10); - continue; - } - assertEquals(buffer.subarray(0, 4), Uint8Array.from([13, 27, 91, 75])); - let i = 4; - // Check Time - assertEquals(buffer[i++], 91); // [ - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(buffer[i++], 58); // : - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(buffer[i++], 93); // ] - assertEquals(buffer[i++], 32); // ' ' - // Check Progress Bar - assertEquals(buffer[i++], 91); // [] - for (let j = 0; j < 50; ++j, ++i) { - assertEquals(buffer[i] === 35 || buffer[i] === 45, true); // '#' || '-' - } - assertEquals(buffer[i++], 93); // ] - assertEquals(buffer[i++], 32); // ' ' - // Check Amount - assertEquals(buffer[i++], 91); // [ - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(buffer[i++], 46); // . - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(buffer[i++], 47); // / - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0.9 - assertEquals(buffer[i++], 46); // . - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(48 <= buffer[i] && buffer[i++] <= 58, true); // 0-9 - assertEquals(buffer[i++], 32); // ' ' - assertEquals(buffer[i++], 75); // K - assertEquals(buffer[i++], 105); // i - assertEquals(buffer[i++], 66); // B - assertEquals(buffer[i++], 93); // ] + const actual = decoder.decode(buffer); + assertEquals(actual, expected[index++]); + } +}); + +Deno.test("ProgressBar() change max", async () => { + const { readable, writable } = new TransformStream(); + + const bar = new ProgressBar({ writable, max: 2 ** 10 }); + await new Promise((resolve) => setTimeout(resolve, 100)); + bar.max = 2 ** 20; + bar.stop().then(() => writable.close()); + + const expected = [ + "\r\u001b[K[00:00] [--------------------------------------------------] [0.00/1.00 KiB] ", + "\r\u001b[K[00:00] [--------------------------------------------------] [0.00/1.00 MiB] ", + "\n", + ]; + + let index = 0; + for await (const buffer of readable) { + const actual = decoder.decode(buffer); + assertEquals(actual, expected[index++]); } }); 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.value += a.length; + const bar = new ProgressBar({ writable, max: 10 * 1000 }); + for (let index = 0; index < 10; index++) { + bar.value += 1000; + await new Promise((resolve) => setTimeout(resolve, 100)); + } bar.stop(); - await readable.cancel(); }); Deno.test("ProgressBar() can remove itself when finished", async () => { const { readable, writable } = new TransformStream(); - const bar = new ProgressBar({ writable, max: 10 * 1000, clear: true }); - for await (const a of getData(10, 1000)) bar.value += a.length; - bar.stop() - .then(() => writable.close()); + const bar = new ProgressBar({ writable, max: 10 * 1000, clear: true }); + for (let index = 0; index < 10; index++) { + bar.value += 1000; + } + bar.stop().then(() => writable.close()); + const expected = [ + "\r\u001b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB] ", + "\r\u001b[K[00:00] [#####---------------------------------------------] [0.98/9.77 KiB] ", + "\r\u001b[K[00:00] [##########----------------------------------------] [1.95/9.77 KiB] ", + "\r\u001b[K[00:00] [###############-----------------------------------] [2.93/9.77 KiB] ", + "\r\u001b[K[00:00] [####################------------------------------] [3.91/9.77 KiB] ", + "\r\u001b[K[00:00] [#########################-------------------------] [4.88/9.77 KiB] ", + "\r\u001b[K[00:00] [##############################--------------------] [5.86/9.77 KiB] ", + "\r\u001b[K[00:00] [###################################---------------] [6.84/9.77 KiB] ", + "\r\u001b[K[00:00] [########################################----------] [7.81/9.77 KiB] ", + "\r\u001b[K[00:00] [#############################################-----] [8.79/9.77 KiB] ", + "\r\u001b[K[00:00] [##################################################] [9.77/9.77 KiB] ", + "\r\x1b[K", + ]; + + let index = 0; for await (const buffer of readable) { - assertEquals(buffer.subarray(0, 4), Uint8Array.from([13, 27, 91, 75])); + const actual = decoder.decode(buffer); + assertEquals(actual, expected[index++]); } }); @@ -108,22 +155,85 @@ Deno.test("ProgressBar() passes correct values to formatter", async () => { await new Response(readable).bytes(); }); -Deno.test("ProgressBar() uses correct unit type", async () => { - const units = ["KiB", "MiB", "GiB", "TiB", "PiB"]; - let i = 0; - for (const unit of units) { +Deno.test("ProgressBar() uses correct unit type", async (t) => { + await t.step("KiB", async () => { + const expected = + "\r\x1b[K[00:00] [--------------------------------------------------] [0.00/1.00 KiB] "; const { readable, writable } = new TransformStream(); const bar = new ProgressBar({ writable, - max: 2 ** (10 * ++i), + max: 2 ** 10, keepOpen: false, }); - - const decoder = new TextDecoder(); for await (const buffer of readable) { - assertEquals(decoder.decode(buffer.subarray(-5, -2)), unit); + const actual = decoder.decode(buffer); + assertEquals(actual, expected); break; } bar.stop(); - } + }); + await t.step("MiB", async () => { + const expected = + "\r\x1b[K[00:00] [--------------------------------------------------] [0.00/1.00 MiB] "; + const { readable, writable } = new TransformStream(); + const bar = new ProgressBar({ + writable, + max: 2 ** 20, + keepOpen: false, + }); + for await (const buffer of readable) { + const actual = decoder.decode(buffer); + assertEquals(actual, expected); + break; + } + bar.stop(); + }); + await t.step("GiB", async () => { + const expected = + "\r\x1b[K[00:00] [--------------------------------------------------] [0.00/1.00 GiB] "; + const { readable, writable } = new TransformStream(); + const bar = new ProgressBar({ + writable, + max: 2 ** 30, + keepOpen: false, + }); + for await (const buffer of readable) { + const actual = decoder.decode(buffer); + assertEquals(actual, expected); + break; + } + bar.stop(); + }); + await t.step("TiB", async () => { + const expected = + "\r\x1b[K[00:00] [--------------------------------------------------] [0.00/1.00 TiB] "; + const { readable, writable } = new TransformStream(); + const bar = new ProgressBar({ + writable, + max: 2 ** 40, + keepOpen: false, + }); + for await (const buffer of readable) { + const actual = decoder.decode(buffer); + assertEquals(actual, expected); + break; + } + bar.stop(); + }); + await t.step("PiB", async () => { + const expected = + "\r\x1b[K[00:00] [--------------------------------------------------] [0.00/1.00 PiB] "; + const { readable, writable } = new TransformStream(); + const bar = new ProgressBar({ + writable, + max: 2 ** 50, + keepOpen: false, + }); + for await (const buffer of readable) { + const actual = decoder.decode(buffer); + assertEquals(actual, expected); + break; + } + bar.stop(); + }); }); From 5a0b52ea345d565c737f3ba46038a3cf7243afa4 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 21 May 2025 11:10:09 +0200 Subject: [PATCH 2/4] update --- cli/_unit.ts | 31 ------------------------------- cli/unstable_progress_bar.ts | 32 +++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 34 deletions(-) delete mode 100644 cli/_unit.ts diff --git a/cli/_unit.ts b/cli/_unit.ts deleted file mode 100644 index 096aff3d44f6..000000000000 --- a/cli/_unit.ts +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2018-2025 the Deno authors. MIT license. - -export type Unit = "KiB" | "MiB" | "GiB" | "TiB" | "PiB"; - -function getUnit(max: number): Unit { - if (max < 2 ** 20) return "KiB"; - if (max < 2 ** 30) return "MiB"; - if (max < 2 ** 40) return "GiB"; - if (max < 2 ** 50) return "TiB"; - return "PiB"; -} - -const UNIT_RATE_MAP = new Map([ - ["KiB", 2 ** 10], - ["MiB", 2 ** 20], - ["GiB", 2 ** 30], - ["TiB", 2 ** 40], - ["PiB", 2 ** 50], -]); - -export function formatUnitFraction( - value: number, - max: number, - fractionDigits: number, -): string { - const unit = getUnit(max); - const rate = UNIT_RATE_MAP.get(unit)!; - const currentValue = (value / rate).toFixed(fractionDigits); - const maxValue = (max / rate).toFixed(fractionDigits); - return `${currentValue}/${maxValue} ${unit}`; -} diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index f22bb7953bf0..e84a75c969d1 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -1,7 +1,5 @@ // Copyright 2018-2025 the Deno authors. MIT license. -import { formatUnitFraction } from "./_unit.ts"; - /** * The properties provided to the fmt function upon every visual update. */ @@ -95,6 +93,24 @@ export interface ProgressBarOptions { keepOpen?: boolean; } +type Unit = "KiB" | "MiB" | "GiB" | "TiB" | "PiB"; + +function getUnit(max: number): Unit { + if (max < 2 ** 20) return "KiB"; + if (max < 2 ** 30) return "MiB"; + if (max < 2 ** 40) return "GiB"; + if (max < 2 ** 50) return "TiB"; + return "PiB"; +} + +const UNIT_RATE_MAP = new Map([ + ["KiB", 2 ** 10], + ["MiB", 2 ** 20], + ["GiB", 2 ** 30], + ["TiB", 2 ** 40], + ["PiB", 2 ** 50], +]); + /** * `ProgressBar` is a customisable class that reports updates to a * {@link WritableStream} on a 1s interval. Progress is communicated by using @@ -192,12 +208,17 @@ export class ProgressBar { */ set max(value: number) { this.#max = value; + this.#unit = getUnit(this.#max); + this.#rate = UNIT_RATE_MAP.get(this.#unit)!; + this.#print(); } get max(): number { return this.#max; } + #unit: Unit; + #rate: number; #writer: WritableStreamDefaultWriter; #id: number; #startTime: number; @@ -237,6 +258,9 @@ export class ProgressBar { this.#fmt = fmt; this.#keepOpen = keepOpen; + this.#unit = getUnit(options.max); + this.#rate = UNIT_RATE_MAP.get(this.#unit)!; + const stream = new TextEncoderStream(); stream.readable .pipeTo(writable, { preventClose: this.#keepOpen }) @@ -265,7 +289,9 @@ export class ProgressBar { return `[${minutes}:${seconds}]`; }, styledData: (fractions = 2): string => { - return `[${formatUnitFraction(this.value, this.max, fractions)}]`; + const currentValue = (this.value / this.#rate).toFixed(fractions); + const maxValue = (this.max / this.#rate).toFixed(fractions); + return `[${currentValue}/${maxValue} ${this.#unit}]`; }, progressBar: `[${fillChars}${emptyChars}]`, time: currentTime - this.#startTime, From 2a4dafbb0733069c798727fdf235d9e39fe6b60d Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 21 May 2025 11:13:07 +0200 Subject: [PATCH 3/4] update --- 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 e84a75c969d1..1e8e97eaebf7 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -192,7 +192,7 @@ export class ProgressBar { /** * The maximum progress that is expected. * - * @param max Max to set. + * @param value Max to set. * * @example Usage * ```ts no-assert From 52ad2cdbe9895534bca3bd939b5e34bead7c289c Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 25 May 2025 10:40:31 +0200 Subject: [PATCH 4/4] update --- cli/unstable_progress_bar.ts | 34 ++++++++++++++++--------------- cli/unstable_progress_bar_test.ts | 27 ++++++++++++------------ 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index 1e8e97eaebf7..27a85c33351a 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -162,7 +162,9 @@ const UNIT_RATE_MAP = new Map([ * await bar.stop(); */ export class ProgressBar { - #value: number; + #hasUpdate = true; + + #value: number = 0; /** * The current progress that has been completed. * @@ -181,14 +183,15 @@ export class ProgressBar { * ``` */ set value(value: number) { + if (this.#value === value) return; this.#value = value; - this.#print(); + this.#hasUpdate = true; } get value(): number { return this.#value; } - #max: number; + #max: number = 0; /** * The maximum progress that is expected. * @@ -207,18 +210,18 @@ export class ProgressBar { * ``` */ set max(value: number) { + if (this.#max === value) return; this.#max = value; this.#unit = getUnit(this.#max); this.#rate = UNIT_RATE_MAP.get(this.#unit)!; - - this.#print(); + this.#hasUpdate = true; } get max(): number { return this.#max; } - #unit: Unit; - #rate: number; + #unit: Unit = "KiB"; + #rate: number = 2 ** 10; #writer: WritableStreamDefaultWriter; #id: number; #startTime: number; @@ -235,9 +238,7 @@ export class ProgressBar { * * @param options The options to configure various settings of the progress bar. */ - constructor( - options: ProgressBarOptions, - ) { + constructor(options: ProgressBarOptions) { const { writable = Deno.stderr.writable, value = 0, @@ -249,8 +250,8 @@ export class ProgressBar { fmt = (x) => `${x.styledTime()} ${x.progressBar} ${x.styledData()} `, keepOpen = true, } = options; - this.#value = value; - this.#max = max; + this.value = value; + this.max = max; this.#barLength = barLength; this.#fillChar = fillChar; this.#emptyChar = emptyChar; @@ -258,9 +259,6 @@ export class ProgressBar { this.#fmt = fmt; this.#keepOpen = keepOpen; - this.#unit = getUnit(options.max); - this.#rate = UNIT_RATE_MAP.get(this.#unit)!; - const stream = new TextEncoderStream(); stream.readable .pipeTo(writable, { preventClose: this.#keepOpen }) @@ -271,13 +269,17 @@ export class ProgressBar { this.#lastTime = this.#startTime; this.#lastValue = this.value; - this.#id = setInterval(() => this.#print(), 1000); + this.#id = setInterval(() => this.#print(), 100); this.#print(); } async #print(): Promise { const currentTime = performance.now(); + const delta = currentTime - this.#lastTime; + if (delta < 1000 && !this.#hasUpdate) return; + this.#hasUpdate = false; + const size = this.value / this.max * this.#barLength | 0; const fillChars = this.#fillChar.repeat(size); const emptyChars = this.#emptyChar.repeat(this.#barLength - size); diff --git a/cli/unstable_progress_bar_test.ts b/cli/unstable_progress_bar_test.ts index c6ee3bd0be38..6568aff47f9a 100644 --- a/cli/unstable_progress_bar_test.ts +++ b/cli/unstable_progress_bar_test.ts @@ -21,7 +21,7 @@ Deno.test("ProgressBar() outputs default result", async () => { const bar = new ProgressBar({ writable, max: 10 * 1000 }); for (let index = 0; index < 10; index++) { bar.value += 1000; - await new Promise((resolve) => setTimeout(resolve, 10)); + await new Promise((resolve) => setTimeout(resolve, 100)); } bar.stop().then(() => writable.close()); @@ -36,15 +36,15 @@ Deno.test("ProgressBar() outputs default result", async () => { "\r\u001b[K[00:00] [###################################---------------] [6.84/9.77 KiB] ", "\r\u001b[K[00:00] [########################################----------] [7.81/9.77 KiB] ", "\r\u001b[K[00:00] [#############################################-----] [8.79/9.77 KiB] ", - "\r\u001b[K[00:00] [##################################################] [9.77/9.77 KiB] ", + "\r\u001b[K[00:01] [##################################################] [9.77/9.77 KiB] ", "\n", ]; - let index = 0; + const actual = []; for await (const buffer of readable) { - const actual = decoder.decode(buffer); - assertEquals(actual, expected[index++]); + actual.push(decoder.decode(buffer)); } + assertEquals(actual, expected); }); Deno.test("ProgressBar() outputs passing time", async () => { @@ -61,19 +61,19 @@ Deno.test("ProgressBar() outputs passing time", async () => { "\n", ]; - let index = 0; + const actual = []; for await (const buffer of readable) { - const actual = decoder.decode(buffer); - assertEquals(actual, expected[index++]); + actual.push(decoder.decode(buffer)); } + assertEquals(actual, expected); }); Deno.test("ProgressBar() change max", async () => { const { readable, writable } = new TransformStream(); const bar = new ProgressBar({ writable, max: 2 ** 10 }); - await new Promise((resolve) => setTimeout(resolve, 100)); bar.max = 2 ** 20; + await new Promise((resolve) => setTimeout(resolve, 100)); bar.stop().then(() => writable.close()); const expected = [ @@ -107,6 +107,7 @@ Deno.test("ProgressBar() can remove itself when finished", async () => { const bar = new ProgressBar({ writable, max: 10 * 1000, clear: true }); for (let index = 0; index < 10; index++) { bar.value += 1000; + await new Promise((resolve) => setTimeout(resolve, 100)); } bar.stop().then(() => writable.close()); @@ -121,15 +122,15 @@ Deno.test("ProgressBar() can remove itself when finished", async () => { "\r\u001b[K[00:00] [###################################---------------] [6.84/9.77 KiB] ", "\r\u001b[K[00:00] [########################################----------] [7.81/9.77 KiB] ", "\r\u001b[K[00:00] [#############################################-----] [8.79/9.77 KiB] ", - "\r\u001b[K[00:00] [##################################################] [9.77/9.77 KiB] ", + "\r\u001b[K[00:01] [##################################################] [9.77/9.77 KiB] ", "\r\x1b[K", ]; - let index = 0; + const actual = []; for await (const buffer of readable) { - const actual = decoder.decode(buffer); - assertEquals(actual, expected[index++]); + actual.push(decoder.decode(buffer)); } + assertEquals(actual, expected); }); Deno.test("ProgressBar() passes correct values to formatter", async () => {