r-lib / cli

Tools for making beautiful & useful command line interfaces
https://cli.r-lib.org/
Other
655 stars 70 forks source link

`cli_fmt()` does not capture output for `cat_()` functions #703

Open wurli opened 4 months ago

wurli commented 4 months ago

Here's a reprex:

cli::cli_fmt(cli::cli_alert("An alert"))
#> [1] "→ An alert"
cli::cli_fmt(cli::cat_line("A message"))
#> A message
#> character(0)

Created on 2024-07-09 with reprex v2.1.0

wurli commented 4 months ago

Here's the workaround function I've been using for anyone running into the same problem:

capture_all_output <- function(expr) {
  con <- textConnection("out", open = "w", local = TRUE)
  sink(con, append = TRUE, type = "output")
  sink(con, append = TRUE, type = "message")
  force(expr)
  sink(type = "output")
  sink(type = "message")
  close(con)
  get("out")
}
gaborcsardi commented 4 months ago

That's intentional. It only captures the semantic CLI elements: https://cli.r-lib.org/reference/index.html#semantic-cli-elements

wurli commented 4 months ago

Aha, thanks for explaining. Is this because cli_() functions write to stderr instead of stdout? I wonder if it would be helpful to add something to the documentation about the different streams cli uses and why...