Colorize Different.
Project description
coloredstrings
Colorize Different
A tiny utility that patches Python's built-in str with convenient ANSI color / style helpers so you can write "hello".red() instead of juggling escape sequences or long constant concatenations. Inspired by the Rust text-colorizer crate — ergonomic, expressive, and surprisingly pleasant to type.
Installation
Stable:
pip install coloredstrings
Latest:
pip install git+https://github.com/samedit66/coloredstrings.git
Why use this? — Isn't patching str un-Pythonic?
Patching builtins is a controversial choice, and at first glance it may look un-Pythonic. Libraries like colorama require you to import constants and build strings by concatenation:
from colorama import Fore, Style
print(Fore.RED + "error: " + Style.RESET_ALL + "something went wrong")
That works fine, but it forces you to manage constants and remember to reset, and your code quickly becomes noisy with + and RESET tokens.
Another example using the termcolor package:
from termcolor import colored
print(colored("error:", "red"), "something went wrong")
termcolor offers a nice function colored with a bunch of arguments, but personally, I still find it lacking.
With coloredstrings the color becomes a readable method on the string itself:
import coloredstrings
# `patched()` patches `str` to have awesome `red()` method
with coloredstrings.patched():
print("error:".red(), "something went wrong")
This reads more like natural prose and keeps color usage local to the value being displayed.
Quick start — example usage
import coloredstrings
# Patched `str` methods are available only within the context
def warn(msg: str) -> None:
with coloredstrings.patched():
print("warning:".yellow().bold(), msg)
# Same idea, but using a decorator
@coloredstrings.patched
def info(msg: str) -> None:
print("[info]:".blue(), msg)
# If you're brave enough and really want it, you can patch `str` globally
coloredstrings.patch()
print("ok".green())
print("warn".yellow().bold())
print("bad".red(), "on green".on_green())
# 24-bit RGB:
print("custom".rgb(123, 45, 200))
# 256-color:
print("teal-ish".color256(37))
# And don't forget to unpatch it afterwards
coloredstrings.unpatch()
API (high level)
-
patch()— attach methods tostr -
unpatch()— remove the attached methods -
patched()- automatically callspatch()andunpatch()in a given context -
Color/style methods attached to str (call on any string):
- Foreground colors:
red(),green(),yellow(),blue(),magenta(),cyan(),white(),black(),bright_red() - Styles:
bold(),dim(),italic(),underline(),inverse() - Background helpers:
on_red(),on_green(),on_rgb(r, g, b) - 24-bit color:
rgb(r, g, b) - 256-color:
color256(idx)
- Foreground colors:
Limitations
Under the hood coloredstrings uses forbiddenfruit package, as a result it also has the same limitations:
Forbbiden Fruit is tested on CPython 3.7-3.13. Since Forbidden Fruit is fundamentally dependent on the C API, this library won't work on other python implementations, such as Jython, pypy, etc.
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 coloredstrings-1.0.0.tar.gz.
File metadata
- Download URL: coloredstrings-1.0.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41795690b4d98c57bf54fb4fd0ad50dc240c5c6bca5cd3bedec58dd6106b43dc
|
|
| MD5 |
2c483dca12b48a44e679efc2a4741356
|
|
| BLAKE2b-256 |
3a2fc123393790d10c503d9e2ab252d4dd3b191bd469d3c9500a616b6a33b4fe
|
File details
Details for the file coloredstrings-1.0.0-py3-none-any.whl.
File metadata
- Download URL: coloredstrings-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eead381752f50950efedf3e8dc684d8230f2edc9fe0fe615a80b2e543148441
|
|
| MD5 |
b73251f8907301d338268175333f6484
|
|
| BLAKE2b-256 |
b7ade7c8dc913b184f79cb0040af8e4b62d08be055d81f08752dbf352768d01c
|