Skip to content

Commit 1691b93

Browse files
authored
Respect color environment variables if set (#813)
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 <[email protected]>
1 parent a3d8b4b commit 1691b93

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

bandit/cli/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,15 @@ def main():
265265
' not be listed in "low".',
266266
choices=["all", "low", "medium", "high"],
267267
)
268-
output_format = "screen" if sys.stdout.isatty() else "txt"
268+
output_format = (
269+
"screen"
270+
if (
271+
sys.stdout.isatty()
272+
and os.getenv("NO_COLOR") is None
273+
and os.getenv("TERM") != "dumb"
274+
)
275+
else "txt"
276+
)
269277
parser.add_argument(
270278
"-f",
271279
"--format",

bandit/core/manager.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,15 @@ def output_results(
163163
try:
164164
formatters_mgr = extension_loader.MANAGER.formatters_mgr
165165
if output_format not in formatters_mgr:
166-
output_format = "screen" if sys.stdout.isatty() else "txt"
166+
output_format = (
167+
"screen"
168+
if (
169+
sys.stdout.isatty()
170+
and os.getenv("NO_COLOR") is None
171+
and os.getenv("TERM") != "dumb"
172+
)
173+
else "txt"
174+
)
167175

168176
formatter = formatters_mgr[output_format]
169177
report_func = formatter.plugin

0 commit comments

Comments
 (0)