Skip to content

Commit

Permalink
Merge pull request #719 from rundel/issue#590
Browse files Browse the repository at this point in the history
Avoid duplication of length 1 vecs when width set using ansi_collapse() - #590
  • Loading branch information
gaborcsardi authored Aug 28, 2024
2 parents b14d61b + f6399e2 commit 175c746
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
(without the serial comma) for two-element vectors if `sep2` is not
given (@rundel, #681).

* `ansi_collapse()` is now correct for length-1 vectors with style "head"
if width is specified (@rundel, #590).

# cli 3.6.3

* cli now builds on ARM Windows.
Expand Down
4 changes: 3 additions & 1 deletion R/glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ collapse_head <- function(x, sep, sep2, last, trunc, width, ellipsis) {
nlast <- if (lnx > 2L) 1L else 0L
wtot <- sum(wx) + nsep * wsep + nsep2 * wsep2 + nlast * wlast
if (wtot <= width) {
if (lnx == 2L) {
if (lnx == 1L) {
return(x)
} else if (lnx == 2L) {
return(paste0(x, collapse = sep2))
} else {
return(paste0(
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-collapsing.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ test_that("ansi_collapse uses `sep2` for length-two inputs", {
"1 and 2")
})

test_that("Avoid duplication of length 1 vecs when width set (#590)", {
expect_equal(ansi_collapse(1), "1")
expect_equal(ansi_collapse(1, style = "head"), "1")
expect_equal(ansi_collapse(1, style = "head", width = 70), "1")
expect_equal(ansi_collapse(1, style = "head", last = " and again "), "1")
expect_equal(ansi_collapse(1, style = "head", width = 70, last = " and again "), "1")
})

test_that("Issue #681", {
# sep2 takes precedence
Expand Down

0 comments on commit 175c746

Please sign in to comment.