Skip to content

Commit

Permalink
Support the NO_COLOR environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Mar 17, 2023
1 parent 87c0441 commit 1e28fbc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ Changes
=======


1.2.2 (unreleased)
1.3.0 (unreleased)
------------------

- Nothing changed yet.
- Support the NO_COLOR environment variable for disabling color autodetection
(see https://no-color.org/).


1.2.1 (2022-10-28)
Expand Down
13 changes: 11 additions & 2 deletions strace_process_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from functools import partial


__version__ = '1.2.2.dev0'
__version__ = '1.3.0.dev0'
__author__ = 'Marius Gedminas <marius@gedmin.as>'
__url__ = "https://github.com/mgedmin/strace-process-tree"
__licence__ = 'GPL v2 or v3' # or ask me for MIT
Expand Down Expand Up @@ -71,7 +71,11 @@ def __init__(self, color=None, unicode=None):

@classmethod
def should_use_color(cls):
return cls.is_terminal() and cls.terminal_supports_color()
return (
cls.is_terminal()
and cls.terminal_supports_color()
and not cls.user_dislikes_color()
)

@classmethod
def is_terminal(cls):
Expand All @@ -81,6 +85,11 @@ def is_terminal(cls):
def terminal_supports_color(cls):
return (os.environ.get('TERM') or 'dumb') != 'dumb'

@classmethod
def user_dislikes_color(cls):
# https://no-color.org/
return bool(os.environ.get('NO_COLOR'))

@classmethod
def can_unicode(cls):
return getattr(sys.stdout, 'encoding', None) == 'UTF-8'
Expand Down
32 changes: 27 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import os
import sys

import pytest
Expand All @@ -22,24 +21,47 @@ def test_Theme_is_terminal_yes_it_is(monkeypatch):


def test_Theme_terminal_supports_color_no(monkeypatch):
monkeypatch.setitem(os.environ, 'TERM', 'dumb')
monkeypatch.setenv('TERM', 'dumb')
assert not stp.Theme.terminal_supports_color()


def test_Theme_terminal_supports_color_yes(monkeypatch):
monkeypatch.setitem(os.environ, 'TERM', 'xterm')
monkeypatch.setenv('TERM', 'xterm')
assert stp.Theme.terminal_supports_color()


def test_Theme_no_color_unset(monkeypatch):
monkeypatch.delenv('NO_COLOR', raising=False)
assert not stp.Theme.user_dislikes_color()


def test_Theme_no_color_blank(monkeypatch):
monkeypatch.setenv('NO_COLOR', '')
assert not stp.Theme.user_dislikes_color()


def test_Theme_no_color_nonblank(monkeypatch):
monkeypatch.setenv('NO_COLOR', 'please')
assert stp.Theme.user_dislikes_color()


def test_Theme_autodetection_color_yes(monkeypatch):
monkeypatch.setattr(sys, 'stdout', FakeStdout())
monkeypatch.setitem(os.environ, 'TERM', 'xterm')
monkeypatch.setenv('TERM', 'xterm')
monkeypatch.delenv('NO_COLOR', raising=False)
assert isinstance(stp.Theme(), stp.AnsiTheme)


def test_Theme_autodetection_color_no(monkeypatch):
monkeypatch.setattr(sys, 'stdout', FakeStdout())
monkeypatch.setitem(os.environ, 'TERM', 'dumb')
monkeypatch.setenv('TERM', 'dumb')
assert isinstance(stp.Theme(), stp.PlainTheme)


def test_Theme_autodetection_color_disabled(monkeypatch):
monkeypatch.setattr(sys, 'stdout', FakeStdout())
monkeypatch.setenv('TERM', 'xterm')
monkeypatch.setenv('NO_COLOR', '1')
assert isinstance(stp.Theme(), stp.PlainTheme)


Expand Down

0 comments on commit 1e28fbc

Please sign in to comment.