Colorclass

2.2.2 · active · verified Fri Apr 10

Colorclass is a Python library for creating colorful and worry-free console applications, providing ANSI color text support for Linux, Mac OS X, and Windows. It includes 'auto colors' functionality to adapt to dark/light terminals. The current stable version is 2.2.2, released in December 2021, and it is actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `Color` class to apply colors and styles using curly-bracket tags. It also shows how to enable Windows console support and list all available tags. The `Windows.enable()` call is conditionally applied to avoid issues in CI/CD or non-interactive environments.

from colorclass import Color, Windows, list_tags
import os

# Enable Windows console support (required for colors on Windows)
# Check if running on CI/CD or in a non-interactive environment
if os.name == 'nt' and os.environ.get('CI') != 'true' and os.isatty(1):
    Windows.enable()

# Print a simple colored string
print(Color('{red}This text is red.{/red}'))

# Combine multiple colors and styles
print(Color('{autoblue}{bgwhite}Blue text on white background.{/bgwhite}{/autoblue}'))

# Use auto colors for terminals
print(Color('{autoyellow}This text adapts to terminal background.{/autoyellow}'))

# List available tags
print('\nAvailable tags:')
for tag in sorted(list_tags()):
    print(f'- {tag}')

view raw JSON →