Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli/unstable): Declare ProgressBar options as separate properties #6433

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 46 additions & 30 deletions cli/unstable_progress_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,23 @@ export interface ProgressBarOptions {
* bar.end();
*/
export class ProgressBar {
#options: Required<ProgressBarOptions>;
#value: number;
#max: number;
#barLength: number;
#fillChar: string;
#emptyChar: string;
#clear: boolean;
#fmt: (fmt: ProgressBarFormatter) => string;
#keepOpen: boolean;

#unit: string;
#rate: number;
#writer: WritableStreamDefaultWriter;
#id: number;
#startTime: number;
#lastTime: number;
#lastValue: number;

/**
* Constructs a new instance.
*
Expand All @@ -153,29 +162,36 @@ export class ProgressBar {
writable: WritableStream<Uint8Array>,
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,
max,
barLength = 50,
fillChar = "#",
emptyChar = "-",
clear = false,
fmt = (x) => x.styledTime() + x.progressBar + x.styledData(),
keepOpen = true,
} = options;

this.#value = value;
this.#max = max;
this.#barLength = barLength;
this.#fillChar = fillChar;
this.#emptyChar = emptyChar;
this.#clear = clear;
this.#fmt = fmt;
this.#keepOpen = keepOpen;

if (options.max < 2 ** 20) {
if (max < 2 ** 20) {
this.#unit = "KiB";
this.#rate = 2 ** 10;
} else if (options.max < 2 ** 30) {
} else if (max < 2 ** 30) {
this.#unit = "MiB";
this.#rate = 2 ** 20;
} else if (options.max < 2 ** 40) {
} else if (max < 2 ** 40) {
this.#unit = "GiB";
this.#rate = 2 ** 30;
} else if (options.max < 2 ** 50) {
} else if (max < 2 ** 50) {
this.#unit = "TiB";
this.#rate = 2 ** 40;
} else {
Expand All @@ -185,20 +201,20 @@ 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<void> {
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 = {
Expand All @@ -223,18 +239,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(() => {});
}

Expand All @@ -244,7 +260,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;
}

/**
Expand All @@ -253,7 +269,7 @@ export class ProgressBar {
async end(): Promise<void> {
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(() => {});
}
Expand Down
Loading