diff --git a/cli/unstable_progress_bar.ts b/cli/unstable_progress_bar.ts index ed170ee5ca41..4ad79d44e197 100644 --- a/cli/unstable_progress_bar.ts +++ b/cli/unstable_progress_bar.ts @@ -136,7 +136,6 @@ export interface ProgressBarOptions { * bar.end(); */ export class ProgressBar { - #options: Required; #unit: string; #rate: number; #writer: WritableStreamDefaultWriter; @@ -144,6 +143,15 @@ export class ProgressBar { #startTime: number; #lastTime: number; #lastValue: number; + + #value: number; + #max: number; + #barLength: number; + #fillChar: string; + #emptyChar: string; + #clear: boolean; + #fmt: (fmt: ProgressBarFormatter) => string; + #keepOpen: boolean; /** * Constructs a new instance. * @@ -154,18 +162,23 @@ export class ProgressBar { writable: WritableStream, options: ProgressBarOptions, ) { - this.#options = { - value: options.value ?? 0, - 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, - }; + const { + value = 0, + barLength = 50, + fillChar = "#", + emptyChar = "-", + clear = false, + fmt = (x) => x.styledTime() + x.progressBar + x.styledData(), + keepOpen = true, + } = options; + this.#value = value; + this.#max = options.max; + this.#barLength = barLength; + this.#fillChar = fillChar; + this.#emptyChar = emptyChar; + this.#clear = clear; + this.#fmt = fmt; + this.#keepOpen = keepOpen; if (options.max < 2 ** 20) { this.#unit = "KiB"; @@ -186,20 +199,18 @@ 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); 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 / - this.#options.max * - this.#options.barLength | 0; + const size = this.#value / this.#max * this.#barLength | 0; const unit = this.#unit; const rate = this.#rate; const x: ProgressBarFormatter = { @@ -224,18 +235,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.#options.value, + value: this.#value, previousValue: this.#lastValue, - max: this.#options.max, + max: this.#max, }; this.#lastTime = currentTime; - this.#lastValue = this.#options.value; - await this.#writer.write("\r\u001b[K" + this.#options.fmt(x)) + this.#lastValue = this.#value; + await this.#writer.write("\r\u001b[K" + this.#fmt(x)) .catch(() => {}); } @@ -245,7 +256,7 @@ export class ProgressBar { * @param x The amount of progress that has been made. */ add(x: number): void { - this.#options.value += x; + this.#value += x; } /** @@ -254,7 +265,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(() => {}); }