[go: up one dir, main page]

File: cprint.py

package info (click to toggle)
colored 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: python: 2,064; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import annotations

from .colored import Colored
from .colored import fore_rgb as foreground_rgb
from .colored import back_rgb as background_rgb


def cprint(text: str,
           fore_256: int | str = None,
           back_256: int | str = None,
           fore_rgb: tuple = None,
           back_rgb: tuple = None,
           formatting: int | str = '',
           line_color: int | str = '',
           reset=True,
           **kwargs) -> None:
    """ Looks like a patch to a built-in python print() function that allows
    to pass colored text and style, to this function.

    Args:
        text: String type text.
        fore_256: Sets the foreground color.
        back_256: Sets the background color.
        fore_rgb: Sets the foreground color.
        back_rgb: Sets the background color.
        formatting: Sets the style of the text.
        line_color: Sets the underline color.
        reset: Reset the formatting style after print, default is True.
    """
    fore_color: str = ''
    back_color: str = ''
    styling: str = Colored(formatting).attribute(line_color)
    terminator: str = Colored('reset').attribute() if reset else ''

    if fore_256:
        fore_color: str = Colored(fore_256).foreground()
    elif fore_rgb:
        fR, fG, fB = fore_rgb
        fore_color: str = foreground_rgb(fR, fG, fB)

    if back_256:
        back_color: str = Colored(back_256).background()
    elif back_rgb:
        bR, bG, bB = back_rgb
        back_color: str = background_rgb(bR, bG, bB)

    print(f'{styling}{back_color}{fore_color}{text}{terminator}', **kwargs)