ProgressBar

import { ProgressBar } from "jsr:@ende/cli/unstable-progress-bar";

class ยท experimental

class ProgressBar

ProgressBar is a customisable class that reports updates to a WritableStream on a 1s interval. Progress is communicated by using the ProgressBar.value property.

Example: Basic Usage

import { delay } from "@ende/async";
import { ProgressBar } from "@ende/cli/unstable-progress-bar";

const gen = async function* () {
  for (let i = 0; i < 100; ++i) {
    yield new Uint8Array(1000).fill(97);
    await delay(Math.random() * 200 | 0);
  }
}();
const writer = (await Ende.create("./_tmp/output.txt")).writable.getWriter();

const bar = new ProgressBar({ max: 100_000 });

for await (const buffer of gen) {
  bar.value += buffer.length;
  await writer.write(buffer);
}

await bar.stop();
await writer.close();

Example: Custom Formatting

import { delay } from "@ende/async";
import { ProgressBar } from "@ende/cli/unstable-progress-bar";

const bar = new ProgressBar({
  max: 100,
  formatter(formatter) {
    return `[${formatter.styledTime}] [${formatter.progressBar}] [${formatter.value}/${formatter.max} files]`;
  },
});

for (const x of Array(100)) {
  bar.value += 1;
  await delay(Math.random() * 500);
}

await bar.stop();

Constructors

constructor

new ProgressBar(options: ProgressBarOptions)

Constructs a new instance.

options: ProgressBarOptions

The options to configure various settings of the progress bar.

Properties

value

value: number

The current progress that has been completed.

Example: Usage

import { ProgressBar } from "@ende/cli/unstable-progress-bar";

const progressBar = new ProgressBar({ max : 10 });
progressBar.value += 1;

// do stuff

await progressBar.stop();

max

max: number

The maximum progress that is expected.

Example: Usage

import { ProgressBar } from "@ende/cli/unstable-progress-bar";

const progressBar = new ProgressBar({ max : 1 });
progressBar.max = 100;

// do stuff

await progressBar.stop();

Methods

stop

stop(): Promise<void>

Ends the progress bar and cleans up any lose ends.

Example: Usage

import { ProgressBar } from "@ende/cli/unstable-progress-bar";

const progressBar = new ProgressBar({ max: 100 });
await progressBar.stop()

Source