coloredstrings


Namecoloredstrings JSON
Version 1.0.1 PyPI version JSON
download
home_pageNone
SummaryColorize Different.
upload_time2025-09-02 21:47:39
maintainersamedit66
docs_urlNone
authorsamedit66
requires_python>=3.9
licenseNone
keywords ansi color colour crossplatform terminal text
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # coloredstrings

[![Python package](https://github.com/samedit66/coloredstrings/actions/workflows/python-package.yml/badge.svg)](https://github.com/samedit66/coloredstrings/actions/workflows/python-package.yml)
[![PyPI Downloads](https://static.pepy.tech/personalized-badge/coloredstrings?period=total&units=ABBREVIATION&left_color=BLACK&right_color=MAGENTA&left_text=downloads)](https://pepy.tech/projects/coloredstrings)
[![PyPI version](https://img.shields.io/pypi/v/coloredstrings.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/coloredstrings)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/coloredstrings.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/coloredstrings)
[![Licence](https://img.shields.io/github/license/samedit66/coloredstrings.svg)](COPYING.txt)
[![Code style: Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/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](https://crates.io/crates/text-colorizer) crate — ergonomic, expressive, and surprisingly pleasant to type.

---

## Installation

Stable:
```bash
pip install coloredstrings
```

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

---

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

```python
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:
```python
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:

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

```python
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()`, `on_rgb(r, g, b)`
    - 24-bit color: `rgb(r, g, b)`
    - 256-color: `color256(idx)`

---

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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "coloredstrings",
    "maintainer": "samedit66",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "samedit66 <samedit66@yandex.ru>",
    "keywords": "ansi, color, colour, crossplatform, terminal, text",
    "author": "samedit66",
    "author_email": "samedit66 <samedit66@yandex.ru>",
    "download_url": "https://files.pythonhosted.org/packages/9e/ef/3a6701ecbbc79afc72427ddf444fbad0883b361adb735bec6dad8afd599a/coloredstrings-1.0.1.tar.gz",
    "platform": null,
    "description": "# coloredstrings\n\n[![Python package](https://github.com/samedit66/coloredstrings/actions/workflows/python-package.yml/badge.svg)](https://github.com/samedit66/coloredstrings/actions/workflows/python-package.yml)\n[![PyPI Downloads](https://static.pepy.tech/personalized-badge/coloredstrings?period=total&units=ABBREVIATION&left_color=BLACK&right_color=MAGENTA&left_text=downloads)](https://pepy.tech/projects/coloredstrings)\n[![PyPI version](https://img.shields.io/pypi/v/coloredstrings.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/coloredstrings)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/coloredstrings.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/coloredstrings)\n[![Licence](https://img.shields.io/github/license/samedit66/coloredstrings.svg)](COPYING.txt)\n[![Code style: Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n\n**Colorize Different**\n\nA 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](https://crates.io/crates/text-colorizer) crate \u2014 ergonomic, expressive, and surprisingly pleasant to type.\n\n---\n\n## Installation\n\nStable:\n```bash\npip install coloredstrings\n```\n\nLatest:\n```bash\npip install git+https://github.com/samedit66/coloredstrings.git\n```\n\n---\n\n## Why use this? \u2014 Isn't patching `str` un-Pythonic?\n\nPatching 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:\n\n```python\nfrom colorama import Fore, Style\n\nprint(Fore.RED + \"error: \" + Style.RESET_ALL + \"something went wrong\")\n```\n\nThat works fine, but it forces you to manage constants and remember to reset, and your code quickly becomes noisy with `+` and `RESET` tokens.\n\nAnother example using the `termcolor` package:\n```python\nfrom termcolor import colored\n\nprint(colored(\"error:\", \"red\"), \"something went wrong\")\n```\n\n`termcolor` offers a nice function `colored` with a bunch of arguments, but personally, I still find it lacking.\n\nWith `coloredstrings` the color becomes a readable method on the string itself:\n\n```python\nimport coloredstrings\n\n# `patched()` patches `str` to have awesome `red()` method\nwith coloredstrings.patched():\n    print(\"error:\".red(), \"something went wrong\")\n```\n\nThis reads more like natural prose and keeps color usage local to the value being displayed.\n\n---\n\n## Quick start \u2014 example usage\n\n```python\nimport coloredstrings\n\n# Patched `str` methods are available only within the context\ndef warn(msg: str) -> None:\n    with coloredstrings.patched():\n        print(\"warning:\".yellow().bold(), msg)\n\n# Same idea, but using a decorator\n@coloredstrings.patched\ndef info(msg: str) -> None:\n    print(\"[info]:\".blue(), msg)\n\n# If you're brave enough and really want it, you can patch `str` globally\ncoloredstrings.patch()\n\nprint(\"ok\".green())\nprint(\"warn\".yellow().bold())\nprint(\"bad\".red(), \"on green\".on_green())\n\n# 24-bit RGB:\nprint(\"custom\".rgb(123, 45, 200))\n\n# 256-color:\nprint(\"teal-ish\".color256(37))\n\n# And don't forget to unpatch it afterwards\ncoloredstrings.unpatch()\n```\n\n---\n\n## API (high level)\n\n- `patch()` \u2014 attach methods to `str`\n- `unpatch()` \u2014 remove the attached methods\n- `patched()` - automatically calls `patch()` and `unpatch()` in a given context\n\n- Color/style methods attached to str (call on any string):\n    - Foreground colors: `red()`, `green()`, `yellow()`, `blue()`, `magenta()`, `cyan()`, `white()`, `black()`, `bright_red()`\n    - Styles: `bold()`, `dim()`, `italic()`, `underline()`, `inverse()`\n    - Background helpers: `on_red()`, `on_green()`, `on_rgb(r, g, b)`\n    - 24-bit color: `rgb(r, g, b)`\n    - 256-color: `color256(idx)`\n\n---\n\n## Limitations\n\nUnder the hood `coloredstrings` uses `forbiddenfruit` package, as a result it also has the same limitations:\n\n> Forbbiden Fruit is tested on CPython 3.7-3.13.\n> Since Forbidden Fruit is fundamentally dependent on the C API, this library won't work on other python implementations, such as Jython, pypy, etc.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Colorize Different.",
    "version": "1.0.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/samedit66/coloredstrings/issues",
        "Changelog": "https://github.com/samedit66/coloredstrings/releases",
        "Documentation": "https://github.com/samedit66/coloredstrings#readme",
        "Homepage": "https://github.com/samedit66/coloredstrings",
        "Repository": "https://github.com/samedit66/coloredstrings"
    },
    "split_keywords": [
        "ansi",
        " color",
        " colour",
        " crossplatform",
        " terminal",
        " text"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ec71c4d9d370b2ab4edbba15012fcd99ae867ec74f332ac5204b093f4145589e",
                "md5": "e836c7b05e1e9c51ef0c970cdf68b30f",
                "sha256": "4f4c16ffb68d92ebb32c88eec57fb4888092f67aba592c52fb2dd6f19fa02190"
            },
            "downloads": -1,
            "filename": "coloredstrings-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e836c7b05e1e9c51ef0c970cdf68b30f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 9588,
            "upload_time": "2025-09-02T21:47:36",
            "upload_time_iso_8601": "2025-09-02T21:47:36.220279Z",
            "url": "https://files.pythonhosted.org/packages/ec/71/c4d9d370b2ab4edbba15012fcd99ae867ec74f332ac5204b093f4145589e/coloredstrings-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9eef3a6701ecbbc79afc72427ddf444fbad0883b361adb735bec6dad8afd599a",
                "md5": "57811f6dea21b89dc0fa5314bdcfa01d",
                "sha256": "4624f05c96fb2a544ef7e0b3c57fede7a7fdb860e935c1eb0002e24d09cb841c"
            },
            "downloads": -1,
            "filename": "coloredstrings-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "57811f6dea21b89dc0fa5314bdcfa01d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 8279,
            "upload_time": "2025-09-02T21:47:39",
            "upload_time_iso_8601": "2025-09-02T21:47:39.317459Z",
            "url": "https://files.pythonhosted.org/packages/9e/ef/3a6701ecbbc79afc72427ddf444fbad0883b361adb735bec6dad8afd599a/coloredstrings-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-02 21:47:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "samedit66",
    "github_project": "coloredstrings",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "coloredstrings"
}
        
Elapsed time: 0.91200s