added cprint (simple colored print function)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user