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.
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_colors
options is set, that is returned.If the
R_CLI_NUM_COLORS
environment variable is set to a non-empty value, then it is used.If the
crayon.enabled
option is set toFALSE
, 1L is returned. (This is for compatibility with code that uses the crayon package.)If the
crayon.enabled
option is set toTRUE
and thecrayon.colors
option is not set, then the value of thecli.default_num_colors
option, or if it is unset, then 8L is returned.If the
crayon.enabled
option is set toTRUE
and thecrayon.colors
option is also set, then the latter is returned. (This is for compatibility with code that uses the crayon package.)If the
NO_COLOR
environment variable is set, then 1L is returned.If we are in knitr, then 1L is returned, to turn off colors in
.Rmd
chunks.If
stream
is"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
stream
is 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_colors
option 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_colors
option, 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
stream
is 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
COLORTERM
environment variable is set totruecolor
or24bit
, then we return 16 million colors.If the
COLORTERM
environment variable is set to anything else, then we return the value of thecli.num_default_colors
option, 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_colors
option 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_colors
option 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_colors
option 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 colors
to determine the number of colors. If this succeeds, we return its return value. If theTERM
environment variable isxterm
andtput
returned 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_colors
option.If
TERM
is set todumb
, we return 1L.If
TERM
starts withscreen
,xterm
, orvt100
, we return 8L.If
TERM
containscolor
,ansi
,cygwin
orlinux
, we return 8L.Otherwise we return 1L.
See also
Other ANSI styling:
ansi-styles
,
combine_ansi_styles()
,
make_ansi_style()