ansi_nchar: Number of characters or bytes in an ANSI substring

Description Usage Arguments Examples

View source: R/nchar.R

Description

This is the ANSI-aware version of the base::nchar() function. It simply drops all ANSI styles and calls base::nchar().

Usage

1

Arguments

x

character vector, or a vector to be coerced to a character vector. Giving a factor is an error.

...

Extra arguments are passed to base::nchar().

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
str <- crayon::red("I am red")

# Has ANSI markup if your terminal supports it
str

# It prints in red if your terminal supports it
cat(str)

# But base::nchar() also counts the ANSI control characters,
# which is not what we want
nchar(str)

# ansi_nchar is better:
ansi_nchar(str)

# This is the same
nchar(crayon::strip_style(str))

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