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

feat(cli): Intro createProgressBar() & new ProgressBarStream() #6378

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

BlackAsLight
Copy link
Contributor

Closes: #6374

This pull request introduces a function called createProgressBar and a class called ProgressBarStream. They write and update a progress to the writable stream passed to them. The methods have quite a bit of configurability.

Tests are yet to be created. Not sure exactly how to create tests to validate behaviour.

Example

import { ProgressBarStream } from "./unstable_progress_bar_stream.ts";

const readable = ReadableStream
  .from<Uint8Array>(async function* (): AsyncGenerator<Uint8Array> {
    for (let i = 0; i < 100; ++i) {
      yield new Uint8Array(1000).fill(97);
      await new Promise((a) => setTimeout(a, (Math.random() * 200) | 0));
    }
  }())
  .pipeThrough(
    new ProgressBarStream(Deno.stdout.writable, {
      totalSize: 1000 * 100,
    }),
  );

// deno-lint-ignore no-empty
for await (const _ of readable) {}

stdout:

[00:04] [###################-------------------------------] [38.09/97.66 KiB]

@github-actions github-actions bot added the cli label Feb 3, 2025
Copy link

codecov bot commented Feb 3, 2025

Codecov Report

Attention: Patch coverage is 69.23077% with 32 lines in your changes missing coverage. Please review.

Project coverage is 96.18%. Comparing base (1f032bb) to head (af463c2).

Files with missing lines Patch % Lines
cli/unstable_progress_bar_stream.ts 21.73% 18 Missing ⚠️
cli/unstable_progress_bar.ts 82.71% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6378      +/-   ##
==========================================
- Coverage   96.24%   96.18%   -0.07%     
==========================================
  Files         555      557       +2     
  Lines       42047    42151     +104     
  Branches     6369     6375       +6     
==========================================
+ Hits        40470    40542      +72     
- Misses       1537     1569      +32     
  Partials       40       40              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* The character to fill the progress bar up with as it makes progress.
* @default {'#'}
*/
fullChar?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fillChar?

@kt3k
Copy link
Member

kt3k commented Feb 4, 2025

I agree that showing the progress of byte number count is a common task. But can we also support the progress of abstract counter, like number of packages, number of files, etc.

@BlackAsLight
Copy link
Contributor Author

I agree that showing the progress of byte number count is a common task. But can we also support the progress of abstract counter, like number of packages, number of files, etc.

Should the amount just offer to display no unit for that instance or ask for custom unit?

@kt3k
Copy link
Member

kt3k commented Feb 4, 2025

Should the amount just offer to display no unit for that instance or ask for custom unit?

I think It'll probably be confusing to just omit the unit. If 10.0/50.0 is displayed, the user don't know if it's 50.0K or 50.0M.

How about having bytes: boolean option, for example? When it's true, it does the current behavior. If it's false, then it shows numbers like 30, 20K, 10M, etc without B suffix.

@BlackAsLight
Copy link
Contributor Author

That could work. I was more thinking of offering a custom unit option and if set then the rate calculation would be 1 so they could see something like [4/500,000 files]

@timreichen
Copy link
Contributor

Maybe we should add a formatter option instead of many different styling options.
This is more flexible and also avoids the problem of localization.

Something according to:

import { format as formatDuration } from "@std/fmt/duration.ts"
import { format as formatBytes } from "@std/fmt/bytes.ts"
new ProgressBarStream(Deno.stdout.writable, { formatter: ({ time, progressBar, value, max }) => `[${formatDuration(time)}] ${progressBar} [${formatBytes(value)}/${formatBytes(max)}]` })

* The offset size of the input if progress is resuming part way through.
* @default {0}
*/
currentSize?: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's call it value like in progress HTMLElement?

/**
* The total size expected to receive.
*/
totalSize: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's call it max like in progress HTMLElement?

* Whether the progress bar should be removed after completion.
* @default {false}
*/
clearBar?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's call it just clear as in

clear?: boolean;
?

* completion.
* @default {true}
*/
preventClose?: boolean;
Copy link
Contributor

@timreichen timreichen Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think options should be formulated positively rather than negatively, something like keepAlive, keepStreamOpen, closeStream or closeStreamWhenCompleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: @std/cli should have a progress bar function
3 participants