deno-library / progress

ProgressBar in terminal for deno

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Renders new line on every render if line width > terminal width

jsejcksn opened this issue · comments

Hello. I have noticed a bug, which is reproducible by running the "simple" example in the readme using the default terminal profile on macOS.

Here are the steps to reproduce:

Versions:
type name version
os macOS 11.3.1 (20E241)
terminal emulator Terminal.app 2.11 (440)
runtime Deno 1.9.2
module progress 1.2.3
  1. Create a new unmodified profile in macOS Terminal:

    0
  2. Open a new Terminal window using that profile:

    1 2
  3. Create an example TypeScript module with the code from the "simple" example in the readme:

    3
  4. Run the example file:

    4.mp4

As you can see in the video, it re-renders to the same line until the "completed" value reaches 10, at which point the render string width exceeds the terminal width and each render begins to print to a new line.

I think this is not a good experience (especially for users who are just trying the module).

If each progress bar can render more than one line, then I think this is simply a bug. However, I haven't researched the source of this module yet, but it seems like it only renders a single line for each progress bar. With that assumption in mind, here is an idea for how to address it:

  • Add a new constructor option maxWidth to manually set the max length of each line rendered. By default, this value would be Infinity if left undefined.
  • When a render occurs, the line is truncated to the length of the maxWidth property value, forcefully cutting off any remaining characters.
  • Optionally: If the length of all non-bar characters is computed before each render, then the length of the bar could become dynamic, so that the non-bar data is not truncated. (This could allow for new options minBarWidth and maxBarWidth.)

Having shared that, I also want to share some feedback about when I first used this module (sometime last year maybe?):

I was confused by the naming of the constructor option width. I expected that it would control what I described above as maxWidth. I think it would be more clear if the width property were renamed barWidth in the next major semver release.

commented

Thank you so much for your feedback.

Since I have turned to golang development for almost 2 years, now I rarely do js and typescript related development, so I am very happy to accept pull requests.

What is the problem ?

Can't get the tty width now. This is the cause of the bug.

  private get ttyColumns(): number {
    return 100;
  }

progress/mod.ts

Line 197 in e5908c3

return 100;

Is there a way to get the tty width

Yes! Deno v1.2.0 started to support tty column, but is unstable until now.

in "mod.unstable.ts"

  private get ttyColumns(): number {
    return Deno.consoleSize(Deno.stdout.rid).columns;
  }

return Deno.consoleSize(Deno.stdout.rid).columns;

You can try it by following code

deno run --unstable ./examples/width.unstable.ts

Other

At the same time, I welcome pull requests.
I hope to be compatible with the old code. Destructive updates will cause errors or unexpected results after users update.

You can add maxWidth or barWidth, It is great. It would be more clear.
You can rename width to barWidth in the next major semver release. But need clearer documentation.

I don't have enough time and energy to maintain this library.I'm very sorry.

commented

I found the problem.

if (str.length < this.lastStr.length) {
  str += " ".repeat(this.lastStr.length - str.length);
}

There is a bug in the length calculation here, if it is colored.

complete = bgGreen(" "),
incomplete = bgWhite(" "),

The space length is one, but the length is 11 if it is colored.
10 more lengths are calculated.

commented

Landed in v1.3.0

commented

Sorry for taking so long to fix this bug