added cprint (simple colored print function)

This commit is contained in:
2022-03-17 12:04:59 +01:00
committed by NichtJens
parent c83d2d9bdb
commit 492a4319ec
2 changed files with 43 additions and 0 deletions
+1
View File
@@ -2,6 +2,7 @@
from .utils import *
from .argfwd import forwards_to
from .channels import load_channels, Channels
from .cprint import cprint
from .config import Config
from .elog import Elog
from .eval import arithmetic_eval
+42
View File
@@ -0,0 +1,42 @@
from colorama import Fore
COLORS = {
"black": Fore.BLACK,
"blue": Fore.BLUE,
"cyan": Fore.CYAN,
"green": Fore.GREEN,
"magenta": Fore.MAGENTA,
"red": Fore.RED,
"white": Fore.WHITE,
"yellow": Fore.YELLOW,
None: None
}
def ncprint(*objects, color=None, sep=" ", **kwargs):
return cprint(*objects, color=None, sep=sep, **kwargs)
def cprint(*objects, color=None, sep=" ", **kwargs):
color = get_color(color)
text = flatten_strings(objects, sep)
return _print(color, text, sep, kwargs)
def get_color(color):
try:
return COLORS[color]
except KeyError as exc:
color = repr(color)
allowed = tuple(COLORS.keys())
raise ValueError(f"{color} not from {allowed}") from exc
def flatten_strings(objects, sep):
return sep.join(str(i) for i in objects)
def _print(color, text, sep, kwargs):
if color is not None:
text = color + text + Fore.RESET
return print(text, sep=sep, **kwargs)