From 5b5d80ab2d8a659497fe8806adf9f31388c2730a Mon Sep 17 00:00:00 2001 From: TheTechRobo <52163910+TheTechRobo@users.noreply.github.com> Date: Thu, 5 May 2022 20:05:54 -0400 Subject: [PATCH] feat(formatting): Add bold support. Some notes: I made a new Classmethod for automating the check for bold. The `bold` arg comes after the fatal message and interrupt arguments, in functions that support them. However, the fatal message is not bolded. --- cprint/cprint.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/cprint/cprint.py b/cprint/cprint.py index d1322c7..d55cee5 100644 --- a/cprint/cprint.py +++ b/cprint/cprint.py @@ -20,7 +20,8 @@ class cprint(object): 'WARNING': '\033[93m', 'ERR': '\033[91m', 'FATAL': '\033[31m', - 'ENDC': '\033[0m' + 'ENDC': '\033[0m', + 'BOLD': '\033[1m' } def __init__(self, str): @@ -37,36 +38,44 @@ def _get_repr(cls, arg): return repr(arg) @classmethod - def ok(cls, arg, *args, **kwargs): + def _check_is_bold(cls, bold): + return cprint.colors["BOLD"] if bold else "" + + @classmethod + def ok(cls, arg, *args, bold=False, **kwargs): """ Prints in blue to stdout + bold=True: Prints in bold. """ - print(cprint.colors['OK'] + cls._get_repr(arg) + cprint.colors['ENDC'], + print(cprint.colors['OK'] + cls._check_is_bold(bold) + cls._get_repr(arg) + cprint.colors['ENDC'], file=sys.stdout, *args, **kwargs) @classmethod - def info(cls, arg, *args, **kwargs): + def info(cls, arg, *args, bold=False, **kwargs): """ Prints in green to stdout + bold=True: Prints in bold. """ - print(cprint.colors['INFO'] + cls._get_repr(arg) + cprint.colors['ENDC'], + print(cprint.colors['INFO'] + cls._check_is_bold(bold) + cls._get_repr(arg) + cprint.colors['ENDC'], file=sys.stdout, *args, **kwargs) @classmethod - def warn(cls, arg, *args, **kwargs): + def warn(cls, arg, *args, bold=False, **kwargs): """ - Prints in yellow to strerr + Prints in yellow to stderr + bold=True: Prints in bold. """ - print(cprint.colors['WARNING'] + cls._get_repr(arg) + cprint.colors['ENDC'], + print(cprint.colors['WARNING'] + cls._check_is_bold(bold) + cls._get_repr(arg) + cprint.colors['ENDC'], file=sys.stderr, *args, **kwargs) @classmethod - def err(cls, arg, interrupt=False, fatal_message="Fatal error: Program stopped.", *args, **kwargs): + def err(cls, arg, interrupt=False, fatal_message="Fatal error: Program stopped.", *args, bold=False, **kwargs): """ Prints in brown to stderr interrupt=True: stops the program + bold=True: Prints in bold. Fatal message will not be bold. """ - print(cprint.colors['ERR'] + cls._get_repr(arg) + cprint.colors['ENDC'], + print(cprint.colors['ERR'] + cls._check_is_bold(bold) + cls._get_repr(arg) + cprint.colors['ENDC'], file=sys.stderr, *args, **kwargs) if interrupt: print(cprint.colors['ERR'] + fatal_message + @@ -75,12 +84,13 @@ def err(cls, arg, interrupt=False, fatal_message="Fatal error: Program stopped." exit(1) @classmethod - def fatal(cls, arg, interrupt=False, fatal_message="Fatal error: Program stopped.", *args, **kwargs): + def fatal(cls, arg, interrupt=False, fatal_message="Fatal error: Program stopped.", *args, bold=False, **kwargs): """ Prints in red to stderr interrupt=True: stops the program + bold=True: Makes text bold. Fatal message will not be printed in bold. """ - print(cprint.colors['FATAL'] + cls._get_repr(arg) + cprint.colors['ENDC'], + print(cprint.colors['FATAL'] + cls._check_is_bold(bold) + cls._get_repr(arg) + cprint.colors['ENDC'], file=sys.stderr, *args, **kwargs) if interrupt: print(cprint.colors['FATAL'] + fatal_message +