Skip to content

Commit

Permalink
Fix ansi_strwrap edge cases
Browse files Browse the repository at this point in the history
When `\f` is inside ANSI markup.
  • Loading branch information
gaborcsardi committed Oct 21, 2021
1 parent 92625f7 commit 582dd41
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions R/ansiex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")) {
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-ansiex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 582dd41

Please sign in to comment.