Add color and style to terminal output using ANSI codes
Project description
🌈 shellcolorize
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 manualRESETneeded. - 🔇 Smart color detection — outputs plain text when piped to a file or when
NO_COLORis set. - 🔗 Open Source — MIT License.
📥 Installation
pip install shellcolorize
🛠 Usage
Using colorize() (recommended)
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 |
|
Color.RESET |
Clears all styles |
🔇 Color detection
colorize() automatically outputs plain text (no ANSI codes) in two situations:
- The output is not a TTY — e.g. redirected to a file or piped to another command.
- The
NO_COLORenvironment variable is set (any value), following the no-color.org convention.
# Plain text — no color codes in the file
python script.py > output.txt
# Plain text — respects NO_COLOR
NO_COLOR=1 python script.py
Direct use of
Colorattributes (f-strings) always emits ANSI codes regardless of environment.
📝 License
MIT — see LICENSE.
💬 Feedback
Open an issue or reach out via GitHub.
🌐 Connect
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shellcolorize-1.0.0.tar.gz.
File metadata
- Download URL: shellcolorize-1.0.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82ac398987dc7b6948acce812fc65d4516f5174b802a6f4b2da55f1c3a527479
|
|
| MD5 |
c5497c259b3b2bc8432780246f43d6ca
|
|
| BLAKE2b-256 |
40eab0c865747b40ba65dda8b82f046a7c9cb93ff98544dd898e7a27c050559f
|
File details
Details for the file shellcolorize-1.0.0-py3-none-any.whl.
File metadata
- Download URL: shellcolorize-1.0.0-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
773c930cde40af02e07d8c3e7c8589d403b2b9cd21a2ff4da39f2b300f20a485
|
|
| MD5 |
4e9ed205570d75eb531a228ae6935d07
|
|
| BLAKE2b-256 |
5fe33772d2ab674f69b36ec614f28b10bf76d999402b552d870d7b307839d025
|