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

Use last for length 2 vectors with ansi_collapse() when sep2 not used #718

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions R/glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,15 @@ collapse_head_notrim <- function(x, trunc, sep, sep2, last, ellipsis) {

lnx <- length(x)

if (lnx == 1L) return(x)
if (lnx == 2L) return(paste0(x, collapse = sep2))
if (lnx == 1L)
return(x)
if (lnx == 2L) {
# Handle the case where last is changed and sep2 is not
if (sep2 == " and " & last != ", and ")
gaborcsardi marked this conversation as resolved.
Show resolved Hide resolved
return(paste0(x, collapse = last))
else
return(paste0(x, collapse = sep2))
}
if (lnx <= trunc) {
# no truncation
return(paste0(
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/_snaps/collapsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,22 @@
Output
[1] "a, b, c, d, e, f, g, h, i, and j"

# Issue #681

Code
v <- cli::cli_vec(c("foo", "bar", "foobar"), style = list(`vec-last` = ", or "))
cli::cli_text("Must be one of: {v}.")
Message
Must be one of: foo, bar, or foobar.
Code
v <- cli::cli_vec(c("foo", "bar"), style = list(`vec-last` = " or "))
cli::cli_text("Must be one of: {v}.")
Message
Must be one of: foo or bar.
Code
v <- cli::cli_vec(c("foo", "bar"), style = list(`vec-last` = " or ",
`vec-sep2` = " xor "))
cli::cli_text("Must be one of: {v}.")
Message
Must be one of: foo xor bar.

27 changes: 27 additions & 0 deletions tests/testthat/test-collapsing.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,30 @@ test_that("ansi_collapse uses `sep2` for length-two inputs", {
expect_equal(ansi_collapse(1:2, trunc = 2, style = "head"),
"1 and 2")
})


test_that("Issue #681", {
expect_equal(ansi_collapse(1:2, last = " or "), "1 or 2")
expect_equal(ansi_collapse(1:2, sep2 = " and ", last = " or "), "1 or 2")
expect_equal(ansi_collapse(1:2, sep2 = " xor ", last = " or "), "1 xor 2")

expect_snapshot({
v <- cli::cli_vec(
c("foo", "bar", "foobar"),
style = list("vec-last" = ", or ")
)
cli::cli_text("Must be one of: {v}.")

v <- cli::cli_vec(
c("foo", "bar"),
style = list("vec-last" = " or ")
)
cli::cli_text("Must be one of: {v}.")

v <- cli::cli_vec(
c("foo", "bar"),
style = list("vec-last" = " or ", "vec-sep2" = " xor ")
)
cli::cli_text("Must be one of: {v}.")
})
})
Loading