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

Support for crayon styling in progress bar output #76

Closed
dwachsmuth opened this issue Apr 23, 2020 · 9 comments
Closed

Support for crayon styling in progress bar output #76

dwachsmuth opened this issue Apr 23, 2020 · 9 comments
Labels
bug Something isn't working handler_progress() question Further information is requested
Milestone

Comments

@dwachsmuth
Copy link

As far as I can tell, progressr doesn't support using crayon styling to change the colour or weight of terminal output. This came up when I attempted to replicate a previous progress::progress_bar format using handler_progress().

So the following produces silver text:

pb <-
progress::progress_bar$new(
format = crayon::silver(":current of :total [:bar]"),
total = 5
)

for (i in 1:5) {
pb$tick()
Sys.sleep(1)
}

But the following does not produce styled text, even though it otherwise seems to support the format argument of progress::progress_bar. (It also doesn't thrown an error or warning that the crayon formatting is being ignored.)

with_progress({

pb <- progressor(steps = 5)

for (i in 1:5) {
pb()
Sys.sleep(1)
}
},
handlers = handler_progress(
format = crayon::silver(":current of :total [:bar]")
))

Is there another way to apply terminal styling to progress bar output that I am missing? If not, is this a feature you'd consider supporting?

@HenrikBengtsson
Copy link
Collaborator

HenrikBengtsson commented Apr 27, 2020

You're almost there; the following does what you want.

library(progressr)

## The user specifies which progress handler(s) to use
handlers(handler_progress(format = crayon::silver(":spin [:bar] :percent :message")))

## Then these handler(s) are used whenever with_progress() is in place
with_progress({
  pb <- progressor(steps = 5)
  for (i in 1:5) {
    pb()
    Sys.sleep(1)
  }
})

@HenrikBengtsson HenrikBengtsson added question Further information is requested handler_progress() and removed feature request labels Apr 27, 2020
@dwachsmuth
Copy link
Author

dwachsmuth commented Apr 27, 2020 via email

@HenrikBengtsson
Copy link
Collaborator

HenrikBengtsson commented Apr 27, 2020

Aha... I been very puzzled for the last 20 minutes, because "it works for me". However, eventually, I tried with R --vanilla (which comes to rescue in many cases), and then I could reproduce. Turns out I have:

if (crayon:::has_color()) options(crayon.enabled = TRUE)

in my ~/.Rprofile setup. This makes it work. Without, it doesn't. The reason is explained in r-lib/crayon#48 (... and of course, it was me that had this problem in the past but for a completely different reason).

So, for now, as a workaround, use:

library(progressr)
if (crayon:::has_color()) options(crayon.enabled = TRUE)
handlers(handler_progress(format = crayon::red(":current of :total [:bar]")))

with_progress(slow_sum(1:5))

I'll see if I can apply this "workaround" to the internal code of handler_progress().

@HenrikBengtsson HenrikBengtsson added the bug Something isn't working label Apr 27, 2020
@HenrikBengtsson HenrikBengtsson added this to the Next release milestone Apr 27, 2020
@HenrikBengtsson
Copy link
Collaborator

Here's an easier workaround - wrapping the format argument in as.character() will do it, e.g.

library(progressr)
handlers(handler_progress(format = as.character(crayon::red(":current of :total [:bar]"))))
with_progress(slow_sum(1:5))

HenrikBengtsson added a commit that referenced this issue Apr 28, 2020
…xpressions while setting up the 'progress' handler. This is more future proof, in case 'progress' will support other data types later [#76]
@HenrikBengtsson
Copy link
Collaborator

This has been fixed for the next release. Until then, install as:

remotes::install_github("HenrikBengtsson/progressr@develop")

Thanks for inquiring/reporting.

@dwachsmuth
Copy link
Author

dwachsmuth commented Apr 28, 2020 via email

@HenrikBengtsson
Copy link
Collaborator

:) ... only possible with feedback like yours

@HenrikBengtsson
Copy link
Collaborator

progressr 0.6.0 on CRAN just now.

@dwachsmuth
Copy link
Author

I just installed progressr 0.6.0, and can confirm that crayon styling is working "out of the box" without needing to set the crayon.enabled option. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working handler_progress() question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants