Ever looked at your Python script’s output and thought, “Meh, it could be brighter… more alive?” You’re not alone! While raw ANSI escape codes give us superpowers to color our terminal, they can get a bit clunky to manage.
Why colorist?
Imagine you want to print a red message. With raw ANSI, you’d write “\033[31mThis is red!\033[0m”. Not terrible, but what if you forget that \033[0m? Or you want to combine bold and yellow? The strings start looking like hieroglyphics!
Colorist cleans up this mess by providing simple functions and objects that represent colors and styles. It’s like having a dedicated artist for your terminal, ready to paint your text exactly how you want it, without you having to remember every single ANSI code.
Getting Started: Installation
First things first, let’s get colorist installed. In a terminal window type
pip install colorist
1. Basic Colors:
The simplest way to use colorist is by importing Color and then calling its print() method or using it in an f-string.
from colorist import Color
print(f"Hello, {Color.BLUE}Alice{Color.OFF}! Welcome!")
Notice Color.OFF? That’s colorist’s elegant way of saying “reset all styling,” ensuring your next print statement starts fresh.
2. Background Colors:
Want to highlight some text? colorist has you covered for background colors too!
from colorist import Color, BgColor
# Yellow text on a blue background
print(f"{BgColor.BLUE}{Color.YELLOW}Warning: Proceed with caution!{Color.OFF}")
# Green text on a black background
print(f"{BgColor.BLACK}{Color.GREEN}Success! Operation completed.{Color.OFF}")
3. Text Effects:
Beyond just colors, colorist lets you apply styles like bold, italic, and underline using the Effect enum.
from colorist import Color, Effect
# Yellow text and bold effect
print(f"{Effect.BOLD}{Color.YELLOW}Proceed with caution!{Color.OFF}{Effect.OFF}")
# Green text and underline effect
print(f"{Effect.UNDERLINE}{Color.GREEN}Operation completed.{Color.OFF}{Effect.OFF}")
