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.
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:
# colorama style (typical)
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.
With coloredstrings the color becomes a readable method on the string itself:
import coloredstrings
coloredstrings.patch()
print("error:".red(), "something went wrong")
coloredstrings.unpatch()
This reads more like natural prose and keeps color usage local to the value being displayed.
Quick start — example usage
To patch globally:
import coloredstrings
# Attach helpers to built-in str
coloredstrings.patch()
print("ok".green()) # green text
print("warn".yellow().bold()) # chained styles (color then bold)
print("bad".red(), "on green".on_green())
# 24-bit RGB:
print("custom".rgb(123, 45, 200))
# 256-color:
print("teal-ish".color256(37))
# When you're done (optional) remove the patched methods:
coloredstrings.unpatch()
To patch locally (inside a function or a context):
import coloredstrings
def perror(message: str):
with coloredstrings.patched():
print(message.red())
@coloredstrings.patched
def log_info(message: str):
colored = "INFO".blue()
print(f"[{colored}]: {message}")
log_info("Downloaded image.")
perror("file not found!")
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() - 24-bit color:
rgb(r, g, b) - 256-color:
color256(idx)
- Foreground colors:
Installation
pip install git+https://github.com/samedit66/coloredstrings.git
I'll soon upload it to PyPi🙃.
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-0.1.0.tar.gz.
File metadata
- Download URL: coloredstrings-0.1.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d692bd0fdd2576a2b9be7ed8103ebc97c2ab09ae6538f391d0547e41740266f2
|
|
| MD5 |
97b691aaf66721b3895098de3104a8f7
|
|
| BLAKE2b-256 |
45f9c946c9f3df9638945f21a9f3890c782263d8a3b3d052c0299a6281278a49
|
File details
Details for the file coloredstrings-0.1.0-py3-none-any.whl.
File metadata
- Download URL: coloredstrings-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8cc4d353c6e482531be0a96737ed65bd0304bcc9410d675aa52b7de335f3909
|
|
| MD5 |
211eacdec7dac9802f252bb6a7bb029a
|
|
| BLAKE2b-256 |
e0b97e50f2d3c50fb9e5170a1261a38791b77f72beeba1b28245872a8a6d0906
|