Skip to contentSkip to conversation

lib/bb: support NO_COLOR

PATCH New Peter A. Bigot 1 participant 1 message Mar 12, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/bb: support NO_COLOR

Red text on a black background can make it difficult for people with visual impairments to read the text of error messages. Respect the presence of a non-empty NO_COLOR environment variable as an indication the user doesn't want colorization to be automatically enabled.

See: https://no-color.org/

---
 lib/bb/msg.py       | 2 +-
 lib/bb/ui/knotty.py | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
lib/bb/msg.py +1 -1
index 93575d89..3e18596f 100644
@@ -230,7 +230,7 @@ def logger_create(name, output=sys.stderr, level=logging.INFO, preserve_handlers
     console = logging.StreamHandler(output)
     console.addFilter(bb.msg.LogFilterShowOnce())
     format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
-    if color == 'always' or (color == 'auto' and output.isatty()):
+    if color == 'always' or (color == 'auto' and output.isatty() and os.environ.get('NO_COLOR', '') == ''):
         format.enable_color()
     console.setFormatter(format)
     if preserve_handlers:
lib/bb/ui/knotty.py +1 -1
index 5a97d040..f86999bb 100644
@@ -179,7 +179,7 @@ class TerminalFilter(object):
             new[3] = new[3] & ~termios.ECHO
             termios.tcsetattr(fd, termios.TCSADRAIN, new)
             curses.setupterm()
-            if curses.tigetnum("colors") > 2:
+            if curses.tigetnum("colors") > 2 and os.environ.get('NO_COLOR', '') == '':
                 for h in handlers:
                     try:
                         h.formatter.enable_color()
Signature · 1 line
2.34.1