fix utils.color.stringc: enclosure non printable sequences in SOH,STX (#64751)

* ansible.utils.color.stringc: add wrap_nonvisible_chars flag in stringc
* add exaplanation for `wrap_nonvisible_chars` case in utils.stringc
* add changelog entry
This commit is contained in:
Gregory Schevchenko 2019-11-21 16:41:42 +02:00 committed by Sam Doran
parent 1a7823af03
commit c6301a9fc7
3 changed files with 18 additions and 3 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- fix wrong command line length calculation in ``ansible-console`` when long command inputted

View file

@ -122,7 +122,7 @@ class ConsoleCLI(CLI, cmd.Cmd):
else:
prompt += "$ "
color = self.NORMAL_PROMPT
self.prompt = stringc(prompt, color)
self.prompt = stringc(prompt, color, wrap_nonvisible_chars=True)
def list_modules(self):
modules = set()

View file

@ -85,12 +85,25 @@ def parsecolor(color):
return u'38;5;%d' % (232 + int(matches.group('gray')))
def stringc(text, color):
def stringc(text, color, wrap_nonvisible_chars=False):
"""String in color."""
if ANSIBLE_COLOR:
color_code = parsecolor(color)
return u"\n".join([u"\033[%sm%s\033[0m" % (color_code, t) for t in text.split(u'\n')])
fmt = u"\033[%sm%s\033[0m"
if wrap_nonvisible_chars:
# This option is provided for use in cases when the
# formatting of a command line prompt is needed, such as
# `ansible-console`. As said in `readline` sources:
# readline/display.c:321
# /* Current implementation:
# \001 (^A) start non-visible characters
# \002 (^B) end non-visible characters
# all characters except \001 and \002 (following a \001) are copied to
# the returned string; all characters except those between \001 and
# \002 are assumed to be `visible'. */
fmt = u"\001\033[%sm\002%s\001\033[0m\002"
return u"\n".join([fmt % (color_code, t) for t in text.split(u'\n')])
else:
return text