Skip to content

Commit 175c746

Browse files
authored
Merge pull request #719 from rundel/issue#590
Avoid duplication of length 1 vecs when width set using ansi_collapse() - #590
2 parents b14d61b + f6399e2 commit 175c746

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
(without the serial comma) for two-element vectors if `sep2` is not
55
given (@rundel, #681).
66

7+
* `ansi_collapse()` is now correct for length-1 vectors with style "head"
8+
if width is specified (@rundel, #590).
9+
710
# cli 3.6.3
811

912
* cli now builds on ARM Windows.

R/glue.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ collapse_head <- function(x, sep, sep2, last, trunc, width, ellipsis) {
188188
nlast <- if (lnx > 2L) 1L else 0L
189189
wtot <- sum(wx) + nsep * wsep + nsep2 * wsep2 + nlast * wlast
190190
if (wtot <= width) {
191-
if (lnx == 2L) {
191+
if (lnx == 1L) {
192+
return(x)
193+
} else if (lnx == 2L) {
192194
return(paste0(x, collapse = sep2))
193195
} else {
194196
return(paste0(

tests/testthat/test-collapsing.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ test_that("ansi_collapse uses `sep2` for length-two inputs", {
198198
"1 and 2")
199199
})
200200

201+
test_that("Avoid duplication of length 1 vecs when width set (#590)", {
202+
expect_equal(ansi_collapse(1), "1")
203+
expect_equal(ansi_collapse(1, style = "head"), "1")
204+
expect_equal(ansi_collapse(1, style = "head", width = 70), "1")
205+
expect_equal(ansi_collapse(1, style = "head", last = " and again "), "1")
206+
expect_equal(ansi_collapse(1, style = "head", width = 70, last = " and again "), "1")
207+
})
201208

202209
test_that("Issue #681", {
203210
# sep2 takes precedence

0 commit comments

Comments
 (0)