diff --git a/NEWS.md b/NEWS.md index c8a1f19c7..94d54ffa5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # cli (development version) +* `ansi_collapse(x, trunc = 1, style = "head")` now indeed show one element + if `length(x) == 2`, as documented (@salim-b, #572). + # cli 3.6.0 * The progressr progress handler now reports progress correctly diff --git a/R/glue.R b/R/glue.R index 11cd54aaa..64f90a18c 100644 --- a/R/glue.R +++ b/R/glue.R @@ -112,10 +112,6 @@ ansi_collapse <- function(x, sep = ", ", last = ", and ", trunc = Inf, width = Inf, ellipsis = symbol$ellipsis, style = c("both-ends", "head")) { - # does not make sense to show ... instead of an element - trunc <- max(trunc, 1L) - if (trunc == length(x) - 1L) trunc <- trunc + 1L - style <- match.arg(style) switch( style, @@ -147,6 +143,7 @@ collapse_head_notrim <- function(x, trunc, sep, last, ellipsis) { collapse_head <- function(x, sep = ", ", last = ", and ", trunc = Inf, width = Inf, ellipsis = symbol$ellipsis) { + trunc <- max(trunc, 1L) x <- as.character(x) lnx <- length(x) diff --git a/tests/testthat/_snaps/collapsing.md b/tests/testthat/_snaps/collapsing.md index ca4dbe2ed..f10ea2529 100644 --- a/tests/testthat/_snaps/collapsing.md +++ b/tests/testthat/_snaps/collapsing.md @@ -81,7 +81,7 @@ Code cli_text("{v(2,1)}") Message - 1 and 2 + 1, ... Code cli_text("{v(3,1)}") Message @@ -105,7 +105,7 @@ Code cli_text("{v(3,2)}") Message - 1, 2, and 3 + 1, 2, ... Code cli_text("{v(4,2)}") Message @@ -129,7 +129,7 @@ Code cli_text("{v(4,3)}") Message - 1, 2, 3, and 4 + 1, 2, 3, ... Code cli_text("{v(0,4)}") Message @@ -263,7 +263,7 @@ Code cli_text("{.val {v(11,10)}}") Message - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 11 + 1, 2, 3, 4, 5, 6, 7, 8, ..., 10, and 11 # both-ends with formatting [ansi] @@ -314,7 +314,7 @@ Code cli_text("{.val {v(11,10)}}") Message - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 11 + 1, 2, 3, 4, 5, 6, 7, 8, ..., 10, and 11 # ansi_collapse