Skip to content

This function is similar to base::strwrap(), but works on ANSI styled strings, and leaves the styling intact.

Usage

ansi_strwrap(
  x,
  width = console_width(),
  indent = 0,
  exdent = 0,
  simplify = TRUE
)

Arguments

x

ANSI string.

width

Width to wrap to.

indent

Indentation of the first line of each paragraph.

exdent

Indentation of the subsequent lines of each paragraph.

simplify

Whether to return all wrapped strings in a single character vector, or wrap each element of x independently and return a list.

Value

If simplify is FALSE, then a list of character vectors, each an ANSI string. Otherwise a single ANSI string vector.

See also

Examples

text <- cli:::lorem_ipsum()
# Highlight some words, that start with 's'
rexp <- gregexpr("\\b([sS][a-zA-Z]+)\\b", text)
regmatches(text, rexp) <- lapply(regmatches(text, rexp), col_red)
cat(text)
#> Nisi ea Lorem laboris dolore labore voluptate. Officia et culpa nisi eiusmod veniam nulla esse magna eu dolor consectetur id veniam. Nisi aute laborum occaecat consequat do qui velit proident aliqua fugiat cillum proident. Enim labore elit exercitation aute eiusmod quis laborum do ut. Lorem cillum excepteur excepteur ipsum nisi nulla minim proident. Culpa ullamco eu voluptate laboris exercitation deserunt dolore sunt adipisicing aute fugiat. Est laborum in sunt laboris enim. Fugiat ut culpa laborum cillum veniam.

wrp <- ansi_strwrap(text, width = 40)
cat(wrp, sep = "\n")
#> Nisi ea Lorem laboris dolore labore
#> voluptate. Officia et culpa nisi
#> eiusmod veniam nulla esse magna eu
#> dolor consectetur id veniam. Nisi aute
#> laborum occaecat consequat do qui velit
#> proident aliqua fugiat cillum proident.
#> Enim labore elit exercitation aute
#> eiusmod quis laborum do ut. Lorem
#> cillum excepteur excepteur ipsum nisi
#> nulla minim proident. Culpa ullamco eu
#> voluptate laboris exercitation deserunt
#> dolore sunt adipisicing aute fugiat.
#> Est laborum in sunt laboris enim.
#> Fugiat ut culpa laborum cillum veniam.