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.


✨ 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

Release files for shellcolorize 1.1.0

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.0
File Size Uploaded
shellcolorize-1.1.0.tar.gz 6.5 kB Details

Built distribution (wheel)

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

Total release size: 12.7 kB

Release files / shellcolorize-1.1.0.tar.gz

Download URL shellcolorize-1.1.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4a919245816054f5e4fc91cfa2d89da115c3dace8fcad57ee74c3fdc5c3886ed
BLAKE2b-256 checksum
How to use checksums
7a0e6e13eadea7feedc0f650c487c78eae2a75caf78191c208f22cb58c384ddc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

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

Download URL shellcolorize-1.1.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9fbe0010b5739a04a3c34082acf0b87f342b5da3f0f7ef54c535cba1983832db
BLAKE2b-256 checksum
How to use checksums
4f871829bd42a481eed38de270c56048d48d453ab588e69a6662b344d2ad1838
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.3

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