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

Open
wants to merge 13 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

All modified and coverable lines are covered by tests ✅

Project coverage is 96.24%. Comparing base (3b75ee7) to head (5fcb98b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6378      +/-   ##
==========================================
+ Coverage   96.23%   96.24%   +0.01%     
==========================================
  Files         556      558       +2     
  Lines       42065    42181     +116     
  Branches     6371     6379       +8     
==========================================
+ Hits        40481    40597     +116     
  Misses       1544     1544              
  Partials       40       40              

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

cli/unstable_progress_bar.ts Outdated Show resolved Hide resolved
@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)}]` })

Instead of having a punch of boolean options about what to and not to include, we now have a default format or a fmt function that can be set to customise the format of the progress bar.
@BlackAsLight
Copy link
Contributor Author

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)}]` })

I added this and a bit more to it, making it optional as I doubt everyone who wants a progress bar wants to go through the effort of styling it.

@BlackAsLight BlackAsLight marked this pull request as ready for review February 5, 2025 11:44
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