diff options
Diffstat (limited to 'ansicolor/ansicolor.py')
-rw-r--r-- | ansicolor/ansicolor.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/ansicolor/ansicolor.py b/ansicolor/ansicolor.py index 5975e65..f84a938 100644 --- a/ansicolor/ansicolor.py +++ b/ansicolor/ansicolor.py @@ -18,8 +18,10 @@ __all__ = [ # noqa 'yellow', 'colorize', + 'colorize_v2', 'wrap_string', 'get_code', + 'get_code_v2', 'highlight_string', 'get_highlighter', @@ -141,6 +143,34 @@ def get_code(color, bold=False, reverse=False): return '\033[' + fmt + color + 'm' +def get_code_v2(color, bold=False, reverse=False, underline=False, blink=False): + """ + Returns the escape code for styling with the given color, + in bold and/or reverse. + :param color: The color to use. + :type color: :class:`Colors` class + :param bool bold: Whether to mark up in bold. + :param bool underline: Whether to mark up in underline. + :param bool blink: Whether to mark up in blink. + :param bool reverse: Whether to mark up in reverse video. + :rtype: string + """ + + if _disabled: + return "" + + fmt = '0' + l = [] + if bold: l.append('1') + if underline: l.append('4') + if blink: l.append('5') + if reverse: l.append('7') + if len(l) != 0: + fmt = ';'.join(l) + + color = (color is not None) and ';3%s' % color.id or '' + + return '\033[' + fmt + color + 'm' def colorize(s, color, bold=False, reverse=False, start=None, end=None): """ @@ -169,6 +199,38 @@ def colorize(s, color, bold=False, reverse=False, start=None, end=None): get_code(None), after)) +def colorize_v2(s, color, bold=False, reverse=False, underline=False, blink=False, + start=None, end=None): + """ + Colorize a string with the color given. + :param string s: The string to colorize. + :param color: The color to use. + :type color: :class:`Colors` class + :param bool bold: Whether to mark up in bold. + :param bool reverse: Whether to mark up in reverse video. + :param bool blink: Whether to mark up in blink. + :param bool reverse: Whether to mark up in reverse video. + :param int start: Index at which to start coloring. + :param int end: Index at which to end coloring. + :rtype: string + """ + + start = start if start else 0 + end = end if end else len(s) + + before = s[:start] + between = s[start:end] + after = s[end:] + + return ("%s%s%s%s%s" % (before, + get_code_v2(color, bold=bold, + underline=underline, + blink=blink, + reverse=reverse), + between, + get_code_v2(None), + after)) + def wrap_string(s, pos, color, bold=False, reverse=False): """ |