Skip to content

Commit

Permalink
Merge pull request #7 from hugovk/env-vars
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Apr 29, 2022
2 parents 210dbca + a5685c7 commit 68d1676
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 92 deletions.
3 changes: 0 additions & 3 deletions .editorconfig
Expand Up @@ -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
40 changes: 6 additions & 34 deletions .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
35 changes: 8 additions & 27 deletions .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: |
Expand All @@ -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 }}
33 changes: 24 additions & 9 deletions .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"]
Expand All @@ -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
1 change: 0 additions & 1 deletion COPYING.txt
Expand Up @@ -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.

2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -21,7 +21,7 @@ for i in range(10):
cprint("Attention!", "red", attrs=["bold"], file=sys.stderr)
```

## Text Properties
## Text properties

Text colors:

Expand Down
25 changes: 13 additions & 12 deletions src/termcolor/termcolor.py
Expand Up @@ -20,7 +20,7 @@
#
# Author: Konstantin Lepa <konstantin.lepa@gmail.com>

"""ANSII Color formatting for output in terminal."""
"""ANSII color formatting for output in terminal."""

import os

Expand Down Expand Up @@ -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):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_termcolor.py
@@ -1,3 +1,5 @@
import os

import pytest

from termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, colored, cprint
Expand All @@ -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"

Expand Down Expand Up @@ -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"
14 changes: 9 additions & 5 deletions 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

0 comments on commit 68d1676

Please sign in to comment.