Skip to main content

Colorize Different.

Project description

coloredstrings

Python package PyPI Downloads PyPI version Supported Python versions Licence Code style: Ruff

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

Quick start

Run a demo:

python -m coloredstrings

Test in code:

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

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.


API

Patch str

  • patch() — attach methods to str
  • unpatch() — remove the attached methods
  • patched() - automatically calls patch() and unpatch() in a given context

Foreground colors (basic & bright)

  • black() (aliases: grey() and gray())
  • red()
  • green()
  • yellow()
  • blue()
  • magenta()
  • cyan()
  • white()
  • bright_black()
  • bright_red()
  • bright_green()
  • bright_yellow()
  • bright_blue()
  • bright_magenta()
  • bright_cyan()
  • bright_white()

Background helpers

  • on_black() (aliases: on_grey() and on_gray())
  • on_red()
  • on_green()
  • on_yellow()
  • on_blue()
  • on_magenta()
  • on_cyan()
  • on_white()
  • on_bright_black() (aliases: on_bright_grey() and on_bright_gray())
  • on_bright_red()
  • on_bright_green()
  • on_bright_yellow()
  • on_bright_blue()
  • on_bright_magenta()
  • on_bright_cyan()
  • on_bright_white()

24-bit / 256-color helpers

  • rgb(r, g, b) -- 24-bit truecolor foreground (clamped to 0..255)
  • color256(idx) -- 256-color foreground (clamped to 0..255)
  • on_rgb(r, g, b) -- set a 24-bit background color (clamped to 0..255)

Text attributes / styles

  • bold()
  • dim() (aliases: faint() and dark())
  • italic()
  • underline()
  • blink()
  • inverse() (alias: reverse)
  • hidden() (aliases: concealed, password)
  • strike()

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-1.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

coloredstrings-1.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for coloredstrings-1.1.0.tar.gz
Algorithm Hash digest
SHA256 1f7eff4e222777b933632821aac4c5a2249e1dde17682f28029ead7466de1f1b
MD5 a181d9065cea76d71deb74117bd12ba0
BLAKE2b-256 149541829becc080e8591d7e0dec35fd20a6868d74a4f66a210c0266364e59db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for coloredstrings-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87556df73e69cf46140e078502c0b8f2bb7cc27e59e0055524773c0cc672aaf9
MD5 8cea1f67a82ed0285844c4f2b0c69130
BLAKE2b-256 feb1afd5cadfc288e67fe6c48ca75f2be344d5748584a3b1e37d1f3802ba7ca7

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