diff --git a/.editorconfig b/.editorconfig index 92909ef..179fd45 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,9 +13,6 @@ indent_size = 4 indent_style = space trim_trailing_whitespace = true -[tabulated_rst.py] -trim_trailing_whitespace = false - # Two-space indentation [*.yml] indent_size = 2 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index ce14782..649ca66 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,40 +1,12 @@ name: Lint -on: [push, pull_request] - -env: - FORCE_COLOR: 1 +on: [push, pull_request, workflow_dispatch] jobs: - build: - runs-on: ubuntu-20.04 + lint: + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - - name: Cache - uses: actions/cache@v2 - with: - path: | - ~/.cache/pip - ~/.cache/pre-commit - key: - lint-v2-${{ hashFiles('**/setup.py') }}-${{ - hashFiles('**/.pre-commit-config.yaml') }} - restore-keys: | - lint-v2- - - - name: Set up Python - uses: actions/setup-python@v3 - with: - python-version: 3.8 - - - name: Install dependencies - run: | - python -m pip install -U pip - python -m pip install -U tox - - - name: Lint - run: tox -e lint - env: - PRE_COMMIT_COLOR: always + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v2.0.3 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5df6b4b..d456cea 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,46 +1,28 @@ name: Test -on: [push, pull_request] +on: [push, pull_request, workflow_dispatch] env: FORCE_COLOR: 1 jobs: - build: + test: runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] - os: [ubuntu-latest, macos-latest, windows-latest] - include: - # Include new variables for Codecov - - { codecov-flag: GHA_Ubuntu, os: ubuntu-latest } - - { codecov-flag: GHA_macOS, os: macos-latest } - - { codecov-flag: GHA_Windows, os: windows-latest } + os: [windows-latest, macos-latest, ubuntu-latest] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v3 with: python-version: ${{ matrix.python-version }} - - - name: Get pip cache dir - id: pip-cache - run: | - echo "::set-output name=dir::$(pip cache dir)" - - - name: Cache - uses: actions/cache@v2 - with: - path: ${{ steps.pip-cache.outputs.dir }} - key: - ${{ matrix.os }}-${{ matrix.python-version }}-v2-${{ - hashFiles('**/setup.py') }} - restore-keys: | - ${{ matrix.os }}-${{ matrix.python-version }}-v2- + cache: pip + cache-dependency-path: setup.py - name: Install dependencies run: | @@ -49,12 +31,11 @@ jobs: python -m pip install -U tox - name: Tox tests - shell: bash run: | tox -e py - name: Upload coverage - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v3 with: - flags: ${{ matrix.codecov-flag }} + flags: ${{ matrix.os }} name: ${{ matrix.os }} Python ${{ matrix.python-version }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 484d481..0c48486 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,12 +1,12 @@ repos: - repo: https://github.com/asottile/pyupgrade - rev: v2.7.2 + rev: v2.32.0 hooks: - id: pyupgrade args: ["--py36-plus"] - repo: https://github.com/psf/black - rev: 20.8b1 + rev: 22.3.0 hooks: - id: black args: ["--target-version", "py36"] @@ -15,30 +15,45 @@ repos: types: [] - repo: https://github.com/PyCQA/isort - rev: 5.5.1 + rev: 5.10.1 hooks: - id: isort - - repo: https://gitlab.com/pycqa/flake8 - rev: 3.8.3 + - repo: https://github.com/PyCQA/flake8 + rev: 4.0.1 hooks: - id: flake8 additional_dependencies: [flake8-2020, flake8-implicit-str-concat] - repo: https://github.com/pre-commit/pygrep-hooks - rev: v1.6.0 + rev: v1.9.0 hooks: - id: python-check-blanket-noqa - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 + rev: v4.2.0 hooks: - id: check-merge-conflict - id: check-toml - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace - - repo: https://github.com/prettier/prettier - rev: 2.1.1 + - repo: https://github.com/tox-dev/pyproject-fmt + rev: 0.3.3 + hooks: + - id: pyproject-fmt + + - repo: https://github.com/tox-dev/tox-ini-fmt + rev: 0.5.2 + hooks: + - id: tox-ini-fmt + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v2.6.2 hooks: - id: prettier args: [--prose-wrap=always, --print-width=88] + +ci: + autoupdate_schedule: quarterly diff --git a/COPYING.txt b/COPYING.txt index d5df976..d0b7970 100644 --- a/COPYING.txt +++ b/COPYING.txt @@ -17,4 +17,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/README.md b/README.md index 34b7580..50f8484 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ for i in range(10): cprint("Attention!", "red", attrs=["bold"], file=sys.stderr) ``` -## Text Properties +## Text properties Text colors: diff --git a/src/termcolor/termcolor.py b/src/termcolor/termcolor.py index 6ad6b93..4ddacaf 100644 --- a/src/termcolor/termcolor.py +++ b/src/termcolor/termcolor.py @@ -20,7 +20,7 @@ # # Author: Konstantin Lepa -"""ANSII Color formatting for output in terminal.""" +"""ANSII color formatting for output in terminal.""" import os @@ -105,20 +105,21 @@ def colored(text, color=None, on_color=None, attrs=None): colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink']) colored('Hello, World!', 'green') """ - if os.getenv("ANSI_COLORS_DISABLED") is None: - fmt_str = "\033[%dm%s" - if color is not None: - text = fmt_str % (COLORS[color], text) + if "NO_COLOR" in os.environ or "ANSI_COLORS_DISABLED" in os.environ: + return text - if on_color is not None: - text = fmt_str % (HIGHLIGHTS[on_color], text) + fmt_str = "\033[%dm%s" + if color is not None: + text = fmt_str % (COLORS[color], text) - if attrs is not None: - for attr in attrs: - text = fmt_str % (ATTRIBUTES[attr], text) + if on_color is not None: + text = fmt_str % (HIGHLIGHTS[on_color], text) - text += RESET - return text + if attrs is not None: + for attr in attrs: + text = fmt_str % (ATTRIBUTES[attr], text) + + return text + RESET def cprint(text, color=None, on_color=None, attrs=None, **kwargs): diff --git a/tests/test_termcolor.py b/tests/test_termcolor.py index 9dbc5b7..f3b0c02 100644 --- a/tests/test_termcolor.py +++ b/tests/test_termcolor.py @@ -1,3 +1,5 @@ +import os + import pytest from termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, colored, cprint @@ -7,6 +9,14 @@ ALL_ATTRIBUTES = list(ATTRIBUTES) + [None] +def setup_module(): + # By default, make sure no env vars already set for tests + try: + del os.environ["ANSI_COLORS_DISABLED"] + except KeyError: # pragma: no cover + pass + + def test_basic(): assert colored("text") == "text\x1b[0m" @@ -79,3 +89,26 @@ def test_on_color(capsys, on_color, expected): def test_attrs(capsys, attr, expected): assert colored("text", attrs=[attr]) == expected assert_cprint(capsys, expected, "text", attrs=[attr]) + + +@pytest.mark.parametrize( + "test_env_var", + [ + "ANSI_COLORS_DISABLED", + "NO_COLOR", + ], +) +@pytest.mark.parametrize( + "test_value", + [ + "true", + "false", + "1", + "0", + "", + ], +) +def test_env_var(monkeypatch, test_env_var, test_value): + """Assert nothing applied when this env var set, regardless of value.""" + monkeypatch.setenv(test_env_var, test_value) + assert colored("text", color="red") == "text" diff --git a/tox.ini b/tox.ini index daff6ed..8ce41fd 100644 --- a/tox.ini +++ b/tox.ini @@ -1,17 +1,21 @@ [tox] envlist = lint - py{36, 37, 38, 39, 310} + py{310, 39, 38, 37, 36} [testenv] +passenv = + FORCE_COLOR extras = tests commands = {envpython} -m pytest --cov termcolor --cov tests --cov-report xml {posargs} -passenv = FORCE_COLOR [testenv:lint] -deps = pre-commit -commands = pre-commit run --all-files --show-diff-on-failure +passenv = + PRE_COMMIT_COLOR skip_install = true -passenv = PRE_COMMIT_COLOR +deps = + pre-commit +commands = + pre-commit run --all-files --show-diff-on-failure