r-lib / progress

Progress bar in your R terminal
http://r-lib.github.io/progress/
Other
468 stars 39 forks source link

Crayon usage in format argument breaks progress bar updating #122

Closed josswright closed 3 years ago

josswright commented 3 years ago

(Referencing issues #32 and #109.)

If I create a progress bar with the following (contrived) code it works fine:

pb <- progress_bar$new(
        format = paste0( "  downloading [:bar]", ":percent", "eta: :eta" ),
        total = 100, clear = FALSE, width= 60)

However if I create a bar with:

pb <- progress_bar$new(
        format = paste0( "  downloading [:bar]", crayon::green(":percent"), " eta: :eta" ),
        total = 100, clear = FALSE, width= 60)

... then each update to the bar with pb$tick() is printed after the previous bar output, without overwriting it:

  downloading [===============>----------------] 50% eta: 0s  downloading [=======================>--------] 75% eta: 0s  downloading [================================]100% eta: 0s

Note that this is not related to the existence of a \n anywhere in the format argument. The updates do not occur on separate lines, as in #109. I can (partially) hack around this by adding a \r to the end of the format string, but that seems to cause some flickering on updates.

josswright commented 3 years ago

Looking at the code, it seems that the normal update happens by prepending a \r to the format string, so I'm doing that and it seems mainly to work. I'm not sure it's a perfect solution, though, and I can't work out why adding crayon colours is preventing that in the normal case.

gaborcsardi commented 3 years ago

You might want to try the progress bar facilities in the cli package: https://cli.r-lib.org/dev/reference/index.html#section-progress-bars

josswright commented 3 years ago

Oh, wow. That looks amazing, and I see you're also the author. Has this package been deprecated, then?

Very happy to move over if there's a new long-term package replacing this.

gaborcsardi commented 3 years ago

Yes, progress will be still available, many packages depend on it, but it will be superseded and new development happens in cli.

josswright commented 3 years ago

Thanks. It only took about fifteen minutes to swap my code over yesterday, and the progress bar is much more flexible, and far prettier. :)

Would it be possible to add a note on the main README.md here that progress is superseded by cli? It would have let me find this much earlier. I originally found progress because it's suggested by some tidyverse packages and pages as the way to do progress bars. It seems like it would be useful for people who are directed here to be immediately informed about cli.

Thanks again.

gaborcsardi commented 3 years ago

Yes, I'll add this soon: #123 The progress bar support in cli is quite new. The tidyverse packages will switch to cli as well, slowly.