Skip to content

Commit 536e858

Browse files
authored
refactor(cli/unstable): make ProgressBar.#print() better readable (#6503)
1 parent 48fae8f commit 536e858

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

cli/unstable_progress_bar.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,34 +210,23 @@ export class ProgressBar {
210210

211211
async #print(): Promise<void> {
212212
const currentTime = performance.now();
213+
213214
const size = this.#value / this.#max * this.#barLength | 0;
214-
const unit = this.#unit;
215-
const rate = this.#rate;
216-
const x: ProgressBarFormatter = {
215+
const fillChars = this.#fillChar.repeat(size);
216+
const emptyChars = this.#emptyChar.repeat(this.#barLength - size);
217+
218+
const formatter: ProgressBarFormatter = {
217219
styledTime() {
218-
return "[" +
219-
(this.time / 1000 / 60 | 0)
220-
.toString()
221-
.padStart(2, "0") +
222-
":" +
223-
(this.time / 1000 % 60 | 0)
224-
.toString()
225-
.padStart(2, "0") +
226-
"] ";
220+
const minutes = (this.time / 1000 / 60 | 0).toString().padStart(2, "0");
221+
const seconds = (this.time / 1000 % 60 | 0).toString().padStart(2, "0");
222+
return `[${minutes}:${seconds}] `;
227223
},
228-
styledData: function (fractions = 2): string {
229-
return "[" +
230-
(this.value / rate).toFixed(fractions) +
231-
"/" +
232-
(this.max / rate).toFixed(fractions) +
233-
" " +
234-
unit +
235-
"] ";
224+
styledData: (fractions = 2): string => {
225+
const currentValue = (this.#value / this.#rate).toFixed(fractions);
226+
const maxValue = (this.#max / this.#rate).toFixed(fractions);
227+
return `[${currentValue}/${maxValue} ${this.#unit}] `;
236228
},
237-
progressBar: "[" +
238-
this.#fillChar.repeat(size) +
239-
this.#emptyChar.repeat(this.#barLength - size) +
240-
"] ",
229+
progressBar: `[${fillChars}${emptyChars}] `,
241230
time: currentTime - this.#startTime,
242231
previousTime: this.#lastTime - this.#startTime,
243232
value: this.#value,
@@ -246,7 +235,7 @@ export class ProgressBar {
246235
};
247236
this.#lastTime = currentTime;
248237
this.#lastValue = this.#value;
249-
await this.#writer.write("\r\u001b[K" + this.#fmt(x))
238+
await this.#writer.write("\r\u001b[K" + this.#fmt(formatter))
250239
.catch(() => {});
251240
}
252241

0 commit comments

Comments
 (0)