r-lib/cli

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

Opened this issue · 3 comments

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

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")
}

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

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...