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

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

Open
wurli opened this issue Jul 9, 2024 · 3 comments
Open

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

wurli opened this issue Jul 9, 2024 · 3 comments

Comments

@wurli
Copy link

wurli commented Jul 9, 2024

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
Copy link
Author

wurli commented Jul 9, 2024

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
Copy link
Member

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

@wurli
Copy link
Author

wurli commented Jul 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants