Skip to main content

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

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


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.0.tar.gz (3.3 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.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for coloredstrings-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d692bd0fdd2576a2b9be7ed8103ebc97c2ab09ae6538f391d0547e41740266f2
MD5 97b691aaf66721b3895098de3104a8f7
BLAKE2b-256 45f9c946c9f3df9638945f21a9f3890c782263d8a3b3d052c0299a6281278a49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for coloredstrings-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c8cc4d353c6e482531be0a96737ed65bd0304bcc9410d675aa52b7de335f3909
MD5 211eacdec7dac9802f252bb6a7bb029a
BLAKE2b-256 e0b97e50f2d3c50fb9e5170a1261a38791b77f72beeba1b28245872a8a6d0906

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