Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added FORCE_COLOR and NO_COLOR environment variables #230

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 246 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,261 @@
name: Test

on: [push, pull_request, workflow_dispatch]

env:
FORCE_COLOR: 1
on:
push:
pull_request:
schedule:
- cron: '0 0 * * 5'
workflow_dispatch:

jobs:

mac:
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.8
- run: ./demos/demo.sh
- run: ./demos/demo.sh
env:
FORCE_COLOR: true
- run: ./demos/demo.sh
env:
NO_COLOR: true

lin:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.8
- run: ./demos/demo.sh
- run: ./demos/demo.sh
env:
FORCE_COLOR: true
- run: ./demos/demo.sh
env:
NO_COLOR: true

docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: docker run --rm -tv $(pwd):/src -w /src/demos python:3-slim ./demo.sh
- run: docker run --rm -tv $(pwd):/src -w /src/demos -e FORCE_COLOR=true python:3-slim ./demo.sh
- run: docker run --rm -tv $(pwd):/src -w /src/demos -e NO_COLOR=true python:3-slim ./demo.sh

cmd:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.8
- shell: cmd
run: ./demos/demo.bat
- shell: cmd
run: ./demos/demo.bat
env:
FORCE_COLOR: true
- shell: cmd
run: ./demos/demo.bat
env:
NO_COLOR: true

ps:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.8
- shell: powershell
run: ./demos/demo.bat
- shell: powershell
run: ./demos/demo.bat
env:
FORCE_COLOR: true
- shell: powershell
run: ./demos/demo.bat
env:
NO_COLOR: true

bash:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.8
- shell: bash
run: ./demos/demo.sh
- shell: bash
run: ./demos/demo.sh
env:
FORCE_COLOR: true
- shell: bash
run: ./demos/demo.sh
env:
NO_COLOR: true

msys2:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.8
- uses: msys2/setup-msys2@v2
with:
msystem: MINGW64
update: true
- shell: msys2 {0}
run: |
export PATH=$PATH:/$(echo "$pythonLocation" | sed 's#\\#/#g' | sed 's#:##g')
./demos/demo.sh
- shell: msys2 {0}
run: |
export PATH=$PATH:/$(echo "$pythonLocation" | sed 's#\\#/#g' | sed 's#:##g')
./demos/demo.sh
env:
FORCE_COLOR: true
- shell: msys2 {0}
run: |
export PATH=$PATH:/$(echo "$pythonLocation" | sed 's#\\#/#g' | sed 's#:##g')
./demos/demo.sh
env:
NO_COLOR: true

pacman:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: msys2/setup-msys2@v2
with:
msystem: MINGW64
update: true
- shell: msys2 {0}
run: |
pacman --noconfirm -S python
- shell: msys2 {0}
run: |
./demos/demo.sh
- shell: msys2 {0}
run: |
./demos/demo.sh
env:
FORCE_COLOR: true
- shell: msys2 {0}
run: |
./demos/demo.sh
env:
NO_COLOR: true

pacman_cmd:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: msys2/setup-msys2@v2
with:
msystem: MINGW64
update: true
- shell: msys2 {0}
run: |
pacman --noconfirm -S python
- shell: cmd
run: |
./demos/demo.bat
- shell: cmd
run: |
./demos/demo.bat
env:
FORCE_COLOR: true
- shell: cmd
run: |
./demos/demo.bat
env:
NO_COLOR: true

pacman_ps:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: msys2/setup-msys2@v2
with:
msystem: MINGW64
update: true
- shell: msys2 {0}
run: |
pacman --noconfirm -S python
- shell: powershell
run: |
./demos/demo.bat
- shell: powershell
run: |
./demos/demo.bat
env:
FORCE_COLOR: true
- shell: powershell
run: |
./demos/demo.bat
env:
NO_COLOR: true

pacman_bash:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: msys2/setup-msys2@v2
with:
msystem: MINGW64
update: true
- shell: msys2 {0}
run: |
pacman --noconfirm -S python
- shell: bash
run: |
./demos/demo.sh
- shell: bash
run: |
./demos/demo.sh
env:
FORCE_COLOR: true
- shell: bash
run: |
./demos/demo.sh
env:
NO_COLOR: true

test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ["pypy2", "pypy3", "2.7", "3.5", "3.6", "3.7", "3.8", "3.9", "3.10"]
os: [ubuntu-latest, macos-latest, windows-latest]
include:
# Add new helper variables to existing jobs
- {python-version: "pypy2", toxenv: "pypy"}
- {python-version: "pypy3", toxenv: "pypy3"}
- {python-version: "2.7", toxenv: "py27"}
- {python-version: "3.5", toxenv: "py35"}
- {python-version: "3.6", toxenv: "py36"}
- {python-version: "3.7", toxenv: "py37"}
- {python-version: "3.8", toxenv: "py38"}
- {python-version: "3.9", toxenv: "py39"}
- {python-version: "3.10", toxenv: "py310"}
os:
- ubuntu-latest
- macos-latest
- windows-latest
pyenv:
- { pyver: "pypy2", toxenv: "pypy" }
- { pyver: "pypy3", toxenv: "pypy3" }
- { pyver: "2.7", toxenv: "py27" }
- { pyver: "3.5", toxenv: "py35" }
- { pyver: "3.6", toxenv: "py36" }
- { pyver: "3.7", toxenv: "py37" }
- { pyver: "3.8", toxenv: "py38" }
- { pyver: "3.9", toxenv: "py39" }
- { pyver: "3.10", toxenv: "py310" }

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ matrix.pyenv.pyver }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ matrix.pyenv.pyver }}

- name: Get pip cache dir
id: pip-cache
Expand All @@ -43,14 +267,14 @@ jobs:
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
${{ matrix.os }}-${{ matrix.python-version }}-v1-${{ hashFiles('**/tox.ini') }}
${{ matrix.os }}-${{ matrix.pyenv.pyver }}-v1-${{ hashFiles('**/tox.ini') }}
restore-keys: |
${{ matrix.os }}-${{ matrix.python-version }}-v1-
${{ matrix.os }}-${{ matrix.pyenv.pyver }}-v1-

- name: Install dependencies
run: |
python -m pip install tox

- name: Tox tests
run: |
tox -e ${{ matrix.toxenv }}
tox -e ${{ matrix.pyenv.toxenv }}
14 changes: 10 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,19 @@ init(autoreset=False):

init(strip=None):
Pass ``True`` or ``False`` to override whether ANSI codes should be
stripped from the output. The default behaviour is to strip if on Windows
or if output is redirected (not a tty).
stripped from the output. The default behaviour is to strip if...

- on Windows or if output is redirected (not a tty)
- or if the ``NO_COLOR`` environment variable is set (with any value)
- and ``FORCE_COLOR`` not set (with any value)

init(convert=None):
Pass ``True`` or ``False`` to override whether to convert ANSI codes in the
output into win32 calls. The default behaviour is to convert if on Windows
and output is to a tty (terminal).
output into win32 calls. The default behaviour is to convert if...

- on Windows and output is to a tty (terminal)
- or if the ``FORCE_COLOR`` environment variable is set (with any value)
- and ``NO_COLOR`` is not set (with any value)

init(wrap=True):
On Windows, Colorama works by replacing ``sys.stdout`` and ``sys.stderr``
Expand Down
15 changes: 13 additions & 2 deletions colorama/ansitowin32.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,25 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
# to support the ANSI codes.
conversion_supported = on_windows and winapi_test()

# if the stream is not closed, check if it's a TTY
not_closed = not self.stream.closed

# check if the FORCE_COLOR environment variable has been set and NO_COLOR has not been set.
if 'NO_COLOR' in os.environ:
color_allowed = False
elif 'FORCE_COLOR' in os.environ: # overridden by NO_COLOR
color_allowed = True
else:
color_allowed = not_closed and self.stream.isatty()

# should we strip ANSI sequences from our output?
if strip is None:
strip = conversion_supported or (not self.stream.closed and not self.stream.isatty())
strip = conversion_supported or (not_closed and not color_allowed)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
strip = conversion_supported or (not_closed and not color_allowed)
strip = conversion_supported or not color_allowed

self.strip = strip

# should we should convert ANSI sequences into win32 calls?
if convert is None:
convert = conversion_supported and not self.stream.closed and self.stream.isatty()
convert = conversion_supported and not_closed and color_allowed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
convert = conversion_supported and not_closed and color_allowed
convert = conversion_supported and color_allowed

self.convert = convert

# dict of ansi codes to win32 functions and parameters
Expand Down
2 changes: 2 additions & 0 deletions demos/demo.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
:: Implemented as a bash script which invokes python so that we can test the
:: behaviour on exit, which resets default colors again.

cd /d "%~dp0"

:: print grid of all colors and brightnesses
python demo01.py

Expand Down
2 changes: 2 additions & 0 deletions demos/demo.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# Implemented as a bash script which invokes python so that we can test the
# behaviour on exit, which resets default colors again.

cd $(dirname "$0")

# print grid of all colors and brightnesses
python demo01.py

Expand Down