Expand description
Determine whether output should use colors or not.
The resulting color choice is determined by taking into account, in order of priority from higher to lower, the following settings:
CLICOLOR_FORCEenvironment variable (requires theclicolor_forcefeature),- explicit user preference (for instance command line arguments),
CLICOLORenvironment variable (requires theclicolorfeature),NO_COLORenvironment variable (requires theno_colorfeature),- application default choice.
If the final choice is ColorChoice::Auto and the feature stream is enabled,
the choice can be refined using ColorChoice::for_stream which takes into account the output stream.
The specification of CLICOLOR, CLICOLOR_FORCE, and NO_COLOR is inspired by:
with the exception that variables which are set to the empty string ""
are treated as if they were unset.
The reason is that it is common to override environment variables by executing programs as
VAR= cmd args... and expect that VAR is unset.
§CLICOLOR_FORCE
Requires the clicolor_force feature.
The meaning of the environment variable is the following:
- if not set or
CLICOLOR_FORCE == ""orCLICOLOR_FORCE == "0": ignore; - if set and
CLICOLOR_FORCE != ""andCLICOLOR_FORCE != "0":ColorChoice::Always.
§CLICOLOR
Requires the clicolor feature.
The meaning of the environment variable is the following:
- if not set or
CLICOLOR == "": ignore; - if set and
CLICOLOR == "0":ColorChoice::Never; - if set and
CLICOLOR != ""andCLICOLOR != "0":ColorChoice::Auto.
§NO_COLOR
Requires the no_color feature.
The meaning of the environment variable is the following:
- if not set or
NO_COLOR == "": ignore; - if set and
NO_COLOR != "":ColorChoice::Never.
§Compatibility
The goal of this crate is to merge and specify the standards proposed in https://no-color.org and https://bixense.com/clicolors/.
Please note that the proposals in the latter are slightly ambiguous and undesirable (see this issue), hence they are merely taken as an inspiration and not followed too strictly.
Relevant quote from https://no-color.org:
Command-line software which adds ANSI color to its output by default should check for a
NO_COLORenvironment variable that, when present and not an empty string (regardless of its value), prevents the addition of ANSI color.
Relevant quote from https://bixense.com/clicolors/:
The idea is to have the environment variables
CLICOLORandCLICOLOR_FORCE(which are currently already used for this exact reason on some UNIX systems). When set, the following rules should apply:
CLICOLOR != 0: ANSI colors are supported and should be used when the program isn’t piped,CLICOLOR == 0: don’t output ANSI color escape codes,CLICOLOR_FORCE != 0: ANSI colors should be enabled no matter what.
§Crate features
clicolor(enabled by default) — Enables the detection ofCLICOLORviaclicolor.clicolor_force(enabled by default) — Enables the detection ofCLICOLOR_FORCEviaclicolor_force.no_color(enabled by default) — Enables the detection ofNO_COLORviano_color.stream(enabled by default) — AddsColorChoice::for_stream.clap— Addsclap_colorand conversion ofColorChoiceto and fromclap::ColorChoice.
Enums§
- Color
Choice - Possible color choices for the output.
Constants§
- CLICOLOR
clicolor - Name of the
CLICOLORenvironment variable. - CLICOLOR_
FORCE clicolor_force - Name of the
CLICOLOR_FORCEenvironment variable. - NO_
COLOR no_color - Name of the
NO_COLORenvironment variable.
Functions§
- clap_
color clap - Compute a
clap::ColorChoicesuitable for theclap::App::colorsetting. - clicolor
clicolor - Get the setting of the
CLICOLORenvironment variable. - clicolor_
force clicolor_force - Get the setting of the
CLICOLOR_FORCEenvironment variable. - no_
color no_color - Get the setting of the
NO_COLORenvironment variable. - resolve
- Resolve the output color choice from the environment variables and an explicit CLI preference.