Added color_string(), blink "colors", and more
* The list of strings are now joined using ' ' by default * Instead of '' * Added YELLOW_BLINK and RED_BLINK escape codes * print_colored() now optionally logs the msg
This commit is contained in:
parent
89f62562f0
commit
193511d83b
1 changed files with 37 additions and 25 deletions
|
|
@ -32,14 +32,16 @@ from wk.cfg.main import (
|
||||||
|
|
||||||
# STATIC VARIABLES
|
# STATIC VARIABLES
|
||||||
COLORS = {
|
COLORS = {
|
||||||
'CLEAR': '\033[0m',
|
'CLEAR': '\033[0m',
|
||||||
'RED': '\033[31m',
|
'RED': '\033[31m',
|
||||||
'ORANGE': '\033[31;1m',
|
'RED_BLINK': '\033[31;5m',
|
||||||
'GREEN': '\033[32m',
|
'ORANGE': '\033[31;1m',
|
||||||
'YELLOW': '\033[33m',
|
'GREEN': '\033[32m',
|
||||||
'BLUE': '\033[34m',
|
'YELLOW': '\033[33m',
|
||||||
'PURPLE': '\033[35m',
|
'YELLOW_BLINK': '\033[33;5m',
|
||||||
'CYAN': '\033[36m',
|
'BLUE': '\033[34m',
|
||||||
|
'PURPLE': '\033[35m',
|
||||||
|
'CYAN': '\033[36m',
|
||||||
}
|
}
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
REGEX_SIZE_STRING = re.compile(
|
REGEX_SIZE_STRING = re.compile(
|
||||||
|
|
@ -685,6 +687,26 @@ def clear_screen():
|
||||||
subprocess.run(cmd, check=False, shell=True, stderr=subprocess.PIPE)
|
subprocess.run(cmd, check=False, shell=True, stderr=subprocess.PIPE)
|
||||||
|
|
||||||
|
|
||||||
|
def color_string(strings, colors, sep=' '):
|
||||||
|
"""Build colored string using ANSI escapes, returns str."""
|
||||||
|
clear_code = COLORS['CLEAR']
|
||||||
|
msg = []
|
||||||
|
|
||||||
|
# Convert to tuples if necessary
|
||||||
|
if isinstance(strings, str):
|
||||||
|
strings = (strings,)
|
||||||
|
if isinstance(colors, str):
|
||||||
|
colors = (colors,)
|
||||||
|
|
||||||
|
# Build new string with color escapes added
|
||||||
|
for string, color in itertools.zip_longest(strings, colors):
|
||||||
|
color_code = COLORS.get(color, clear_code)
|
||||||
|
msg.append(f'{color_code}{string}{clear_code}')
|
||||||
|
|
||||||
|
# Done
|
||||||
|
return sep.join(msg)
|
||||||
|
|
||||||
|
|
||||||
def generate_debug_report():
|
def generate_debug_report():
|
||||||
"""Generate debug report, returns str."""
|
"""Generate debug report, returns str."""
|
||||||
import socket
|
import socket
|
||||||
|
|
@ -805,26 +827,16 @@ def pause(prompt='Press Enter to continue... '):
|
||||||
def print_colored(strings, colors, **kwargs):
|
def print_colored(strings, colors, **kwargs):
|
||||||
"""Prints strings in the colors specified."""
|
"""Prints strings in the colors specified."""
|
||||||
LOG.debug('strings: %s, colors: %s, kwargs: %s', strings, colors, kwargs)
|
LOG.debug('strings: %s, colors: %s, kwargs: %s', strings, colors, kwargs)
|
||||||
clear_code = COLORS['CLEAR']
|
msg = color_string(strings, colors, sep=kwargs.get('sep', ' '))
|
||||||
msg = ''
|
|
||||||
print_options = {
|
print_options = {
|
||||||
'end': kwargs.get('end', '\n'),
|
'end': kwargs.get('end', '\n'),
|
||||||
'file': kwargs.get('file', sys.stdout),
|
'file': kwargs.get('file', sys.stdout),
|
||||||
'flush': kwargs.get('flush', False),
|
'flush': kwargs.get('flush', False),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Convert to tuples if necessary
|
|
||||||
if isinstance(strings, str):
|
|
||||||
strings = (strings,)
|
|
||||||
if isinstance(colors, str):
|
|
||||||
colors = (colors,)
|
|
||||||
|
|
||||||
# Build new string with color escapes added
|
|
||||||
for string, color in itertools.zip_longest(strings, colors):
|
|
||||||
color_code = COLORS.get(color, clear_code)
|
|
||||||
msg += f'{color_code}{string}{clear_code}'
|
|
||||||
|
|
||||||
print(msg, **print_options)
|
print(msg, **print_options)
|
||||||
|
if kwargs.get('log', False):
|
||||||
|
LOG.info(strip_colors(msg))
|
||||||
|
|
||||||
|
|
||||||
def print_error(msg, log=True, **kwargs):
|
def print_error(msg, log=True, **kwargs):
|
||||||
|
|
@ -832,14 +844,14 @@ def print_error(msg, log=True, **kwargs):
|
||||||
if 'file' not in kwargs:
|
if 'file' not in kwargs:
|
||||||
# Only set if not specified
|
# Only set if not specified
|
||||||
kwargs['file'] = sys.stderr
|
kwargs['file'] = sys.stderr
|
||||||
print_colored([msg], ['RED'], **kwargs)
|
print_colored(msg, 'RED', **kwargs)
|
||||||
if log:
|
if log:
|
||||||
LOG.error(msg)
|
LOG.error(msg)
|
||||||
|
|
||||||
|
|
||||||
def print_info(msg, log=True, **kwargs):
|
def print_info(msg, log=True, **kwargs):
|
||||||
"""Prints message in BLUE and log as INFO."""
|
"""Prints message in BLUE and log as INFO."""
|
||||||
print_colored([msg], ['BLUE'], **kwargs)
|
print_colored(msg, 'BLUE', **kwargs)
|
||||||
if log:
|
if log:
|
||||||
LOG.info(msg)
|
LOG.info(msg)
|
||||||
|
|
||||||
|
|
@ -853,7 +865,7 @@ def print_standard(msg, log=True, **kwargs):
|
||||||
|
|
||||||
def print_success(msg, log=True, **kwargs):
|
def print_success(msg, log=True, **kwargs):
|
||||||
"""Prints message in GREEN and log as INFO."""
|
"""Prints message in GREEN and log as INFO."""
|
||||||
print_colored([msg], ['GREEN'], **kwargs)
|
print_colored(msg, 'GREEN', **kwargs)
|
||||||
if log:
|
if log:
|
||||||
LOG.info(msg)
|
LOG.info(msg)
|
||||||
|
|
||||||
|
|
@ -863,7 +875,7 @@ def print_warning(msg, log=True, **kwargs):
|
||||||
if 'file' not in kwargs:
|
if 'file' not in kwargs:
|
||||||
# Only set if not specified
|
# Only set if not specified
|
||||||
kwargs['file'] = sys.stderr
|
kwargs['file'] = sys.stderr
|
||||||
print_colored([msg], ['YELLOW'], **kwargs)
|
print_colored(msg, 'YELLOW', **kwargs)
|
||||||
if log:
|
if log:
|
||||||
LOG.warning(msg)
|
LOG.warning(msg)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue