diff options
author | Hypnoes <hypnoes@qq.com> | 2018-01-22 18:05:33 +0800 |
---|---|---|
committer | Hypnoes <hypnoes@qq.com> | 2018-01-22 18:05:33 +0800 |
commit | 4104c0c4756838b881c2045ad1a12057f861dbb8 (patch) | |
tree | 52114b40c9c89da7017e9d913f9868fe2667d6d1 /ansicolor | |
parent | a50435acb1c57be89368ee2936c9a3205c50b605 (diff) | |
download | ansicolor-4104c0c4756838b881c2045ad1a12057f861dbb8.tar.gz |
update ansicolor/ansicolor.py, ansicolor/demos.py, tests/test_colors.py
ansicolor/ansicolor.py: move function get_code_v2 colorize_v2 follow the original function
tests/test_colors.py: move test test_color_v2 behind test_color
ansicolor/demos.py: new demo demo_color_v2
Diffstat (limited to 'ansicolor')
-rw-r--r-- | ansicolor/ansicolor.py | 118 | ||||
-rw-r--r-- | ansicolor/demos.py | 44 |
2 files changed, 104 insertions, 58 deletions
diff --git a/ansicolor/ansicolor.py b/ansicolor/ansicolor.py index 877330e..f84a938 100644 --- a/ansicolor/ansicolor.py +++ b/ansicolor/ansicolor.py @@ -143,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): """ @@ -171,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): """ @@ -456,61 +516,3 @@ def write_err(s): """ write_to(sys.stderr, s) - -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_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, reverse=reverse), - between, - get_code_v2(None), - after)) diff --git a/ansicolor/demos.py b/ansicolor/demos.py index 4b65426..d724250 100644 --- a/ansicolor/demos.py +++ b/ansicolor/demos.py @@ -5,6 +5,7 @@ import sys from ansicolor.ansicolor import Colors from ansicolor.ansicolor import colorize +from ansicolor.ansicolor import colorize_v2 from ansicolor.ansicolor import colordiff from ansicolor.ansicolor import get_highlighter from ansicolor.ansicolor import highlight_string @@ -48,6 +49,48 @@ def demo_color(): write_out('%s ' % item) write_out("\n") +def demo_color_v2(): + width = 10 + + lst = [] + + lst.extend([[], ['>>> Without colors'], []]) + line = [] + line.append(colorize_v2("Standard".ljust(width), None)) + line.append(colorize_v2("Bold".ljust(width), None, bold=True)) + line.append(colorize_v2("Underline".ljust(width), None, underline=True)) + line.append(colorize_v2("Blink".ljust(width), None, blink=True)) + line.append(colorize_v2("Reverse".ljust(width), None, reverse=True)) + line.append(colorize_v2("Bold & Rev".ljust(width), None, bold=True, underline=True, reverse=True)) # noqa + lst.append(line) + + lst.extend([[], ['>>> Using colors_v2'], []]) + for color in Colors.iter(): + line = [] + line.append(colorize_v2(color.__name__.ljust(width), color)) + line.append(colorize_v2(color.__name__.ljust(width), color, bold=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, underline=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, blink=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, reverse=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, bold=True, underline=True, reverse=True)) # noqa + lst.append(line) + + lst.extend([[], ['>>> Using highlighting colors_v2'], []]) + for color in Colors.iter(): + color = get_highlighter(color.id) + line = [] + line.append(colorize_v2(color.__name__.ljust(width), color)) + line.append(colorize_v2(color.__name__.ljust(width), color, bold=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, underline=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, blink=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, reverse=True)) # noqa + line.append(colorize_v2(color.__name__.ljust(width), color, bold=True, underline=True, reverse=True)) # noqa + lst.append(line) + + for line in lst: + for item in line: + write_out('%s ' % item) + write_out("\n") def _demo_highlight(reverse=False): rxs = [ @@ -118,6 +161,7 @@ if __name__ == '__main__': if action == '--color': demo_color() + demo_color_v2() elif action == '--highlight': demo_highlight() elif action == '--highlight-reverse': |