Skip to main content

Colorize text and data structures in the terminal with ANSI escape codes and themed decorators.

Project description

ColorDoll: Nested ANSI Colorization for Python

PyPI version

ColorDoll is a Python library that provides flexible and powerful ANSI colorization, including nested colorization and theming for complex data structures like dictionaries, lists, and JSON strings.

And, it's fairly Quick.

🚀 Performance Benchmarks ⏱️

(amd 3800, 3200mhz ram, single XPG-8200 nvme, win11)

Function Runs Min Time (sec) Max Time (sec) Avg Time (sec) As milliseconds Runs / second
colorize 10,000 0.000006 0.000006 0.000006 0.006 ~165000
theme_colorize 10,000 0.000059 0.000060 0.000059 0.059 ~16950
Themed Decorator 10,000 0.000023 0.000023 0.000023 0.023 ~43450

Features

  • Nested Colorization: Handles nested ANSI color codes gracefully, ensuring correct color rendering even with complex formatting.
  • Theming: Supports predefined and custom themes for consistent colorization across your output.
  • Data Structure Coloring: Colorizes dictionaries, lists, and JSON strings recursively, highlighting keys, values, and different data types.
  • Decorator Support: Provides decorators for easily colorizing function outputs and applying themes.
  • Customizable Configurations: Allows loading color configurations from JSON files or dictionaries.

Installation

pip install colordoll

Usage

Basic Colorization

from colordoll import default_colorizer, red, blue, green

# Using color functions
print(red("This is red text."))
print(blue("This is blue text."))

# Using the colorize method with foreground and background colors
print(default_colorizer.colorize("Yellow text on a blue background", "yellow", "blue"))

# Handling nested colors correctly
print(default_colorizer.colorize(f"This is {default_colorizer.colorize('red text', 'red')} inside blue text.", "blue"))

Themed Colorization

from colordoll import darktheme, vibranttheme

@darktheme
def get_data():
    return {"key1": "value1", "key2": [1, 2, 3], "key3": True}

@vibranttheme
def get_other_data():
    return [{"name": "Item 1", "value": 10}, {"name": "Item 2", "value": 20}]

print(get_data())
print(get_other_data())

Custom Themes and Configurations

from colordoll import Colorizer, ColorConfig

# Load a custom color configuration from a JSON file
config = ColorConfig("my_colors.json")  # my_colors.json contains your custom color definitions
colorizer = Colorizer(config)

# Create a custom theme
my_theme = {
    "key": "bright_magenta",
    "string": "cyan",
    "number": "yellow",
    "bool": "green",
    "null": "red",
    "other": "blue"
}

# Colorize data using the custom theme
colored_data = colorizer.theme_colorize({"my_key": "my_value", "numbers": [1, 2, 3]}, my_theme)
print(colored_data)

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues.

License

This project is licensed under the MIT License.

Change Log

0.1 (Initial Release)

  • Implemented core colorization functionality.
  • Created nested colorization and background colorization abilities. Superpower!
  • Introduced theming and decorator support.
  • Enabled custom color configurations.
  • Included various pre-defined themes.

0.1.2 Refactor

  • Added Bench

0.1.3 Readme

  • fixed

0.1.4 Rewrites

  • Yaml/json/dicts, etc all work correct now.

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

colordoll-0.1.4.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

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

colordoll-0.1.4-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: colordoll-0.1.4.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for colordoll-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5bb503869aabd52fefa8bcff719f5c5583db5817f7bc97a1a32c3867e01dcfe1
MD5 da01b9025c30bf2559f2683b37296f5a
BLAKE2b-256 85e648b30e2de1fd745b95ea6f095383f619132b3aa5d3b1cd9ef086f09774db

See more details on using hashes here.

File details

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

File metadata

  • Download URL: colordoll-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for colordoll-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5fbc6b75eb4273bd0d2504e97a526bf8db7fd03606eff046d4bce1308fefbd90
MD5 2a3004838b8abcbf4fa082454a29c785
BLAKE2b-256 5dd8a265f8d02f6e858b1a57580a060f284f5910aabf2b9922842641e8ab0ab6

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