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

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

Arguments

x

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

type

Whether to count characters, bytes, or calculate the display width of the string. Passed to base::nchar().

...

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

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
#> [1] 17