cli has a number of functions to color and style text at the command line. These all use the crayon package under the hood, but provide a slightly simpler interface.
bg_black(...) bg_blue(...) bg_cyan(...) bg_green(...) bg_magenta(...) bg_red(...) bg_white(...) bg_yellow(...) col_black(...) col_blue(...) col_cyan(...) col_green(...) col_magenta(...) col_red(...) col_white(...) col_yellow(...) col_grey(...) col_silver(...) style_dim(...) style_blurred(...) style_bold(...) style_hidden(...) style_inverse(...) style_italic(...) style_reset(...) style_strikethrough(...) style_underline(...)
Character strings, they will be pasted together with
An ANSI string (class
ansi_string), that contains ANSI
sequences, if the current platform supports them. You can simply
cat() to print them to the terminal.
col_* functions change the (foreground) color to the text.
These are the eight original ANSI colors. Note that in some terminals,
they might actually look differently, as terminals have their own
settings for how to show them.
bg_* functions change the background color of the text.
These are the eight original ANSI background colors. These, too, can
vary in appearence, depending on terminal settings.
style_* functions apply other styling to the text. The currently
supported styling funtions are:
style_reset() to remove any style, including color,
style_bold() for boldface / strong text, although some terminals
show a bright, high intensity text instead,
style_blurred() reduced intensity text.
style_italic() (not widely supported).
style_strikethrough() (not widely supported).
The style functions take any number of character vectors as arguments,
and they concatenate them using
paste0() before adding the style.
Styles can also be nested, and then inner style takes precedence, see examples below.
col_blue("Hello ", "world!")#> <ansi_string> #>  Hello world!cat(col_blue("Hello ", "world!"))#> Hello world!cat("... to highlight the", col_red("search term"), "in a block of text\n")#> ... to highlight the search term in a block of text## Style stack properly cat(col_green( "I am a green line ", col_blue(style_underline(style_bold("with a blue substring"))), " that becomes green again!" ))#> I am a green line with a blue substring that becomes green again!error <- combine_ansi_styles("red", "bold") warn <- combine_ansi_styles("magenta", "underline") note <- col_cyan cat(error("Error: subscript out of bounds!\n"))#> Error: subscript out of bounds! #>cat(warn("Warning: shorter argument was recycled.\n"))#> Warning: shorter argument was recycled. #>cat(note("Note: no such directory.\n"))#> Note: no such directory. #>