ansi_strsplit: Split an ANSI colored string

Description Usage Arguments Value Examples

View source: R/strsplit.R

Description

This is the color-aware counterpart of base::strsplit. It works almost exactly like the original, but keeps the colors in the substrings.

Usage

1

Arguments

x

Character vector, potentially ANSI styled, or a vector to coarced to character.

split

Character vector of length 1 (or object which can be coerced to such) containing regular expression(s) (unless fixed = TRUE) to use for splitting. If empty matches occur, in particular if split has zero characters, x is split into single characters.

...

Extra arguments are passed to base::strsplit.

Value

A list of the same length as x, the i-th element of which contains the vector of splits of x[i]. ANSI styles are retained.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
str <- paste0(
  crayon::red("I am red---"),
  crayon::green("and I am green-"),
  crayon::underline("I underlined")
)

cat(str, "\n")

# split at dashes, keep color
cat(ansi_strsplit(str, "[-]+")[[1]], sep = "\n")
strsplit(crayon::strip_style(str), "[-]+")

# split to characters, keep color
cat(ansi_strsplit(str, "")[[1]], "\n", sep = " ")
strsplit(crayon::strip_style(str), "")

r-lib/ansistrings documentation built on March 7, 2020, 10:13 p.m.