Skip to content

Commit

Permalink
ansi_collapse(): fix trunc behavior
Browse files Browse the repository at this point in the history
Now it is as documented.
  • Loading branch information
gaborcsardi committed Sep 13, 2023
1 parent 168d03c commit 1abc22e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 1 addition & 4 deletions R/glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/collapsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
Code
cli_text("{v(2,1)}")
Message
1 and 2
1, ...
Code
cli_text("{v(3,1)}")
Message
Expand All @@ -105,7 +105,7 @@
Code
cli_text("{v(3,2)}")
Message
1, 2, and 3
1, 2, ...
Code
cli_text("{v(4,2)}")
Message
Expand All @@ -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
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -314,7 +314,7 @@
Code
cli_text("{.val {v(11,10)}}")
Message
[34m1[39m, [34m2[39m, [34m3[39m, [34m4[39m, [34m5[39m, [34m6[39m, [34m7[39m, [34m8[39m, [34m9[39m, [34m10[39m, and [34m11[39m
[34m1[39m, [34m2[39m, [34m3[39m, [34m4[39m, [34m5[39m, [34m6[39m, [34m7[39m, [34m8[39m, ..., [34m10[39m, and [34m11[39m

# ansi_collapse

Expand Down

0 comments on commit 1abc22e

Please sign in to comment.