Skip to main content

🌈 shellcolorize

CI PyPI version License: MIT GitHub stars

shellcolorize is a lightweight Python library for adding color and style to terminal output using ANSI codes. Zero dependencies, no configuration — just import and use.

shellcolorize demo: colors in a terminal, plain text when piped or with NO_COLOR


✨ Features

  • 🎨 Full ANSI palette — 8 standard + 8 bright text colors, same for backgrounds.
  • ✍️ Text styles — bold, dim, italic, underline, blink, reverse, strikethrough.
  • 🛠 colorize() helper — applies styles and resets automatically, no manual RESET needed.
  • 🔇 Smart color detection — plain text when piped to a file, honours NO_COLOR and FORCE_COLOR.
  • 🔌 Global on/off switch — Color.auto() / Color.disable() make every f-string plain text in one call.
  • 🔗 Open Source — MIT License.

📥 Installation

pip install shellcolorize

🛠 Usage

colorize(text, *styles) applies any combination of colors and styles and resets automatically. It also returns plain text when the output is not a TTY or when the NO_COLOR env var is set.

from shellcolorize import Color, colorize

print(colorize("This is red", Color.RED))
print(colorize("Bold and underlined", Color.BOLD, Color.UNDERLINE))
print(colorize("White on blue background", Color.BG_BLUE, Color.WHITE))
print(colorize("Bright green, bold", Color.BRIGHT_GREEN, Color.BOLD))

Using Color attributes directly

For inline use in f-strings. Remember to close with Color.RESET.

from shellcolorize import Color

print(f"{Color.RED}This is red{Color.RESET}")
print(f"{Color.BG_YELLOW}{Color.BLACK}Black on yellow{Color.RESET}")
print(f"{Color.BOLD}{Color.CYAN}Bold cyan{Color.RESET}")

🎨 Available Colors

Standard text colors

Attribute Attribute
Color.BLACK Color.BRIGHT_BLACK
Color.RED Color.BRIGHT_RED
Color.GREEN Color.BRIGHT_GREEN
Color.YELLOW Color.BRIGHT_YELLOW
Color.BLUE Color.BRIGHT_BLUE
Color.MAGENTA Color.BRIGHT_MAGENTA
Color.CYAN Color.BRIGHT_CYAN
Color.WHITE Color.BRIGHT_WHITE

Background colors

Attribute Attribute
Color.BG_BLACK Color.BG_BRIGHT_BLACK
Color.BG_RED Color.BG_BRIGHT_RED
Color.BG_GREEN Color.BG_BRIGHT_GREEN
Color.BG_YELLOW Color.BG_BRIGHT_YELLOW
Color.BG_BLUE Color.BG_BRIGHT_BLUE
Color.BG_MAGENTA Color.BG_BRIGHT_MAGENTA
Color.BG_CYAN Color.BG_BRIGHT_CYAN
Color.BG_WHITE Color.BG_BRIGHT_WHITE

Text styles

Attribute Effect
Color.BOLD Bold
Color.DIM Dimmed
Color.ITALIC Italic
Color.UNDERLINE Underline
Color.BLINK Blinking
Color.REVERSE Swaps fg/bg colors
Color.STRIKETHROUGH Strikethrough
Color.RESET Clears all styles

🔇 Color detection

supports_color() decides whether colors should be used, following common CLI conventions:

Situation Result
NO_COLOR set (any value) — see no-color.org plain text
FORCE_COLOR set (any value except 0) colors
Output is a terminal colors
Output redirected to a file or piped plain text

colorize() applies this check on every call. For f-strings that use Color attributes directly, call Color.auto() once at start-up and every attribute becomes an empty string when colors are not supported:

from shellcolorize import Color

def main():
    Color.auto()          # respects NO_COLOR / FORCE_COLOR / TTY detection
    print(f"{Color.GREEN}✔ done{Color.RESET}")   # plain "✔ done" when piped

Color.disable() and Color.enable() switch colors off and on explicitly (e.g. for a --no-color flag).

python script.py > output.txt      # plain text in the file
NO_COLOR=1 python script.py        # plain text
FORCE_COLOR=1 python script.py | less -R   # keep colors through a pager

🧪 Development

pip install -e ".[dev]"
ruff check .
pytest

📝 License

MIT — see LICENSE.


💬 Feedback

Open an issue or reach out via GitHub.

🌐 Connect

GitHub

Metadata

Release files for shellcolorize 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shellcolorize 1.1.1
File Size Uploaded
shellcolorize-1.1.1.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shellcolorize 1.1.1
File Interpreter ABI Platform
shellcolorize-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / shellcolorize-1.1.1.tar.gz

Download URL shellcolorize-1.1.1.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
619f8fddde2aa8e076395b4fa4e3f8974977b4dd5684a63a75b48c1703fefb5e
BLAKE2b-256 checksum
How to use checksums
bee0609d7c84d17cf0489ed12c08a580079d137945c998c4d7e1ffeb14eb325e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / shellcolorize-1.1.1-py3-none-any.whl

Download URL shellcolorize-1.1.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b17bc849e2102ee855b39862478ac39a10cf601af874491e4afe802ba158d7dc
BLAKE2b-256 checksum
How to use checksums
5fa382d07474aabcb59d122c646343f3d99d76b5093ce9ee14629ed70f3b4b7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page