Skip to content

Commit

Permalink
Merge pull request #1387 from tidyverse/docs-cli-format
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Sep 4, 2022
2 parents 8d7b477 + f2bc7b3 commit 199ab3a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion R/error.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ tibble_error <- function(x, ..., parent = NULL) {
call <- sys.call(-1)
fn_name <- as_name(call[[1]])
class <- tibble_error_class(gsub("^error_", "", fn_name))
error_cnd(class, ..., message = x, parent = parent)
error_cnd(class, ..., message = x, parent = parent, use_cli_format = TRUE)
}
12 changes: 6 additions & 6 deletions R/utils-msg-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,19 @@ pluralise_n <- function(message, n) {
}

bullets <- function(header, ..., info = NULL) {
# FIXME: Convert info to i with rlang >= 0.4.12, use set_default_name(bullets, "*")
# FIXME: Avoid ensure_full_stop()
bullets <- vec_c(..., .name_spec = "{outer}")
bullets <- set_default_name(bullets, "*")

paste0(
ensure_full_stop(header), "\n",
format_error_bullets(ensure_full_stop(bullets)),
if (!is.null(info)) paste0("\n", format_error_bullets(c(i = info)))
vec_c(
ensure_full_stop(vec_c(header, bullets, .name_spec = "{outer}")),
i = info,
.name_spec = "{outer}"
)
}

problems <- function(header, ..., .problem = " problem(s)") {
problems <- vec_c(..., .name_spec = "{outer}")
problems <- set_default_name(problems, "x")
MAX_BULLETS <- 6L
if (length(problems) >= MAX_BULLETS) {
n_more <- length(problems) - MAX_BULLETS + 1L
Expand All @@ -70,6 +69,7 @@ problems <- function(header, ..., .problem = " problem(s)") {
length(problems) <- MAX_BULLETS
}

problems <- set_default_name(problems, "x")
bullets(header, problems)
}

Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/_snaps/utils-msg-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

Code
# # Problems
writeLines(problems("header", c("item 1", "item 2")))
writeLines(format_error_bullets(problems("header", c("item 1", "item 2"))))
Output
header.
x item 1.
x item 2.
Code
writeLines(problems("header", LETTERS))
writeLines(format_error_bullets(problems("header", LETTERS)))
Output
header.
x A.
Expand All @@ -18,7 +18,7 @@
x E.
x ... and 21 more problems.
Code
writeLines(problems("header", 1:6))
writeLines(format_error_bullets(problems("header", as.character(1:6))))
Output
header.
x 1.
Expand All @@ -29,13 +29,13 @@
x ... and 1 more problem.
Code
# # Bullets
writeLines(bullets("header", c("item 1", "item 2")))
writeLines(format_error_bullets(bullets("header", c("item 1", "item 2"))))
Output
header.
* item 1.
* item 2.
Code
writeLines(bullets("header", LETTERS))
writeLines(format_error_bullets(bullets("header", LETTERS)))
Output
header.
* A.
Expand Down Expand Up @@ -65,7 +65,7 @@
* Y.
* Z.
Code
writeLines(bullets("header", 1:6))
writeLines(format_error_bullets(bullets("header", as.character(1:6))))
Output
header.
* 1.
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-error.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ test_that("tibble_error()", {
class = c("tibble_error_foo", "tibble_error"),
message = "message",
foo = 42,
bar = 7
bar = 7,
use_cli_format = TRUE
)
)
})
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-utils-msg-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ test_that("pluralise_msg works correctly", {
test_that("output test", {
expect_snapshot({
"# Problems"
writeLines(problems("header", c("item 1", "item 2")))
writeLines(problems("header", LETTERS))
writeLines(problems("header", 1:6))
writeLines(format_error_bullets(problems("header", c("item 1", "item 2"))))
writeLines(format_error_bullets(problems("header", LETTERS)))
writeLines(format_error_bullets(problems("header", as.character(1:6))))

"# Bullets"
writeLines(bullets("header", c("item 1", "item 2")))
writeLines(bullets("header", LETTERS))
writeLines(bullets("header", 1:6))
writeLines(format_error_bullets(bullets("header", c("item 1", "item 2"))))
writeLines(format_error_bullets(bullets("header", LETTERS)))
writeLines(format_error_bullets(bullets("header", as.character(1:6))))

"# Commas"
commas("1")
Expand Down

0 comments on commit 199ab3a

Please sign in to comment.