Skip to content
Open
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
27 changes: 16 additions & 11 deletions cli/unstable_progress_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import { formatUnitFraction } from "./_unit.ts";
* The properties provided to the fmt function upon every visual update.
*/
export interface ProgressBarFormatter {
/**
* A formatted version of the duration.
* `mm:ss`
*/
styledTime: string;
/**
* A function that returns a formatted version of the data received.
* `0.40/97.66 KiB`
Expand Down Expand Up @@ -103,8 +98,23 @@ export interface ProgressBarOptions {

const LINE_CLEAR = "\r\u001b[K";

const timeFormatter = new Intl.DurationFormat(undefined, {
minutes: "2-digit",
seconds: "2-digit",
});

const DURATION_ROUNDING_OPTIONS: Temporal.DurationRoundingOptions = {
largestUnit: "minutes",
smallestUnit: "seconds",
roundingMode: "trunc",
};

function defaultFormatter(formatter: ProgressBarFormatter) {
return `[${formatter.styledTime}] [${formatter.progressBar}] [${formatter.styledData()}]`;
const duration = Temporal.Duration
.from({ milliseconds: formatter.time })
.round(DURATION_ROUNDING_OPTIONS);
const styledTime = timeFormatter.format(duration);
return `[${styledTime}] [${formatter.progressBar}] [${formatter.styledData()}]`;
}

function clamp(value: number, min: number, max: number) {
Expand Down Expand Up @@ -255,11 +265,6 @@ export class ProgressBar {
const emptyChars = this.#emptyChar.repeat(this.#barLength - size);

return {
get styledTime() {
const minutes = (this.time / 1000 / 60 | 0).toString().padStart(2, "0");
const seconds = (this.time / 1000 % 60 | 0).toString().padStart(2, "0");
return `${minutes}:${seconds}`;
},
styledData() {
return formatUnitFraction(this.value, this.max);
},
Expand Down
Loading