ansi_nchar {cli}R Documentation

Count number of characters in an ANSI colored string

Description

This is a color-aware counterpart of base::nchar(), which does not do well, since it also counts the ANSI control characters.

Usage

ansi_nchar(x, ...)

Arguments

x

Character vector, potentially ANSO styled, or a vector to be coarced to character.

...

Additional arguments, passed on to base::nchar() after removing ANSI escape sequences.

Value

Numeric vector, the length of the strings in the character vector.

See Also

Other ANSI string operations: ansi_align(), ansi_strsplit(), ansi_substring(), ansi_substr()

Examples

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

cat(str, "\n")
nchar(str)
ansi_nchar(str)
nchar(ansi_strip(str))

[Package cli version 2.2.0 Index]