diff options
-rw-r--r-- | README.rst | 4 | ||||
-rw-r--r-- | ansicolor/__init__.py | 2 | ||||
-rw-r--r-- | ansicolor/ansicolor.py | 28 | ||||
-rw-r--r-- | docs/index.rst | 11 | ||||
-rw-r--r-- | docs/snippets/getting_started_3.png | bin | 2620 -> 2747 bytes | |||
-rw-r--r-- | docs/snippets/getting_started_3.py | 4 | ||||
-rw-r--r-- | docs/src/changes.rst | 9 | ||||
-rw-r--r-- | tests/test_colors.py | 15 | ||||
-rw-r--r-- | tox.ini | 3 |
9 files changed, 64 insertions, 12 deletions
@@ -1,8 +1,8 @@ ansicolor ========= -.. image:: https://pypip.in/v/ansicolor/badge.png - :target: https://pypi.python.org/pypi/ansicolor/ +.. image:: https://badge.fury.io/py/ansicolor.png + :target: https://badge.fury.io/py/ansicolor .. image:: https://travis-ci.org/numerodix/ansicolor.png?branch=master :target: https://travis-ci.org/numerodix/ansicolor diff --git a/ansicolor/__init__.py b/ansicolor/__init__.py index 7ef0984..74d2c88 100644 --- a/ansicolor/__init__.py +++ b/ansicolor/__init__.py @@ -32,5 +32,5 @@ __all__ = [ ] __major_version__ = "0.2" -__release__ = "1" +__release__ = "2" __version__ = "%s.%s" % (__major_version__, __release__) diff --git a/ansicolor/ansicolor.py b/ansicolor/ansicolor.py index 9afbcc7..799089f 100644 --- a/ansicolor/ansicolor.py +++ b/ansicolor/ansicolor.py @@ -5,9 +5,10 @@ import difflib import os import re import sys +import warnings -__all__ = [ +__all__ = [ # noqa 'black', 'blue', 'cyan', @@ -141,7 +142,8 @@ def get_code(color, bold=False, reverse=False): return '\033[' + fmt + color + 'm' -def colorize(s, color, bold=False, reverse=False): + +def colorize(s, color, bold=False, reverse=False, start=None, end=None): """ Colorize a string with the color given. @@ -150,11 +152,24 @@ def colorize(s, color, bold=False, reverse=False): :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 int start: Index at which to start coloring. + :param int end: Index at which to end coloring. :rtype: string """ - return ("%s%s%s" % (get_code(color, bold=bold, reverse=reverse), - s, get_code(None))) + 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(color, bold=bold, reverse=reverse), + between, + get_code(None), + after)) + def wrap_string(s, pos, color, bold=False, reverse=False): """ @@ -167,8 +182,13 @@ def wrap_string(s, pos, color, bold=False, reverse=False): :param bool bold: Whether to mark up in bold. :param bool reverse: Whether to mark up in reverse video. :rtype: string + + .. deprecated:: 0.2.2 + This function has been deprecated in favor of :func:`colorize`. """ + warnings.warn("wrap_string is deprecated", PendingDeprecationWarning, 2) + if _disabled: if pos == 0: pos = 1 diff --git a/docs/index.rst b/docs/index.rst index 5e3720d..2cbbc48 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,6 +1,7 @@ ansicolor ========= + User guide ---------- @@ -12,6 +13,7 @@ User guide src/colored_diffs.rst src/marked_up_strings.rst + API documentation ----------------- @@ -21,6 +23,15 @@ API documentation ansicolor +Project documentation +--------------------- + +.. toctree:: + :maxdepth: 4 + + src/changes.rst + + Indices and tables ================== diff --git a/docs/snippets/getting_started_3.png b/docs/snippets/getting_started_3.png Binary files differindex 275dc66..2ec3660 100644 --- a/docs/snippets/getting_started_3.png +++ b/docs/snippets/getting_started_3.png diff --git a/docs/snippets/getting_started_3.py b/docs/snippets/getting_started_3.py index c7d3b62..c0d9a1c 100644 --- a/docs/snippets/getting_started_3.py +++ b/docs/snippets/getting_started_3.py @@ -1,4 +1,4 @@ from ansicolor import Colors -from ansicolor import wrap_string +from ansicolor import colorize -print(wrap_string("I'm blue, said the smurf.", 8, Colors.Blue)) +print(colorize('''"I'm blue", said the smurf.''', Colors.Blue, start=1, end=9)) diff --git a/docs/src/changes.rst b/docs/src/changes.rst new file mode 100644 index 0000000..421c3e7 --- /dev/null +++ b/docs/src/changes.rst @@ -0,0 +1,9 @@ +Release notes +============= + + +0.2.2 +----- + +- :func:`ansicolor.colorize` now accepts ``start`` and ``end`` kwargs. + :func:`ansicolor.wrap_string` has been deprecated. diff --git a/tests/test_colors.py b/tests/test_colors.py index 0e1bb1a..e586520 100644 --- a/tests/test_colors.py +++ b/tests/test_colors.py @@ -44,13 +44,22 @@ def test_highlights(): assert Colors.Yellow == get_highlighter(1) -def test_colorize(): +def test_colorize1(): assert ( get_code(Colors.Red) + "Hi there" + get_code(None) ) == colorize("Hi there", Colors.Red) +def test_colorize2(): + 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 ( @@ -62,7 +71,9 @@ def test_wrap_string(): def test_strip_escapes(): - assert "Hi there" == strip_escapes(wrap_string("Hi there", 3, Colors.Red)) + assert "Hi there" == strip_escapes( + colorize("Hi there", Colors.Red, start=3) + ) assert strip_escapes( colorize("Hi", None, bold=True) + @@ -4,8 +4,9 @@ develop=true [testenv] commands= - pip install pytest + pip install pytest flake8 python -m ansicolor.demos --color python -m ansicolor.demos --diff python -m ansicolor.demos --highlight py.test + flake8 |