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.
col_substr(x, start, stop)Character vector of the same length as x, containing
the requested substrings. ANSI styles are retained.
Other ANSI string operations:
col_align(),
col_nchar(),
col_strsplit(),
col_substring()
str <- paste(
red("red"),
"default",
green("green")
)
cat(str, "\n")
#> red default green
cat(col_substr(str, 1, 5), "\n")
#> red d
cat(col_substr(str, 1, 15), "\n")
#> red default gre
cat(col_substr(str, 3, 7), "\n")
#> d def
substr(strip_style(str), 1, 5)
#> [1] "red d"
substr(strip_style(str), 1, 15)
#> [1] "red default gre"
substr(strip_style(str), 3, 7)
#> [1] "d def"
str2 <- "another " %+%
red("multi-", sep = "", underline("style")) %+%
" text"
cat(str2, "\n")
#> another multi-style text
cat(col_substr(c(str, str2), c(3,5), c(7, 18)), sep = "\n")
#> d def
#> her multi-styl
substr(strip_style(c(str, str2)), c(3,5), c(7, 18))
#> [1] "d def" "her multi-styl"