Skip to content
Open
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
29 changes: 16 additions & 13 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
name: lint_python
on: [pull_request, push]
name: lint
on: [pull_request, push, workflow_dispatch]
jobs:
pre-commit:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- uses: tox-dev/action-pre-commit-uv@v1

lint_python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install --upgrade pip wheel
- run: pip install bandit black codespell flake8 flake8-bugbear
flake8-comprehensions isort mypy pytest pyupgrade safety
- run: bandit --recursive --skip B311 .
- run: pip install --upgrade pip
- run: pip install black codespell flake8 flake8-bugbear mypy
- run: black --check . || true
- run: codespell # --ignore-words-list="" --skip="*.css,*.js,*.lock"
- run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- run: flake8 . --count --exit-zero --max-complexity=10 --max-line-length=88
--show-source --statistics
- run: isort --check-only --profile black . || true
- run: pip install -r requirements.txt
- run: mkdir --parents --verbose .mypy_cache
- run: mypy --ignore-missing-imports --install-types --non-interactive . || true
- run: pytest .
- run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true
- run: safety check
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.3
hooks:
- id: ruff-check
args: [--exit-non-zero-on-fix]
4 changes: 2 additions & 2 deletions colorama/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
from .initialise import init, deinit, reinit, colorama_text, just_fix_windows_console
from .ansi import Fore, Back, Style, Cursor
from .ansi import Back, Cursor, Fore, Style
from .ansitowin32 import AnsiToWin32
from .initialise import colorama_text, deinit, init, just_fix_windows_console, reinit

__version__ = '0.4.7dev1'

13 changes: 6 additions & 7 deletions colorama/ansitowin32.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
import os
import re
import sys
import os

from .ansi import AnsiFore, AnsiBack, AnsiStyle, Style, BEL
from .winterm import enable_vt_processing, WinTerm, WinColor, WinStyle
from .win32 import windll, winapi_test

from .ansi import BEL, AnsiBack, AnsiFore, AnsiStyle, Style
from .win32 import winapi_test, windll
from .winterm import WinColor, WinStyle, WinTerm, enable_vt_processing

winterm = None
if windll is not None:
Expand Down Expand Up @@ -170,7 +169,7 @@ def get_win32_calls(self):
AnsiBack.LIGHTCYAN_EX: (winterm.back, WinColor.CYAN, True),
AnsiBack.LIGHTWHITE_EX: (winterm.back, WinColor.GREY, True),
}
return dict()
return {}

def write(self, text):
if self.strip or self.convert:
Expand Down Expand Up @@ -242,7 +241,7 @@ def call_win32(self, command, params):
func_args = self.win32_calls[param]
func = func_args[0]
args = func_args[1:]
kwargs = dict(on_stderr=self.on_stderr)
kwargs = {'on_stderr': self.on_stderr}
func(*args, **kwargs)
elif command in 'J':
winterm.erase_screen(params[0], on_stderr=self.on_stderr)
Expand Down
2 changes: 1 addition & 1 deletion colorama/tests/ansitowin32_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
from contextlib import ExitStack
from io import StringIO, TextIOWrapper
from unittest import TestCase, main
from unittest.mock import MagicMock, Mock, patch
from contextlib import ExitStack

from ..ansitowin32 import AnsiToWin32, StreamWrapper
from ..win32 import ENABLE_VIRTUAL_TERMINAL_PROCESSING
Expand Down
4 changes: 2 additions & 2 deletions colorama/tests/initialise_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
import sys
from unittest import TestCase, main, skipUnless
from unittest.mock import patch, Mock
from unittest.mock import Mock, patch

from ..ansitowin32 import StreamWrapper
from ..initialise import init, just_fix_windows_console, _wipe_internal_state_for_tests
from ..initialise import _wipe_internal_state_for_tests, init, just_fix_windows_console
from .utils import osname, replace_by

orig_stdout = sys.stdout
Expand Down
4 changes: 2 additions & 2 deletions colorama/tests/isatty_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sys
from unittest import TestCase, main

from ..ansitowin32 import StreamWrapper, AnsiToWin32
from .utils import pycharm, replace_by, replace_original_by, StreamTTY, StreamNonTTY
from ..ansitowin32 import AnsiToWin32, StreamWrapper
from .utils import StreamNonTTY, StreamTTY, pycharm, replace_by, replace_original_by


def is_a_tty(stream):
Expand Down
4 changes: 2 additions & 2 deletions colorama/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
import os
import sys
from contextlib import contextmanager
from io import StringIO
import sys
import os


class StreamTTY(StringIO):
Expand Down
14 changes: 7 additions & 7 deletions colorama/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
SetConsoleTextAttribute = lambda *_: None
winapi_test = lambda *_: None
else:
from ctypes import byref, Structure, c_char, POINTER
from ctypes import POINTER, Structure, byref, c_char

COORD = wintypes._COORD

Expand All @@ -30,12 +30,12 @@ class CONSOLE_SCREEN_BUFFER_INFO(Structure):
("dwMaximumWindowSize", COORD),
]
def __str__(self):
return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % (
self.dwSize.Y, self.dwSize.X
, self.dwCursorPosition.Y, self.dwCursorPosition.X
, self.wAttributes
, self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, self.srWindow.Right
, self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X
return (
f"({self.dwSize.Y},{self.dwSize.X},"
f"{self.dwCursorPosition.Y},{self.dwCursorPosition.X},"
f"{self.wAttributes},"
f"{self.srWindow.Top},{self.srWindow.Left},{self.srWindow.Bottom},{self.srWindow.Right},"
f"{self.dwMaximumWindowSize.Y},{self.dwMaximumWindowSize.X})"
)

_GetStdHandle = windll.kernel32.GetStdHandle
Expand Down
1 change: 1 addition & 0 deletions colorama/winterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def get_osfhandle(_):

from . import win32


# from wincon.h
class WinColor:
BLACK = 0
Expand Down
9 changes: 5 additions & 4 deletions demos/demo01.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# Add parent dir to sys path, so the following 'import colorama' always finds
# the local source in preference to any installed version of colorama.
import fixpath
from colorama import just_fix_windows_console, Fore, Back, Style

from colorama import Back, Fore, Style, just_fix_windows_console

just_fix_windows_console()

Expand All @@ -29,18 +30,18 @@
# show the color names
sys.stdout.write(' ')
for foreground in FORES:
sys.stdout.write('%s%-7s' % (foreground, NAMES[foreground]))
sys.stdout.write(f'{foreground}{NAMES[foreground]:<7}')
print()

# make a row for each background color
for background in BACKS:
sys.stdout.write('%s%-7s%s %s' % (background, NAMES[background], Back.RESET, background))
sys.stdout.write(f'{background}{NAMES[background]:<7}{Back.RESET} {background}')
# make a column for each foreground color
for foreground in FORES:
sys.stdout.write(foreground)
# show dim, normal bright
for brightness in STYLES:
sys.stdout.write('%sX ' % brightness)
sys.stdout.write(f'{brightness}X ')
sys.stdout.write(Style.RESET_ALL + ' ' + background)
print(Style.RESET_ALL)

Expand Down
3 changes: 2 additions & 1 deletion demos/demo02.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# Simple demo of changing foreground, background and brightness.

import fixpath
from colorama import just_fix_windows_console, Fore, Back, Style

from colorama import Back, Fore, Style, just_fix_windows_console

just_fix_windows_console()

Expand Down
3 changes: 2 additions & 1 deletion demos/demo03.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# Demonstrate the different behavior when autoreset is True and False.

import fixpath
from colorama import init, Fore, Back, Style

from colorama import Back, Fore, Style, init

init(autoreset=True)
print(Fore.CYAN + Back.MAGENTA + Style.BRIGHT + 'Line 1: colored, with autoreset=True')
Expand Down
4 changes: 3 additions & 1 deletion demos/demo04.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

# check that stripped ANSI in redirected stderr does not affect stdout
import sys

import fixpath
from colorama import init, Fore

from colorama import Fore, init

init()
print(Fore.GREEN + 'GREEN set on stdout. ', end='')
Expand Down
14 changes: 8 additions & 6 deletions demos/demo05.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
# The unwrapped cases will be interpreted with ANSI on Unix, but not on Windows.

import sys

import fixpath
from colorama import AnsiToWin32, init, Fore

from colorama import AnsiToWin32, Fore, init

init()
print('%sWrapped yellow going to stdout, via the default print function.' % Fore.YELLOW)
print(f'{Fore.YELLOW}Wrapped yellow going to stdout, via the default print function.')

init(wrap=False)
print('%sUnwrapped CYAN going to stdout, via the default print function.' % Fore.CYAN)
print('%sUnwrapped CYAN, using the file parameter to write via colorama the AnsiToWin32 function.' % Fore.CYAN, file=AnsiToWin32(sys.stdout))
print('%sUnwrapped RED going to stdout, via the default print function.' % Fore.RED)
print(f'{Fore.CYAN}Unwrapped CYAN going to stdout, via the default print function.')
print(f'{Fore.CYAN}Unwrapped CYAN, using the file parameter to write via colorama the AnsiToWin32 function.', file=AnsiToWin32(sys.stdout))
print(f'{Fore.RED}Unwrapped RED going to stdout, via the default print function.')

init()
print('%sWrapped RED going to stdout, via the default print function.' % Fore.RED)
print(f'{Fore.RED}Wrapped RED going to stdout, via the default print function.')
20 changes: 11 additions & 9 deletions demos/demo06.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
from random import choice, randint
from string import printable

import fixpath

import colorama
from colorama import Fore, Back, Style, Cursor
from random import randint, choice
from string import printable
from colorama import Back, Cursor, Fore, Style

# Demonstrate printing colored, random characters at random positions on the screen

Expand All @@ -27,15 +29,15 @@ def main():
pos = lambda y, x: Cursor.POS(x, y)
# draw a white border.
print(Back.WHITE, end='')
print('%s%s' % (pos(MINY, MINX), ' '*MAXX), end='')
print('{}{}'.format(pos(MINY, MINX), ' '*MAXX), end='')
for y in range(MINY, 1+MAXY):
print('%s %s ' % (pos(y, MINX), pos(y, MAXX)), end='')
print('%s%s' % (pos(MAXY, MINX), ' '*MAXX), end='')
print(f'{pos(y, MINX)} {pos(y, MAXX)} ', end='')
print('{}{}'.format(pos(MAXY, MINX), ' '*MAXX), end='')
# draw some blinky lights for a while.
for i in range(PASSES):
print('%s%s%s%s%s' % (pos(randint(1+MINY,MAXY-1), randint(1+MINX,MAXX-1)), choice(FORES), choice(BACKS), choice(STYLES), choice(CHARS)), end='')
for _ in range(PASSES):
print(f'{pos(randint(1+MINY,MAXY-1), randint(1+MINX,MAXX-1))}{choice(FORES)}{choice(BACKS)}{choice(STYLES)}{choice(CHARS)}', end='')
# put cursor to top, left, and set color to white-on-black with normal brightness.
print('%s%s%s%s' % (pos(MINY, MINX), Fore.WHITE, Back.BLACK, Style.NORMAL), end='')
print(f'{pos(MINY, MINX)}{Fore.WHITE}{Back.BLACK}{Style.NORMAL}', end='')

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions demos/demo07.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fixpath

import colorama

# Demonstrate cursor relative movement: UP, DOWN, FORWARD, and BACK in colorama.CURSOR
Expand Down
3 changes: 2 additions & 1 deletion demos/demo08.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fixpath
from colorama import colorama_text, Fore

from colorama import Fore, colorama_text


def main():
Expand Down
6 changes: 4 additions & 2 deletions demos/demo09.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# https://www.youtube.com/watch?v=F5a8RLY2N8M&list=PL1_riyn9sOjcKIAYzo7f8drxD-Yg9La-D&index=61
# Generic colorama demo using command line arguments
# By George Ogden
from colorama import Fore, Back, Style, init
import argparse

from colorama import Back, Fore, Style, init

parser = argparse.ArgumentParser("colorama demo")

def format(module):
return list(map(lambda x: x.lower(),module.__dict__.keys()))
return [x.lower() for x in module.__dict__.keys()]

def find(module,item):
return module.__dict__[item.upper()]
Expand Down
3 changes: 2 additions & 1 deletion demos/fixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Add demo dir's parent to sys path, so that 'import colorama' always finds
# the local source in preference to any installed version of colorama.
import sys
from os.path import normpath, dirname, join
from os.path import dirname, join, normpath

local_colorama_module = normpath(join(dirname(__file__), '..'))
sys.path.insert(0, local_colorama_module)
18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,21 @@ include = [
include = [
"/colorama/*",
]

[tool.ruff]
fix = true

lint.select = [
"C4", # flake8-comprehensions
"C90", # McCabe complexity
"E9", # pycodestyle errors
"F63", # Pyflakes
"F7", # Pyflakes
"F82", # Pyflakes
"I", # isort
"S", # flake8-bandit
"UP", # pyupgrade
]
lint.ignore = [
"S311", # suspicious-non-cryptographic-random-usage
]
Loading