Skip to main content

Object-oriented library for stylizing terminal output

Project description

License pypi CI CodeQL python3.8 black

Object-oriented library for stylizing terminal output


$ pip install object-colors


Import the Color object from object_colors

>>> from object_colors import Color

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

>>> Color(effect="bold", fore="red", back="green")
Color(effect=1, fore=1, back=2, objects())

The following would yield the same result

>>> Color(effect=1, fore=1, back=2)
Color(effect=1, fore=1, back=2, objects())

The above options are part of the below mapping

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

To configure the current object either effect, fore, or back can be provided

They must be an int, str, or None type

>>> c = Color()
>>> c.set(effect="bold", fore="red", back="red")
>>> c
Color(effect=1, fore=1, back=1, objects())

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

Use set to set multiple parameters

>>> c = Color()
>>> c.set(bold_green=dict(effect="bold", fore="green"))
>>> c
Color(effect=None, fore=None, back=None, objects(bold_green))

Return str or tuple using get

>>> c = Color()
>>> c.set(red=dict(fore="red"))
>>> c.set(yellow=dict(fore="yellow"))
>>> f"{'*')} {c.yellow.get('Warning')}"
'\x1b[31m*\x1b[0;0m \x1b[33mWarning\x1b[0;0m'
>>> c = Color()
>>> c.set(red=dict(fore="red"))
>>> xyz ="x", "y", "z")
>>> xyz
('\x1b[31mx\x1b[0;0m', '\x1b[31my\x1b[0;0m', '\x1b[31mz\x1b[0;0m')
>>> x, y, z = xyz
>>> f"{x} {y} {z}"
'\x1b[31mx\x1b[0;0m \x1b[31my\x1b[0;0m \x1b[31mz\x1b[0;0m'

Print the result using print

>>> c = Color(effect="bold", fore="cyan")
>>> # doctest strips ansi codes from print
>>> c.print("bold cyan")  # '\x1b[1;36mbold cyan\x1b[0;0m'
bold cyan

Load all effect, fore, or back elements using populate()

>>> c = Color()
>>> c.populate("fore")
>>> c
Color(effect=None, fore=None, back=None, objects(black, red, green, yellow, blue, magenta, cyan, white))
>>> c = Color()
>>> c.set(red=dict(fore="red"))
Color(effect=None, fore=1, back=None, objects(none, bold, dim, italic, underline, blink, blinking, negative, empty, strikethrough))
>>> # doctest strips ansi codes from print
>>>"strikethrough red")  # '\x1b[9;31mstrikethrough red\x1b[0;0m'
strikethrough red

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

object-colors-2.1.0.tar.gz (7.0 kB view hashes)

Uploaded Source

Built Distribution

object_colors-2.1.0-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page