| num_ansi_colors {crayon} | R Documentation |
Detect the number of ANSI colors to use
Description
Certain Unix and Windows terminals, and also certain R GUIs, e.g. RStudio, support styling terminal output using special control sequences (ANSI sequences).
num_ansi_colors() detects if the current R session supports ANSI
sequences, and if it does how many colors are supported.
Usage
num_ansi_colors(stream = "auto")
detect_tty_colors()
Arguments
stream |
The stream that will be used for output, an R connection
object. It can also be a string, one of |
Details
The detection mechanism is quite involved and it is designed to work out of the box on most systems. If it does not work on your system, please report a bug. Setting options and environment variables to turn on ANSI support is error prone, because they are inherited in other environments, e.g. knitr, that might not have ANSI support.
If you want to turn off ANSI colors, set the NO_COLOR environment
variable to a non-empty value.
The exact detection mechanism is as follows:
If the
cli.num_colorsoptions is set, that is returned.If the
R_CLI_NUM_COLORSenvironment variable is set to a non-empty value, then it is used.If the
crayon.enabledoption is set toFALSE, 1L is returned. (This is for compatibility with code that uses the crayon package.)If the
crayon.enabledoption is set toTRUEand thecrayon.colorsoption is not set, then the value of thecli.default_num_colorsoption, or if it is unset, then 8L is returned.If the
crayon.enabledoption is set toTRUEand thecrayon.colorsoption is also set, then the latter is returned. (This is for compatibility with code that uses the crayon package.)If the
NO_COLORenvironment variable is set, then 1L is returned.If we are in knitr, then 1L is returned, to turn off colors in
.Rmdchunks.If
streamis"auto"(the default) and there is an active sink (either for"output"or"message"), then we return 1L. (In theory we would only need to check the stream that will be be actually used, but there is no easy way to tell that.)If
streamis not"auto", but it isstderr()and there is an active sink for it, then 1L is returned. (If a sink is active for "output", then R changes thestdout()stream, so this check is not needed.)If the
cli.default_num_colorsoption is set, then we use that.If R is running inside RGui on Windows, or R.app on macOS, then we return 1L.
If R is running inside RStudio, with color support, then the appropriate number of colors is returned, usually 256L.
If R is running on Windows, inside an Emacs version that is recent enough to support ANSI colors, then the value of the
cli.default_num_colorsoption, or if unset 8L is returned. (On Windows, Emacs hasisatty(stdout()) == FALSE, so we need to check for this here before dealing with terminals.)If
streamis not the standard output or standard error in a terminal, then 1L is returned.Otherwise we use and cache the result of the terminal color detection (see below).
The terminal color detection algorithm:
If the
COLORTERMenvironment variable is set totruecoloror24bit, then we return 16 million colors.If the
COLORTERMenvironment variable is set to anything else, then we return the value of thecli.num_default_colorsoption, 8L if unset.If R is running on Unix, inside an Emacs version that is recent enough to support ANSI colors, then the value of the
cli.default_num_colorsoption is returned, or 8L if unset.If we are on Windows in an RStudio terminal, then apparently we only have eight colors, but the
cli.default_num_colorsoption can be used to override this.If we are in a recent enough Windows 10 terminal, then there is either true color (from build 14931) or 256 color (from build 10586) support. You can also use the
cli.default_num_colorsoption to override these.If we are on Windows, under ConEmu or cmder, or ANSICON is loaded, then the value of
cli.default_num_colors, or 8L if unset, is returned.Otherwise if we are on Windows, return 1L.
Otherwise we are on Unix and try to run
tput colorsto determine the number of colors. If this succeeds, we return its return value. If theTERMenvironment variable isxtermandtputreturned 8L, we return 256L, because xterm compatible terminals tend to support 256 colors (https://github.com/r-lib/crayon/issues/17) You can override this with thecli.default_num_colorsoption.If
TERMis set todumb, we return 1L.If
TERMstarts withscreen,xterm, orvt100, we return 8L.If
TERMcontainscolor,ansi,cygwinorlinux, we return 8L.Otherwise we return 1L.
Value
Integer, the number of ANSI colors the current R session
supports for stream.
Examples
num_ansi_colors()