Adding colours to Python simplified into a single class
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for object_colors-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d1b57e418b348388f734e5179cf2cf1a804553e67e9f284210ed87f0eab595a |
|
MD5 | 74346bd83964f2f401ef8502cf308a86 |
|
BLAKE2b-256 | dab1cb355355ee6e4f62887b8417ed0c2b05ad83c670d2648701f544b5609c32 |