Skip to content

Commit

Permalink
Respect color environment variables if set (#813)
Browse files Browse the repository at this point in the history
According to command line standards [1], a command line should
do its best to honor certain environment variables requesting
whether color should be part of the standard output. Two such
vars include NO_COLOR (if set) and TERM (if set to dumb), when
set tell the CLI to disable color.

[1] https://clig.dev/#output

Partially-fixes #678

Signed-off-by: Eric Brown <browne@vmware.com>
  • Loading branch information
ericwb committed Feb 17, 2022
1 parent a3d8b4b commit 1691b93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion bandit/cli/main.py
Expand Up @@ -265,7 +265,15 @@ def main():
' not be listed in "low".',
choices=["all", "low", "medium", "high"],
)
output_format = "screen" if sys.stdout.isatty() else "txt"
output_format = (
"screen"
if (
sys.stdout.isatty()
and os.getenv("NO_COLOR") is None
and os.getenv("TERM") != "dumb"
)
else "txt"
)
parser.add_argument(
"-f",
"--format",
Expand Down
10 changes: 9 additions & 1 deletion bandit/core/manager.py
Expand Up @@ -163,7 +163,15 @@ def output_results(
try:
formatters_mgr = extension_loader.MANAGER.formatters_mgr
if output_format not in formatters_mgr:
output_format = "screen" if sys.stdout.isatty() else "txt"
output_format = (
"screen"
if (
sys.stdout.isatty()
and os.getenv("NO_COLOR") is None
and os.getenv("TERM") != "dumb"
)
else "txt"
)

formatter = formatters_mgr[output_format]
report_func = formatter.plugin
Expand Down

0 comments on commit 1691b93

Please sign in to comment.