diff options
author | Martin Matusiak <numerodix@gmail.com> | 2014-03-08 15:18:28 +0100 |
---|---|---|
committer | Martin Matusiak <numerodix@gmail.com> | 2014-03-08 15:18:28 +0100 |
commit | 2cd4276953ef25d779bb5231f165cea74ccdeee0 (patch) | |
tree | 7456ab1f10ef9ed579eaa535bded12f1407220ae | |
parent | 120647562667f8ad23e6b4364d3bfc4e8e2f63ac (diff) | |
download | ansicolor-2cd4276953ef25d779bb5231f165cea74ccdeee0.tar.gz |
move demos to a separate module
-rw-r--r-- | ansicolor/ansicolor.py | 96 | ||||
-rw-r--r-- | ansicolor/demos.py | 106 | ||||
-rw-r--r-- | tests/test_colors.py | 2 | ||||
-rw-r--r-- | tox.ini | 6 |
4 files changed, 109 insertions, 101 deletions
diff --git a/ansicolor/ansicolor.py b/ansicolor/ansicolor.py index 4841ee9..628078a 100644 --- a/ansicolor/ansicolor.py +++ b/ansicolor/ansicolor.py @@ -306,99 +306,3 @@ def write_out(s): def write_err(s): '''Write a string to stderr, strip escapes if output is a pipe''' write_to(sys.stderr, s) - - -if __name__ == '__main__': - def test_color(): - width = 10 - - lst = [] - - lst.extend([[], ['>>> Without colors'], []]) - line = [] - line.append(colorize("Standard".ljust(width), None)) - line.append(colorize("Bold".ljust(width), None, bold=True)) - line.append(colorize("Reverse".ljust(width), None, reverse=True)) - line.append(colorize("Bold & Rev".ljust(width), None, bold=True, reverse=True)) # noqa - lst.append(line) - - lst.extend([[], ['>>> Using colors'], []]) - for color in Colors.iter(): - line = [] - line.append(colorize(color.__name__.ljust(width), color)) - line.append(colorize(color.__name__.ljust(width), color, bold=True)) # noqa - line.append(colorize(color.__name__.ljust(width), color, reverse=True)) # noqa - line.append(colorize(color.__name__.ljust(width), color, bold=True, reverse=True)) # noqa - lst.append(line) - - lst.extend([[], ['>>> Using highlighting colors'], []]) - for color in Colors.iter(): - color = get_highlighter(color.id) - line = [] - line.append(colorize(color.__name__.ljust(width), color)) - line.append(colorize(color.__name__.ljust(width), color, bold=True)) # noqa - line.append(colorize(color.__name__.ljust(width), color, reverse=True)) # noqa - line.append(colorize(color.__name__.ljust(width), color, bold=True, reverse=True)) # noqa - lst.append(line) - - for line in lst: - for item in line: - write_out('%s ' % item) - write_out("\n") - - def test_highlight(): - import re - rxs = [ - '(b+).*\\1', - '(c+).*\\1', - '(d+).*\\1', - '(e+).*\\1', - ] - s = """\ -aaabbbcccdddeeefffeeedddcccbbbaaa -fffeeedddcccbbbaaabbbcccdddeeefff -""" - def display(rxs, s): - spanlists = [] - for rx in rxs: - spanlist = [] - for m in re.finditer(rx, s): - spanlist.append(m.span()) - spanlists.append(spanlist) - s = highlight_string(s, *spanlists) - for (i, rx) in enumerate(rxs): - color = get_highlighter(i) - color = colorize(color.__name__.ljust(10), color) - write_out('Regex %s: %s %s\n' % (i, color, rx)) - write_out(s) - - for i in range(0, len(rxs) + 1): - write_out('\n') - display(rxs[:i], s) - - def test_diff(): - def display_diff(s, t): - (s_fmt, t_fmt) = colordiff(s, t) - write_out('>>> %s\n' % s_fmt) - write_out(' %s\n\n' % t_fmt) - - display_diff('first last', 'First Last') - display_diff('the the boss', 'the boss') - display_diff('the coder', 'the first coder') - display_diff('agcat', 'gac') - display_diff('XMJYAUZ', 'MZJAWXU') - display_diff('abcdfghjqz', 'abcdefgijkrxyz') - - - try: - action = sys.argv[1] - except IndexError: - print("Usage: %s [ --color | --highlight | --diff ]" % sys.argv[0]) - sys.exit(1) - - if action == '--color': - test_color() - elif action == '--highlight': - test_highlight() - elif action == '--diff': - test_diff() diff --git a/ansicolor/demos.py b/ansicolor/demos.py new file mode 100644 index 0000000..d2892aa --- /dev/null +++ b/ansicolor/demos.py @@ -0,0 +1,106 @@ +from __future__ import absolute_import + +import re +import sys + +from ansicolor.ansicolor import Colors +from ansicolor.ansicolor import colorize +from ansicolor.ansicolor import colordiff +from ansicolor.ansicolor import get_highlighter +from ansicolor.ansicolor import highlight_string +from ansicolor.ansicolor import write_out + + +if __name__ == '__main__': + def test_color(): + width = 10 + + lst = [] + + lst.extend([[], ['>>> Without colors'], []]) + line = [] + line.append(colorize("Standard".ljust(width), None)) + line.append(colorize("Bold".ljust(width), None, bold=True)) + line.append(colorize("Reverse".ljust(width), None, reverse=True)) + line.append(colorize("Bold & Rev".ljust(width), None, bold=True, reverse=True)) # noqa + lst.append(line) + + lst.extend([[], ['>>> Using colors'], []]) + for color in Colors.iter(): + line = [] + line.append(colorize(color.__name__.ljust(width), color)) + line.append(colorize(color.__name__.ljust(width), color, bold=True)) # noqa + line.append(colorize(color.__name__.ljust(width), color, reverse=True)) # noqa + line.append(colorize(color.__name__.ljust(width), color, bold=True, reverse=True)) # noqa + lst.append(line) + + lst.extend([[], ['>>> Using highlighting colors'], []]) + for color in Colors.iter(): + color = get_highlighter(color.id) + line = [] + line.append(colorize(color.__name__.ljust(width), color)) + line.append(colorize(color.__name__.ljust(width), color, bold=True)) # noqa + line.append(colorize(color.__name__.ljust(width), color, reverse=True)) # noqa + line.append(colorize(color.__name__.ljust(width), color, bold=True, reverse=True)) # noqa + lst.append(line) + + for line in lst: + for item in line: + write_out('%s ' % item) + write_out("\n") + + def test_highlight(): + rxs = [ + '(b+).*\\1', + '(c+).*\\1', + '(d+).*\\1', + '(e+).*\\1', + ] + s = """\ +aaabbbcccdddeeefffeeedddcccbbbaaa +fffeeedddcccbbbaaabbbcccdddeeefff +""" + def display(rxs, s): + spanlists = [] + for rx in rxs: + spanlist = [] + for m in re.finditer(rx, s): + spanlist.append(m.span()) + spanlists.append(spanlist) + s = highlight_string(s, *spanlists) + for (i, rx) in enumerate(rxs): + color = get_highlighter(i) + color = colorize(color.__name__.ljust(10), color) + write_out('Regex %s: %s %s\n' % (i, color, rx)) + write_out(s) + + for i in range(0, len(rxs) + 1): + write_out('\n') + display(rxs[:i], s) + + def test_diff(): + def display_diff(s, t): + (s_fmt, t_fmt) = colordiff(s, t) + write_out('>>> %s\n' % s_fmt) + write_out(' %s\n\n' % t_fmt) + + display_diff('first last', 'First Last') + display_diff('the the boss', 'the boss') + display_diff('the coder', 'the first coder') + display_diff('agcat', 'gac') + display_diff('XMJYAUZ', 'MZJAWXU') + display_diff('abcdfghjqz', 'abcdefgijkrxyz') + + + try: + action = sys.argv[1] + except IndexError: + print("Usage: %s [ --color | --highlight | --diff ]" % sys.argv[0]) + sys.exit(1) + + if action == '--color': + test_color() + elif action == '--highlight': + test_highlight() + elif action == '--diff': + test_diff() diff --git a/tests/test_colors.py b/tests/test_colors.py index edf51e9..0e1bb1a 100644 --- a/tests/test_colors.py +++ b/tests/test_colors.py @@ -1,5 +1,3 @@ -import string - from ansicolor import Colors from ansicolor import blue from ansicolor import colordiff @@ -5,7 +5,7 @@ develop=true [testenv] commands= pip install pytest - python -m ansicolor.ansicolor --color - python -m ansicolor.ansicolor --diff - python -m ansicolor.ansicolor --highlight + python -m ansicolor.demos --color + python -m ansicolor.demos --diff + python -m ansicolor.demos --highlight py.test |