From c6301a9fc7807bca57236f19e654df85632cb3f9 Mon Sep 17 00:00:00 2001 From: Gregory Schevchenko <3405066+gvsheva@users.noreply.github.com> Date: Thu, 21 Nov 2019 16:41:42 +0200 Subject: [PATCH] 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 --- ...wrong-promt-len-calc-in-ansible-console.yaml | 2 ++ lib/ansible/cli/console.py | 2 +- lib/ansible/utils/color.py | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/64751-fix-wrong-promt-len-calc-in-ansible-console.yaml diff --git a/changelogs/fragments/64751-fix-wrong-promt-len-calc-in-ansible-console.yaml b/changelogs/fragments/64751-fix-wrong-promt-len-calc-in-ansible-console.yaml new file mode 100644 index 00000000000..4d6343aa7c6 --- /dev/null +++ b/changelogs/fragments/64751-fix-wrong-promt-len-calc-in-ansible-console.yaml @@ -0,0 +1,2 @@ +bugfixes: + - fix wrong command line length calculation in ``ansible-console`` when long command inputted diff --git a/lib/ansible/cli/console.py b/lib/ansible/cli/console.py index a1492ca7b06..a4f1f5dde04 100644 --- a/lib/ansible/cli/console.py +++ b/lib/ansible/cli/console.py @@ -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() diff --git a/lib/ansible/utils/color.py b/lib/ansible/utils/color.py index e6f35c0b587..8762b44f3f8 100644 --- a/lib/ansible/utils/color.py +++ b/lib/ansible/utils/color.py @@ -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