object_colors

Object-oriented library for stylizing terminal output.

class object_colors.Color(effect: int | str | None = None, fore: int | str | None = None, back: int | str | None = None)[source]

Bases: object

Color object.

Args passed to constructor call may be strings or integers. There are a defined set of options for each.

The list of options referenced below are the string form.

The integer that can be called is the index of the list item beginning.

The two types of attributes to set are the object’s instance attributes, and the dynamic object attributes.

Standard attributes can be either int, str, or None.

Object values can only be a dict to create a new object.

All int values correspond to the index of the color or effect and their respective ANSI code.

All str values will be converted to their index integer.

Ensure int passed as parameter does not exceed the length of the key’s index.

Ensure str is one of the specific strings matching the key’s index.

If a key does not match effect, fore, or back it must be a dict which can be instantiated to create a new named object.

@DynamicAttrs

Parameters:
  • effect – Effect applied to text output. Select from the following Color.effects.

  • fore – Foreground color applied to text output. Select from the following Color.colors.

  • back – Background color applied to text output. Select from the following Color.colors.

colors: Tuple[str, str, str, str, str, str, str, str] = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
effects: Tuple[str, str, str, str, str, str, str, str, str, str] = ('none', 'bold', 'dim', 'italic', 'underline', 'blink', 'blinking', 'negative', 'empty', 'strikethrough')
get(*args: Any, format: bool = False) Any[source]

Return colored str or tuple depending on the arg passed.

Parameters:
  • args – Manipulate string(s).

  • format – Return a string instead of a tuple if strings are passed as tuple.

Returns:

Colored string or None.

populate(elem: str) None[source]

Create an object for every available selection.

Parameters:

elem – Attribute to fill with available options.

Raises:

AttributeError – If element does not exist.

populate_colors() None[source]

Create an object for every available foreground color.

Deprecated.

print(*args, file: Any | None = None, sep: str = ' ', end: str = '\n', flush: bool = False) None[source]

Print colored strings using the builtin print function.

Parameters:
  • args – String(s) to print.

  • file – A file-like object (stream); defaults to the current sys.stdout.

  • sep – String inserted between values, default a space.

  • end – String appended after the last value, default a newline.

  • flush – Whether to forcibly flush the stream.

set(**kwargs: str | int | dict[str, _t.Any]) None[source]

Call to set new instance values.

If not making a subclass then process args and kwargs and add compiled dict to masterclass.

Key effect:

Text effect to use.

Key fore:

Color of text foreground.

Key back:

Color of text background.

Key dict:

If **kwargs are provided then any keyword can be provided.