Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

* Fix issues with `ansi_strwrap()` having `\r` in the string (#667)

* `ansi_collapse(style = "head")` no longer exceeds `width` when not even
the first element fits (#570, @taekop).

# cli 3.6.6

* New `{.num}` and `{.bytes}` inline styles to format numbers
Expand Down
2 changes: 1 addition & 1 deletion R/glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ collapse_head <- function(x, sep, sep2, last, trunc, width, ellipsis) {
} else if (well == width) {
return(ellipsis)
} else if (well + wsep >= width) {
return(paste0(ansi_strtrim(x[1L], width, ellipsis = ""), ellipsis))
return(paste0(ansi_strtrim(x[1L], width - well, ellipsis = ""), ellipsis))
} else {
return(paste0(
ansi_strtrim(x[1L], max(width - well - wsep, 0L), ellipsis = ellipsis),
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-collapsing.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ test_that("ansi_collapse with width trimming", {
})
})

test_that("ansi_collapse with style = 'head' never exceeds width (#570)", {
x <- c("loooooooong", "healthy live the Penguin")
expect_equal(ansi_collapse(x, width = 4, style = "head"), "l...")
expect_equal(ansi_collapse(x, width = 5, style = "head"), "lo...")
expect_equal(
ansi_collapse(x, width = 2, style = "head", ellipsis = "\u2026"),
"l\u2026"
)
for (width in 0:30) {
expect_lte(
ansi_nchar(ansi_collapse(x, width = width, style = "head")),
width
)
}
})

test_that("ansi_collapse produces consistent truncation results", {
expect_equal(
ansi_collapse(1:2, trunc = 1, style = "head"),
Expand Down
Loading