Skip to main content

Adding colours to Python simplified into a single class

Project description

build python3.8 pypi codecov.io readthedocs.org mit black

Object-oriented library for stylizing terminal output

Installation

$ pip install object-colors

Options

Args can be provided as strings or as indices corresponding to their index in an ANSI escape sequence

The following would yield the same result

>>> from object_colors import Color
>>> c = Color(effect="bold", fore="red", back="green")
>>> print(c)
Color(effect=1, fore=1, back=2, objects())
>>> c = Color(effect=1, fore=1, back=2)
>>> print(c)
Color(effect=1, fore=1, back=2, objects())
The above options are part of the below mapping
>>> from object_colors import Color
>>> for i, c in enumerate(Color.colors):
...     print(i, c)
0 black
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 white
>>> for i, e in enumerate(Color.effects):
...     print(i, e)
0 none
1 bold
2 dim
3 italic
4 underline
5 blink
6 blinking
7 negative
8 empty
9 strikethrough

Usage

To configure the current object either ``effect``, ``fore``, or ``back`` can be provided and they must be an ``int``, ``str``, or ``None``

Create new objects with by providing a ``dict`` object with any keyword argument

Use ``set`` to set multiple parameters

>>> from object_colors import Color
>>> c = Color()
>>> c.set(effect="bold", fore="red", back="red")
>>> print(c)
Color(effect=1, fore=1, back=1, objects())
>>> c.set(bold_green={"effect": "bold", "fore": "green"})
>>> print(c)
Color(effect=1, fore=1, back=1, objects(bold_green))
Return ``str`` or ``tuple`` using ``get``
>>> from object_colors import Color
>>> c = Color()
>>> c.set(red={"fore": "red"})
>>> c.set(yellow={"fore": "yellow"})
>>> print(c.red.get("*") + " " + c.yellow.get("Warning"))
'\u001b[31m*\u001b[0;0m \u001b[33mWarning\u001b[0;0m'
>>> x, y, z = c.red.get("x", "y", "z")
>>> print(x, y, z)
'\u001b[31mx\u001b[0;0m \u001b[31my\u001b[0;0m \u001b[31mz\u001b[0;0m'
Print the result using ``print``
>>> from object_colors import Color
>>> c = Color(effect="bold", fore="cyan")
>>> c.print("bold cyan")
'\u001b[1;36mbold cyan\u001b[0;0m'
Load all ``effect``, ``fore``, or ``back`` elements using ``populate``
>>> from object_colors import Color
>>> c = Color()
>>> c.populate("fore")
>>> print(c)
Color(effect=None, fore=None, back=None, objects(black, red, green, yellow, blue, magenta, cyan, white))
>>> c.red.populate("effect")
>>> print(c.red)
Color(effect=None, fore=1, back=None, objects(none, bold, dim, italic, underline, blink, blinking, negative, empty, strikethrough))
>>> c.red.strikethrough.print("strikethrough red")
'\u001b[9;31mstrikethrough red\u001b[0;0m'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for object-colors, version 2.0.1
Filename, size File type Python version Upload date Hashes
Filename, size object_colors-2.0.1.tar.gz (6.6 kB) File type Source Python version None Upload date Hashes View
Filename, size object_colors-2.0.1-py3-none-any.whl (6.5 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page