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

BREAKING(cli/unstable): make ProgressBar writable optional #6409

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
18 changes: 10 additions & 8 deletions cli/unstable_progress_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export interface ProgressBarFormatter {
* {@link ProgressBarStream}.
*/
export interface ProgressBarOptions {
/**
* The {@link WritableStream} that will receive the progress bar reports.
* @default {Deno.stderr.writable}
*/
writable?: WritableStream<Uint8Array>;
/**
* The offset size of the input if progress is resuming part way through.
* @default {0}
Expand Down Expand Up @@ -104,7 +109,7 @@ export interface ProgressBarOptions {
* }();
* const writer = (await Deno.create("./_tmp/output.txt")).writable.getWriter();
*
* const bar = new ProgressBar(Deno.stdout.writable, { max: 100_000 });
* const bar = new ProgressBar({ max: 100_000 });
*
* for await (const buffer of gen) {
* bar.add(buffer.length);
Expand All @@ -120,7 +125,7 @@ export interface ProgressBarOptions {
* import { delay } from "@std/async";
* import { ProgressBar } from "@std/cli/unstable-progress-bar";
*
* const bar = new ProgressBar(Deno.stdout.writable, {
* const bar = new ProgressBar({
* max: 100,
* fmt(x) {
* return `${x.styledTime()}${x.progressBar}[${x.value}/${x.max} files]`;
Expand All @@ -146,14 +151,11 @@ export class ProgressBar {
/**
* Constructs a new instance.
*
* @param writable The {@link WritableStream} that will receive the progress bar reports.
* @param options The options to configure various settings of the progress bar.
*/
constructor(
writable: WritableStream<Uint8Array>,
options: ProgressBarOptions,
) {
constructor(options: ProgressBarOptions) {
this.#options = {
writable: options.writable ??= Deno.stderr.writable,
value: options.value ?? 0,
max: options.max,
barLength: options.barLength ?? 50,
Expand Down Expand Up @@ -185,7 +187,7 @@ export class ProgressBar {

const stream = new TextEncoderStream();
stream.readable
.pipeTo(writable, { preventClose: this.#options.keepOpen })
.pipeTo(options.writable, { preventClose: this.#options.keepOpen })
.catch(() => clearInterval(this.#id));
this.#writer = stream.writable.getWriter();
this.#id = setInterval(() => this.#print(), 200);
Expand Down
7 changes: 2 additions & 5 deletions cli/unstable_progress_bar_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
* let readable = response.body
* if (max) {
* readable = readable
* ?.pipeThrough(new ProgressBarStream(Deno.stdout.writable, { max })) ?? null;
* ?.pipeThrough(new ProgressBarStream({ max })) ?? null;
* }
* await readable?.pipeTo((await Deno.create("./_tmp/example.com.html")).writable);
* ```
Expand All @@ -29,18 +29,15 @@ export class ProgressBarStream extends TransformStream<Uint8Array, Uint8Array> {
/**
* Constructs a new instance.
*
* @param writable The {@link WritableStream} that will receive the progress bar
* reports.
* @param options The options to configure various settings of the progress bar.
*/
constructor(
writable: WritableStream<Uint8Array>,
options: ProgressBarOptions,
) {
let bar: ProgressBar | undefined;
super({
start(_controller) {
bar = new ProgressBar(writable, options);
bar = new ProgressBar(options);
},
transform(chunk, controller) {
bar?.add(chunk.length);
Expand Down
4 changes: 2 additions & 2 deletions cli/unstable_progress_bar_stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Deno.test("ProgressBarStream() flushes", async () => {
const _ of ReadableStream
.from(getData(10, 1000))
.pipeThrough(
new ProgressBarStream(writable, { max: 10 * 1000, keepOpen: false }),
new ProgressBarStream({ writable, max: 10 * 1000, keepOpen: false }),
)
// deno-lint-ignore no-empty
) {}
Expand All @@ -34,7 +34,7 @@ Deno.test("ProgressBarStream() cancels", async () => {
await ReadableStream
.from(getData(10, 1000))
.pipeThrough(
new ProgressBarStream(writable, { max: 10 * 1000, keepOpen: false }),
new ProgressBarStream({ writable, max: 10 * 1000, keepOpen: false }),
)
.cancel();

Expand Down
15 changes: 7 additions & 8 deletions cli/unstable_progress_bar_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function* getData(

Deno.test("ProgressBar() outputs default result", async () => {
const { readable, writable } = new TransformStream();
const bar = new ProgressBar(writable, { max: 10 * 1000 });
const bar = new ProgressBar({ writable, max: 10 * 1000 });

for await (const a of getData(10, 1000)) bar.add(a.length);
bar.end().then(() => writable.close());
Expand Down Expand Up @@ -64,7 +64,7 @@ Deno.test("ProgressBar() outputs default result", async () => {

Deno.test("ProgressBar() can handle a readable.cancel() correctly", async () => {
const { readable, writable } = new TransformStream();
const bar = new ProgressBar(writable, { max: 10 * 1000 });
const bar = new ProgressBar({ writable, max: 10 * 1000 });

for await (const a of getData(10, 1000)) bar.add(a.length);
bar.end();
Expand All @@ -74,10 +74,7 @@ Deno.test("ProgressBar() can handle a readable.cancel() correctly", async () =>

Deno.test("ProgressBar() can remove itself when finished", async () => {
const { readable, writable } = new TransformStream();
const bar = new ProgressBar(writable, {
max: 10 * 1000,
clear: true,
});
const bar = new ProgressBar({ writable, max: 10 * 1000, clear: true });

for await (const a of getData(10, 1000)) bar.add(a.length);
bar.end()
Expand All @@ -92,7 +89,8 @@ Deno.test("ProgressBar() passes correct values to formatter", async () => {
const { readable, writable } = new TransformStream();
let lastTime: undefined | number = undefined;
let lastValue: undefined | number = undefined;
const bar = new ProgressBar(writable, {
const bar = new ProgressBar({
writable,
max: 10 * 1000,
keepOpen: false,
fmt(x) {
Expand All @@ -115,7 +113,8 @@ Deno.test("ProgressBar() uses correct unit type", async () => {
let i = 0;
for (const unit of units) {
const { readable, writable } = new TransformStream();
const bar = new ProgressBar(writable, {
const bar = new ProgressBar({
writable,
max: 2 ** (10 * ++i),
keepOpen: false,
});
Expand Down
Loading