This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ANSI escape sequences breaking string justification
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Krzysztof Słychań, eric.smith, r.david.murray
Priority: normal Keywords:

Created on 2015-07-06 09:21 by Krzysztof Słychań, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg246351 - (view) Author: Krzysztof Słychań (Krzysztof Słychań) Date: 2015-07-06 09:21
Strings containing ANSI escape sequences are not justified if the length including escape sequences exceeds the field width.

Testcase:
Let's make a string with escape sequences - bold, for example:
>>> string = '\033[01m' + 'bold' + '\033[0m'

String length will be larger than the length of a displayed string
(should be 4, is 13). Looks like '\03' is counted as escape sequence,
but '3[01m' and '3[0m' are not, and count into the string length.
>>> len(string)
13

Try to print justified string - should be centered between '|' signs
>>> print('|' + string.center(10) + '|')
|bold|
Does not work at all...

If field length is larger than len(string), justification works
>>> print('|' + string.center(30) + '|')
|        bold         |
msg246357 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2015-07-06 12:19
strings are unaware of any ANSI escape sequences or other structure that is being ascribed to their contents.

The '\033' escape character is being counted, as are the rest of the characters in that string. Since the string is already at least 10 characters long, .center(10) returns the original string.
msg246364 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-07-06 14:35
See issue 12499 for an RFE to textwrap that would at least partially address this use case.
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68762
2015-07-06 14:35:41r.david.murraysetnosy: + r.david.murray
messages: + msg246364
2015-07-06 12:19:26eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg246357

resolution: not a bug
stage: resolved
2015-07-06 09:21:39Krzysztof Słychańcreate