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:

# 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 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.4.tar.gz (7.9 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.4-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for coloredstrings-0.1.4.tar.gz
Algorithm Hash digest
SHA256 2a556774cad8218348ebf9b121a354c7e90270abf292c0741f8dcb22f74043ce
MD5 0d7c1e797acbac68ce546738bd3d53cd
BLAKE2b-256 991cd9d9ce0082c12cdb90919ec684a3f9de36c4fb376a5610d27eedc24f932e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for coloredstrings-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 34f58a6e2b8df7016042884004555e8e69b4e9d0732196c1d325c2ecdee6952c
MD5 15d5585a062f9d5f010ea8595720ed11
BLAKE2b-256 de4d27bbbc300b9440651b6584717fe14649186e4d9d723517e43c2dd8418a57

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