From a50435acb1c57be89368ee2936c9a3205c50b605 Mon Sep 17 00:00:00 2001 From: Hypnoes Date: Sun, 21 Jan 2018 22:21:14 +0800 Subject: SUPPORT NEW FEATURES: add new function get_code_v2; add new function colorize_v2; support blink text; support underline text; update test_color.py --- .gitignore | 2 ++ ansicolor/ansicolor.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_colors.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/.gitignore b/.gitignore index ca50944..4be52a0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ build/ dist/ docs/_build/ + +.vscode/ diff --git a/ansicolor/ansicolor.py b/ansicolor/ansicolor.py index 5975e65..877330e 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', @@ -454,3 +456,61 @@ 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/tests/test_colors.py b/tests/test_colors.py index e586520..7bcaf2f 100644 --- a/tests/test_colors.py +++ b/tests/test_colors.py @@ -8,6 +8,8 @@ from ansicolor import justify_formatted from ansicolor import red from ansicolor import strip_escapes from ansicolor import wrap_string +from ansicolor import get_code_v2 +from ansicolor import colorize_v2 def test_codes(): @@ -100,3 +102,54 @@ def test_justify_formatted(): assert justify_formatted( red("hi"), rjust, 10 ) == " " + red("hi") + + +def test_codes_v2(): + # reset code + assert '\033[0m' == get_code_v2(None) + + # plain color codes + assert '\033[0;30m' == get_code_v2(Colors.Black) + assert '\033[0;31m' == get_code_v2(Colors.Red) + assert '\033[0;32m' == get_code_v2(Colors.Green) + assert '\033[0;33m' == get_code_v2(Colors.Yellow) + assert '\033[0;34m' == get_code_v2(Colors.Blue) + assert '\033[0;35m' == get_code_v2(Colors.Magenta) + assert '\033[0;36m' == get_code_v2(Colors.Cyan) + assert '\033[0;37m' == get_code_v2(Colors.White) + + # bold, underline, blink, reverse color + assert '\033[1;31m' == get_code_v2(Colors.Red, bold=True) + assert '\033[4;31m' == get_code_v2(Colors.Red, underline=True) + assert '\033[5;31m' == get_code_v2(Colors.Red, blink=True) + assert '\033[7;31m' == get_code_v2(Colors.Red, reverse=True) + + # mixed color + assert '\033[1;4;31m' == get_code_v2(Colors.Red, bold=True, underline=True) + assert '\033[1;5;31m' == get_code_v2(Colors.Red, bold=True, blink=True) + assert '\033[1;7;31m' == get_code_v2(Colors.Red, bold=True, reverse=True) + + assert '\033[4;5;31m' == get_code_v2(Colors.Red, underline=True, blink=True) + assert '\033[4;7;31m' == get_code_v2(Colors.Red, underline=True, reverse=True) + + assert '\033[5;7;31m' == get_code_v2(Colors.Red, blink=True, reverse=True) + + assert '\033[1;4;5;31m' == get_code_v2(Colors.Red, bold=True, underline=True, blink=True) + assert '\033[1;4;7;31m' == get_code_v2(Colors.Red, bold=True, underline=True, reverse=True) + assert '\033[1;5;7;31m' == get_code_v2(Colors.Red, bold=True, blink=True, reverse=True) + assert '\033[1;4;5;7;31m' == get_code_v2(Colors.Red, bold=True, underline=True, blink=True, reverse=True) + +def test_colorize_v2(): + assert ( + get_code(Colors.Red) + + "Hi there" + + get_code(None) + ) == colorize("Hi there", Colors.Red) + + assert ( + "H" + + get_code(Colors.Red) + + "i ther" + + get_code(None) + + "e" + ) == colorize("Hi there", Colors.Red, start=1, end=7) -- cgit v1.2.1 From 4104c0c4756838b881c2045ad1a12057f861dbb8 Mon Sep 17 00:00:00 2001 From: Hypnoes Date: Mon, 22 Jan 2018 18:05:33 +0800 Subject: 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 --- ansicolor/ansicolor.py | 118 +++++++++++++++++++++++++------------------------ ansicolor/demos.py | 44 ++++++++++++++++++ tests/test_colors.py | 100 ++++++++++++++++++++--------------------- 3 files changed, 153 insertions(+), 109 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': diff --git a/tests/test_colors.py b/tests/test_colors.py index 7bcaf2f..ea1fb38 100644 --- a/tests/test_colors.py +++ b/tests/test_colors.py @@ -35,6 +35,40 @@ def test_codes(): # bold + reverse color assert '\033[1;7;31m' == get_code(Colors.Red, bold=True, reverse=True) +def test_codes_v2(): + # reset code + assert '\033[0m' == get_code_v2(None) + + # plain color codes + assert '\033[0;30m' == get_code_v2(Colors.Black) + assert '\033[0;31m' == get_code_v2(Colors.Red) + assert '\033[0;32m' == get_code_v2(Colors.Green) + assert '\033[0;33m' == get_code_v2(Colors.Yellow) + assert '\033[0;34m' == get_code_v2(Colors.Blue) + assert '\033[0;35m' == get_code_v2(Colors.Magenta) + assert '\033[0;36m' == get_code_v2(Colors.Cyan) + assert '\033[0;37m' == get_code_v2(Colors.White) + + # bold, underline, blink, reverse color + assert '\033[1;31m' == get_code_v2(Colors.Red, bold=True) + assert '\033[4;31m' == get_code_v2(Colors.Red, underline=True) + assert '\033[5;31m' == get_code_v2(Colors.Red, blink=True) + assert '\033[7;31m' == get_code_v2(Colors.Red, reverse=True) + + # mixed color + assert '\033[1;4;31m' == get_code_v2(Colors.Red, bold=True, underline=True) + assert '\033[1;5;31m' == get_code_v2(Colors.Red, bold=True, blink=True) + assert '\033[1;7;31m' == get_code_v2(Colors.Red, bold=True, reverse=True) + + assert '\033[4;5;31m' == get_code_v2(Colors.Red, underline=True, blink=True) + assert '\033[4;7;31m' == get_code_v2(Colors.Red, underline=True, reverse=True) + + assert '\033[5;7;31m' == get_code_v2(Colors.Red, blink=True, reverse=True) + + assert '\033[1;4;5;31m' == get_code_v2(Colors.Red, bold=True, underline=True, blink=True) + assert '\033[1;4;7;31m' == get_code_v2(Colors.Red, bold=True, underline=True, reverse=True) + assert '\033[1;5;7;31m' == get_code_v2(Colors.Red, bold=True, blink=True, reverse=True) + assert '\033[1;4;5;7;31m' == get_code_v2(Colors.Red, bold=True, underline=True, blink=True, reverse=True) def test_coloring(): assert '\033[0;0;31m' + 'hi' + '\033[0;0m' == red('hi') @@ -62,6 +96,21 @@ def test_colorize2(): + "e" ) == colorize("Hi there", Colors.Red, start=1, end=7) +def test_colorize_v2(): + assert ( + get_code(Colors.Red) + + "Hi there" + + get_code(None) + ) == colorize("Hi there", Colors.Red) + + assert ( + "H" + + get_code(Colors.Red) + + "i ther" + + get_code(None) + + "e" + ) == colorize("Hi there", Colors.Red, start=1, end=7) + def test_wrap_string(): assert ( @@ -102,54 +151,3 @@ def test_justify_formatted(): assert justify_formatted( red("hi"), rjust, 10 ) == " " + red("hi") - - -def test_codes_v2(): - # reset code - assert '\033[0m' == get_code_v2(None) - - # plain color codes - assert '\033[0;30m' == get_code_v2(Colors.Black) - assert '\033[0;31m' == get_code_v2(Colors.Red) - assert '\033[0;32m' == get_code_v2(Colors.Green) - assert '\033[0;33m' == get_code_v2(Colors.Yellow) - assert '\033[0;34m' == get_code_v2(Colors.Blue) - assert '\033[0;35m' == get_code_v2(Colors.Magenta) - assert '\033[0;36m' == get_code_v2(Colors.Cyan) - assert '\033[0;37m' == get_code_v2(Colors.White) - - # bold, underline, blink, reverse color - assert '\033[1;31m' == get_code_v2(Colors.Red, bold=True) - assert '\033[4;31m' == get_code_v2(Colors.Red, underline=True) - assert '\033[5;31m' == get_code_v2(Colors.Red, blink=True) - assert '\033[7;31m' == get_code_v2(Colors.Red, reverse=True) - - # mixed color - assert '\033[1;4;31m' == get_code_v2(Colors.Red, bold=True, underline=True) - assert '\033[1;5;31m' == get_code_v2(Colors.Red, bold=True, blink=True) - assert '\033[1;7;31m' == get_code_v2(Colors.Red, bold=True, reverse=True) - - assert '\033[4;5;31m' == get_code_v2(Colors.Red, underline=True, blink=True) - assert '\033[4;7;31m' == get_code_v2(Colors.Red, underline=True, reverse=True) - - assert '\033[5;7;31m' == get_code_v2(Colors.Red, blink=True, reverse=True) - - assert '\033[1;4;5;31m' == get_code_v2(Colors.Red, bold=True, underline=True, blink=True) - assert '\033[1;4;7;31m' == get_code_v2(Colors.Red, bold=True, underline=True, reverse=True) - assert '\033[1;5;7;31m' == get_code_v2(Colors.Red, bold=True, blink=True, reverse=True) - assert '\033[1;4;5;7;31m' == get_code_v2(Colors.Red, bold=True, underline=True, blink=True, reverse=True) - -def test_colorize_v2(): - assert ( - get_code(Colors.Red) - + "Hi there" - + get_code(None) - ) == colorize("Hi there", Colors.Red) - - assert ( - "H" - + get_code(Colors.Red) - + "i ther" - + get_code(None) - + "e" - ) == colorize("Hi there", Colors.Red, start=1, end=7) -- cgit v1.2.1