diff --git a/R/ansiex.R b/R/ansiex.R index 6022be157..fa082df44 100644 --- a/R/ansiex.R +++ b/R/ansiex.R @@ -502,7 +502,7 @@ ansi_strwrap <- function(x, width = console_width(), indent = 0, smark <- paste0("\n\n", mark, "\n\n") x <- gsub("\f", smark, x, fixed = TRUE, useBytes = TRUE) fix_ff <- function(x) { - rem <- which(x == mark) + rem <- which(ansi_strip(x) == mark) if (length(rem)) { x[-c(rem - 1, rem, rem + 1)] } else { @@ -549,7 +549,10 @@ ansi_strwrap <- function(x, width = console_width(), indent = 0, while (xsidx <= xslen) { xsc <- substr(xs, xsidx, xsidx) xwc <- substr(xw[xwidx[1]], xwidx[2], xwidx[2]) - if (xsc == xwc) { + if (is.na(xwc)) { + # colored trailing white space in input? + xsidx <- xsidx + 1L + } else if (xsc == xwc) { xsidx <- xsidx + 1L xwidx[2] <- xwidx[2] + 1L } else if (xsc %in% c(" ", "\n", "\t")) { diff --git a/tests/testthat/test-ansiex.R b/tests/testthat/test-ansiex.R index b732e419b..6d986d4de 100644 --- a/tests/testthat/test-ansiex.R +++ b/tests/testthat/test-ansiex.R @@ -519,6 +519,17 @@ test_that("ansi_strwrap newlines", { ) }) +test_that("ansi_strwrap and \f edge cases", { + expect_equal( + ansi_strwrap("\033[32mfoo\fbar\033[39m"), + ansi_string(c("\033[32mfoo\033[39m", "\033[32mbar\033[39m")) + ) + expect_equal( + ansi_strwrap("\033[32m\ffoo\f\033[39m"), + ansi_string(c("", "\033[32mfoo\033[39m")) + ) +}) + test_that_cli(configs = c("plain", "ansi"), "ansi_strtrim", { withr::local_options(c(cli.unicode = FALSE)) setup_unicode_width_fix()