col_strsplit: Split an ANSI colored string

View source: R/string_operations.R

col_strsplitR Documentation

Split an ANSI colored string

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

col_strsplit(x, split, ...)

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.

See Also

Other ANSI string operations: col_align(), col_nchar(), col_substr(), col_substring()

Examples

str <- red("I am red---") %+%
  green("and I am green-") %+%
  underline("I underlined")

cat(str, "\n")

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

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

crayon documentation built on June 22, 2024, 9:39 a.m.