GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cli
  • FAQ
  • Reference
  • Building a Semantic CLI
  • From usethis::ui functions to cli
  • cli pluralization
  • Introduction to progress bars in cli
  • Advanced cli progress bars
  • cli color palettes
  • More articles...
  • Changelog

This view

  • Original page
  • New address

GeezerVison is showing cli.r-lib.org — Substring(s) of an ANSI colored string — ansi_substring

Substring(s) of an ANSI colored string

Source: R/ansiex.R
ansi_substring.Rd

This is the color-aware counterpart of base::substring(). 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

ansi_substring(text, first, last = 1000000L)

Arguments

text

Character vector, potentially ANSI styled, or a vector to coerced to character. It is recycled to the longest of first and last.

first

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

last

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: ansi_align(), ansi_columns(), ansi_nchar(), ansi_strsplit(), ansi_strtrim(), ansi_strwrap(), ansi_substr(), ansi_toupper(), ansi_trimws()

Examples

str <- paste(
  col_red("red"),
  "default",
  col_green("green")
)

cat(str, "\n")
#> red default green 
cat(ansi_substring(str, 1, 5), "\n")
#> red d 
cat(ansi_substring(str, 1, 15), "\n")
#> red default gre 
cat(ansi_substring(str, 3, 7), "\n")
#> d def 

substring(ansi_strip(str), 1, 5)
#> [1] "red d"
substring(ansi_strip(str), 1, 15)
#> [1] "red default gre"
substring(ansi_strip(str), 3, 7)
#> [1] "d def"

str2 <- paste(
  "another",
  col_red("multi-", style_underline("style")),
  "text"
)

cat(str2, "\n")
#> another multi-style text 
cat(ansi_substring(str2, c(3,5), c(7, 18)), sep = "\n")
#> other
#> her multi-styl
substring(ansi_strip(str2), c(3,5), c(7, 18))
#> [1] "other"          "her multi-styl"
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.