Skip to content

This function is similar to base::trimws() but works on ANSI strings, and keeps color and other styling.

Usage

ansi_trimws(x, which = c("both", "left", "right"))

Arguments

x

ANSI string vector.

which

Whether to remove leading or trailing whitespace or both.

Value

ANSI string, with the whitespace removed.

See also

Examples

trimws(paste0("   ", col_red("I am red"), "   "))
#> [1] "\033[31mI am red\033[39m"
ansi_trimws(paste0("   ", col_red("I am red"), "   "))
#> <cli_ansi_string>
#> [1] I am red
trimws(col_red("   I am red   "))
#> <cli_ansi_string>
#> [1]    I am red   
ansi_trimws(col_red("   I am red   "))
#> <cli_ansi_string>
#> [1] I am red