col_substr: Substring(s) of an ANSI colored string

Description Usage Arguments Value See Also Examples

View source: R/string_operations.r

Description

This is a color-aware counterpart of base::substr(). It works exactly like the original, but keeps the colors in the substrings. The ANSI escape sequences are ignored when calculating the positions within the string.

Usage

1

Arguments

x

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

start

Starting index or indices, recycled to match the length of x.

stop

Ending index or indices, recycled to match the length of x.

Value

Character vector of the same length as x, containing the requested substrings. ANSI styles are retained.

See Also

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
str <- paste(
  red("red"),
  "default",
  green("green")
)

cat(str, "\n")
cat(col_substr(str, 1, 5), "\n")
cat(col_substr(str, 1, 15), "\n")
cat(col_substr(str, 3, 7), "\n")

substr(strip_style(str), 1, 5)
substr(strip_style(str), 1, 15)
substr(strip_style(str), 3, 7)

str2 <- "another " %+%
  red("multi-", sep = "", underline("style")) %+%
  " text"

cat(str2, "\n")
cat(col_substr(c(str, str2), c(3,5), c(7, 18)), sep = "\n")
substr(strip_style(c(str, str2)), c(3,5), c(7, 18))

Example output

red default green 
red d 
red default gre 
d def 
[1] "red d"
[1] "red default gre"
[1] "d def"
another multi-style text 
d def
her multi-styl
[1] "d def"          "her multi-styl"

crayon documentation built on Feb. 9, 2021, 1:06 a.m.