Crate should_color
source · [−]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 (requiresclicolor_forcefeature),- explicit user preference (for instance command line arguments),
CLICOLORenvironment variable (requiresclicolorfeature),NO_COLORenvironment variable (requiresno_colorfeature),- application default choice.
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 implement 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.
Enums
Possible color choices for the output.
Constants
clicolorName of the CLICOLOR environment variable.
clicolor_forceName of the CLICOLOR_FORCE environment variable.
no_colorName of the NO_COLOR environment variable.
Functions
clapCompute a clap::ColorChoice
suitable for the clap::App::color setting.
clicolorGet the setting of the CLICOLOR environment variable.
clicolor_forceGet the setting of the CLICOLOR_FORCE environment variable.
no_colorGet the setting of the NO_COLOR environment variable.
Resolve the output color choice from the environment variables and an explicit CLI preference.