Skip to content

Commit

Permalink
Fix colorama not being disabled after being used
Browse files Browse the repository at this point in the history
  • Loading branch information
adambenali committed Mar 9, 2020
1 parent 0211dd7 commit 7cf6139
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions bandit/formatters/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@
from bandit.core import docs_utils
from bandit.core import test_properties

IS_WIN_PLATFORM = sys.platform.startswith('win32')
COLORAMA = False

# This fixes terminal colors not displaying properly on Windows systems.
# Colorama will intercept any ANSI escape codes and convert them to the
# proper Windows console API calls to change text color.
if sys.platform.startswith('win32'):
if IS_WIN_PLATFORM:
try:
import colorama
except ImportError:
pass
else:
colorama.init()
COLORAMA = True


LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -161,6 +165,9 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
:param lines: Number of lines to report, -1 for all
"""

if IS_WIN_PLATFORM and COLORAMA:
colorama.init()

bits = []
if not manager.quiet or manager.results_count(sev_level, conf_level):
bits.append(header("Run started:%s", datetime.datetime.utcnow()))
Expand All @@ -186,3 +193,6 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
if fileobj.name != sys.stdout.name:
LOG.info("Screen formatter output was not written to file: %s, "
"consider '-f txt'", fileobj.name)

if IS_WIN_PLATFORM and COLORAMA:
colorama.deinit()

0 comments on commit 7cf6139

Please sign in to comment.