diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0c893394..1e4f1263 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -43,9 +267,9 @@ 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: | @@ -53,4 +277,4 @@ jobs: - name: Tox tests run: | - tox -e ${{ matrix.toxenv }} + tox -e ${{ matrix.pyenv.toxenv }} diff --git a/README.rst b/README.rst index 9a349ac1..4d229e59 100644 --- a/README.rst +++ b/README.rst @@ -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`` diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index 6039a054..27578529 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -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) 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 self.convert = convert # dict of ansi codes to win32 functions and parameters diff --git a/demos/demo.bat b/demos/demo.bat index 7955234f..3ffb60ed 100644 --- a/demos/demo.bat +++ b/demos/demo.bat @@ -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 diff --git a/demos/demo.sh b/demos/demo.sh old mode 100644 new mode 100755 index d8cbbcca..5e0132bb --- a/demos/demo.sh +++ b/demos/demo.sh @@ -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