Skip to main content

Colorize Different.

Project description

coloredstrings Python package PyPI Downloads

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:

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 cprint

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 to str

  • unpatch() — remove the attached methods

  • patched() - automatically calls patch() and unpatch() 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)

Installation

Stable:

pip install coloredstrings

Latest:

pip install git+https://github.com/samedit66/coloredstrings.git

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coloredstrings-0.1.5.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coloredstrings-0.1.5-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file coloredstrings-0.1.5.tar.gz.

File metadata

  • Download URL: coloredstrings-0.1.5.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for coloredstrings-0.1.5.tar.gz
Algorithm Hash digest
SHA256 566fe2cad42fb656a595e92d4a719b827f1bfc005d4da30e67ba676e68ad8f08
MD5 70b3e2b19b9c2abe5a909b3691c7cf1b
BLAKE2b-256 bebb6e3c345d79eafd6ec89ea64bb3905fbb888f8949d88bc0cc7479423cc98a

See more details on using hashes here.

File details

Details for the file coloredstrings-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for coloredstrings-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6870192683fd9fe71a202f2f7940ad0feab0100b9fe9e69b1b7fdb903090aabf
MD5 4376abf4cbe65ab8c8c6c058b25e216f
BLAKE2b-256 d4f36875346140a203f0eca5f0c2e51384076a883d59334d4befd5425bf8ae7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page