Skip to content
Open
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* New `{.num}` and `{.bytes}` inline styles to format numbers
and bytes (@m-muecke, #644, #588, #643).
* Fix issues with `ansi_strwrap()` having `\r` in the string (#667)

# cli 3.6.5

Expand Down
11 changes: 6 additions & 5 deletions R/ansiex.R
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ ansi_strwrap <- function(

# First we need to remove the multiple spaces, to make it easier to
# map the strings later on. We do this per paragraph, to keep paragraphs.
pars <- strsplit(x, "\n[ \t\n]*\n", perl = TRUE)
pars <- strsplit(x, "\r?\n[ \t\n\r]*\r?\n", perl = TRUE)
pars <- lapply(pars, ansi_trimws)

# Within paragraphs, replace multiple spaces with one, except when there
Expand All @@ -598,11 +598,11 @@ ansi_strwrap <- function(
# some is outside, but for now, we'll live with this limitation.
pars <- lapply(pars, function(s) {
# First replace multiple spaces that are not at the end of a sentence
s <- gsub("(?<![.!?])[ \t\n][ \t\n]*", " ", s, perl = TRUE)
s <- gsub("(?<![.!?])[ \t\r\n][ \t\r\n]*", " ", s, perl = TRUE)
# Handle multiple spaces at the end of a sentence
s <- gsub("(?<=[.!?])[ \t\n][ \t\n][ \t\n]*", " ", s, perl = TRUE)
s <- gsub("(?<=[.!?])[ \t\r\n][ \t\n][ \t\r\n]*", " ", s, perl = TRUE)
# Handle simple space at the end of a sentence
gsub("(?<=[.!?])[ \t\n]", " ", s, perl = TRUE)
gsub("(?<=[.!?])[ \t\r\n]", " ", s, perl = TRUE)
})

# Put them back together
Expand Down Expand Up @@ -632,7 +632,8 @@ ansi_strwrap <- function(
} else if (xsc == xwc) {
xsidx <- xsidx + 1L
xwidx[2] <- xwidx[2] + 1L
} else if (xsc %in% c(" ", "\n", "\t")) {
} else if (xsc %in% c(" ", "\n", "\t", "\r")) {
# Ignore whitespace characters
drop <- c(drop, xsidx)
xsidx <- xsidx + 1L
} else if (xwc == " ") {
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-ansiex.R
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,11 @@ test_that("ansi_strwrap newlines", {
ansi_strwrap("\033[32mv\033[39m hello world.\nxxx"),
ansi_string("\033[32mv\033[39m hello world. xxx")
)
# Reprex for issue #667 about using \r
expect_equal(
ansi_strwrap("\033[36m•\033[39m x \r y"),
ansi_string("\033[36m•\033[39m x y")
)
})

test_that("ansi_strwrap and \f edge cases", {
Expand Down