Skip to content

BREAKING(cli/unstable): remove ProgressBarFormatter.styledTime and use DurationFormat #6692

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
25 changes: 13 additions & 12 deletions cli/unstable_progress_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
* 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 @@ -117,8 +112,19 @@ function getUnitEntry(max: number): [Unit, number] {

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

function defaultFormatter(x: ProgressBarFormatter) {
return `[${x.styledTime}] [${x.progressBar}] [${x.styledData()}]`;
// HACK (Intl as any) until https://github.com/microsoft/TypeScript/issues/60608 is closed
// deno-lint-ignore no-explicit-any
const timeFormatter = new (Intl as any).DurationFormat(undefined, {
minutes: "2-digit",
seconds: "2-digit",
fractionalDigits: 0,
});

function defaultFormatter(
{ time, progressBar, styledData }: ProgressBarFormatter,
) {
const timeString = timeFormatter.format({ milliseconds: time });
return `[${timeString}] [${progressBar}] [${styledData()}]`;
}

function clamp(value: number, min: number, max: number) {
Expand Down Expand Up @@ -275,11 +281,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: (fractions = 2): string => {
const currentValue = (this.value / this.#rate).toFixed(fractions);
const maxValue = (this.max / this.#rate).toFixed(fractions);
Expand Down
Loading