...
 
Commits (6)
07-05-2017
Version 1.3.5
[Added] - readline-safe stylize_interactive() #6
20-01-2017
Version 1.3.4
[Fixed] - Color capitalization
[Updated] - Year license
19-11-2016
Version 1.3.3
[Updated] - Updated to make HEX codes case-insensitive #2
......
......@@ -411,6 +411,15 @@ Or the convenient `stylize(text, *styles)` wrapper to save some keystrokes:
>>> print("But this is not.")
But this is not.
Or the variant `stylize_interactive(text, *styles)` for readline-friendliness:
.. code-block:: bash
>>> import colored, sys
>>> from colored import stylize_interactive, fg
>>> sys.ps1 = stylize_interactive("myPrompt: ", fg('red'))
myPrompt:
Use directly like `colorama <https://pypi.python.org/pypi/colorama>`_ but with more colors:
.. code-block:: bash
......
......@@ -30,5 +30,5 @@ from .fore import *
from .back import *
from .style import *
__version_info__ = (1, 3, 4)
__version_info__ = (1, 3, 5)
__version__ = '{0}.{1}.{2}'.format(*__version_info__)
......@@ -390,3 +390,20 @@ def stylize(text, styles, reset=True):
"""conveniently styles your text as and resets ANSI codes at its end."""
terminator = attr("reset") if reset else ""
return "{}{}{}".format("".join(styles), text, terminator)
def _c0wrap(styles):
"""wrap a set of ANSI styles in C0 control codes for readline safety."""
C0_SOH = '\x01' # mark the beginning of nonprinting characters
C0_STX = '\x02' # mark the end of nonprinting characters
return "{}{}{}".format(C0_SOH, "".join(styles), C0_STX)
def stylize_interactive(text, styles, reset=True):
"""stylize() variant that adds C0 control codes (SOH/STX) for readline
safety."""
# problem: readline includes bare ANSI codes in width calculations.
# solution: wrap nonprinting codes in SOH/STX when necessary.
# see: https://github.com/dslackw/colored/issues/5
terminator = _c0wrap(attr("reset")) if reset else ""
return "{}{}{}".format(_c0wrap(styles), text, terminator)
File mode changed from 100644 to 100755