Skip to content

This is a color-aware counterpart of utf8_nchar(). By default it counts Unicode grapheme clusters, instead of code points.

Usage

ansi_nchar(x, type = c("chars", "bytes", "width", "graphemes", "codepoints"))

Arguments

x

Character vector, potentially ANSI styled, or a vector to be coerced to character. If it converted to UTF-8.

type

Whether to count graphemes (characters), code points, bytes, or calculate the display width of the string.

Value

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

See also

Examples

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

cat(str, "\n")
#> red default green 
nchar(str)
#> [1] 37
ansi_nchar(str)
#> [1] 17
nchar(ansi_strip(str))
#> [1] 17